Location: PHPKode > projects > ContentCMS > core/content_rule.class.php
<?php
/* 
 * @package     ContentCMS
 * @author      Dan Goldsmith
 * @copyright   Dan Goldsmith 2012
 * @link        http://contentcms.d2g.org.uk/
 * @version     {SUBVERSION_BUILD_NUMBER}
 * 
 * @licence     MPL 2.0
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 *
 *
 * Defines the rules where content_id's are automatically assigned to 
 * plugins on generation. This is so that if you create a new blog the 
 * post automatically getenrates the comments etc.
 */
class content_rule
{
    private $id                     = null;
    private $regular_expression     = null;
    private $plugin                 = null;
    private $priority               = 0;
    
    public function __construct($id = null) 
    {
        if($id !== null)
        {
            $this->setID($id);
            $this->getFromDB();
        }
    }
    
    private function setID($id)
    {
        $this->id = $id;
    }
    
    public function getID()
    {
        return $this->id;
    }
    
    public function setRegularExpression($expresion)
    {
        $this->regular_expression = $expresion;
    }
    
    public function getRegularExpression()
    {
        return $this->regular_expression;
    }
    
    public function setPlugin($plugin_name)
    {
        $this->plugin = $plugin_name;
    }
    
    public function getPlugin()
    {
        return $this->plugin;
    }
    
    public function setPriority($priority)
    {
        $this->priority = $priority;
    }
    
    public function getPriority()
    {
        return $this->priority;
    }
    
    public function getFromDB()
    {
        $connection = new simple_mysqli();
        
        $select_sql =   "
                        SELECT
                            *
                        FROM
                            `" . CONTENT_DB_NAME . "`.`" . CONTENT_DB_TABLE_PREFIX . "tbl_content_rule`
                        WHERE
                            `cr_id` = '" . $connection->real_escape_string($this->getID()) . "'
                        ";

        if(!($result = $connection->query($select_sql)))
        {
            throw new Exception(CONTENT_DB_HOST . " SQL: " . $select_sql . " ERROR: " . $connection->error);
        }

        if($result->num_rows > 0)
        {
            $record = $result->fetch_assoc();
            $this->setRegularExpression($record['cr_regular_expression']);
            $this->setPlugin($record['cr_content_plugin']);
            $this->setPriority($record['cr_priority']);
        }
        
        $connection->close();
    }
    
    public function setToDB()
    {
        if($this->getID() === null)
        {
            $this->writeToDB();
        }
        else
        {
            $this->updateToDB();
        }
    }
    
    private function writeToDB()
    {
        $connection = new simple_mysqli();

        $insert_sql =   "
                        INSERT INTO `" . CONTENT_DB_NAME . "`.`" . CONTENT_DB_TABLE_PREFIX . "tbl_content_rule`
                        (
                            `cr_id`,
                            `cr_regular_expression`,
                            `cr_content_plugin`,
                            `cr_priority`
                        )
                        VALUES
                        (
                            '" . $connection->real_escape_string($this->getID()) . "',
                            '" . $connection->real_escape_string($this->getRegularExpression()) . "',
                            '" . $connection->real_escape_string($this->getPlugin()) . "',
                            '" . $connection->real_escape_string($this->getPriority()) . "'
                        )
                        ";
        
        if(!($result = $connection->query($insert_sql)))
        {
            throw new Exception(CONTENT_DB_HOST . " SQL: " . $insert_sql . " ERROR: " . $connection->error);
        }
        
        $this->setID($connection->insert_id);
        
        $connection->close();        
    }
    
    private function updateToDB()
    {
       $connection = new simple_mysqli();

        $update_sql =   "
                        UPDATE `" . CONTENT_DB_NAME . "`.`" . CONTENT_DB_TABLE_PREFIX . "tbl_content_rule`
                        SET
                            `cr_regular_expression` =   '" . $connection->real_escape_string($this->getRegularExpression()) . "',
                            `cr_content_plugin`     =   '" . $connection->real_escape_string($this->getPlugin()) . "',
                            `cr_priority`           =   '" . $connection->real_escape_string($this->getPriority()) . "'
                        WHERE
                            `cr_id` = '" . $connection->real_escape_string($this->getID()) . "'
                        ";

        if(!($result = $connection->query($update_sql)))
        {
            throw new Exception(CONTENT_DB_HOST . " SQL: " . $update_sql . " ERROR: " . $connection->error);
        }
        $connection->close();        
    }
    
    public function deleteFromDB()
    {
       $connection = new simple_mysqli();

        $delete_sql =   "
                        DELETE FROM `" . CONTENT_DB_NAME . "`.`" . CONTENT_DB_TABLE_PREFIX . "tbl_content_rule`
                        WHERE
                            `cr_id` = '" . $connection->real_escape_string($this->getID()) . "'
                        ";

        if(!($result = $connection->query($delete_sql)))
        {
            throw new Exception(CONTENT_DB_HOST . " SQL: " . $delete_sql . " ERROR: " . $connection->error);
        }
        
        $this->setID(null);
        
        $connection->close();         
    }
    
    
    public static function getAll()
    {
        $rules = array();
        
        $connection = new simple_mysqli();
        
        $select_sql =   "
                        SELECT
                            `cr_id`
                        FROM
                            `" . CONTENT_DB_NAME . "`.`" . CONTENT_DB_TABLE_PREFIX . "tbl_content_rule`
                        ORDER BY
                            `cr_priority` desc
                        ";

        if(!($result = $connection->query($select_sql)))
        {
            throw new Exception(CONTENT_DB_HOST . " SQL: " . $select_sql . " ERROR: " . $connection->error);
        }

        while($record = $result->fetch_assoc())
        {
            $rules[] = new self($record['cr_id']);
        }

        $connection->close();
        
        return $rules;
    }
    
    public static function getPluginIDForContentID($content_id)
    {
        $rules = self::getAll();
        
        foreach($rules as $rule)
        {
            if(preg_match($rule->getRegularExpression(), $content_id) > 0)
            {
                return $rule->getPlugin();
            }
        }       
        
        return null;
    }
    
    
}

?>
Return current item: ContentCMS