Skip to content
Snippets Groups Projects
ObjectControlerAbstract.php 14.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • SNA's avatar
    SNA committed
    <?php
    
    define ("_DEBUG", false);
    define ("_ADVANCED_DEBUG",false);
    
    /**
     * Implementing few generic features for controlers of type
     * "all-the-properties-of-the-object-are-the-columns-of-the-
     * database-table", i.e. BaseObject-kind.
    
    SNA's avatar
    SNA committed
     *
    
    SNA's avatar
    SNA committed
     * @author boulio
     *
     */
    
    abstract class ObjectControler
    {
    
    SNA's avatar
    SNA committed
        static protected $db;
    
        static protected $computed_properties = array();
        static protected $foolish_ids = array();
    
    SNA's avatar
    SNA committed
        static protected $specific_id ;
    
        protected function set_foolish_ids($array)
        {
    
            if (isset($array) && is_array($array)){
    
    SNA's avatar
    SNA committed
                self::$foolish_ids = $array;
    
    SNA's avatar
    SNA committed
        }
    
        protected function set_specific_id($id)
        {
    
    SNA's avatar
    SNA committed
        }
    
        /**
         * Insert given object in given table.
         * Return inserted object if succeeded.
         * @param unknown_type $object
         * @return unknown_type
         */
    
        protected function advanced_insert($object)
        {
            $tableName = get_class($object);
            if (!isset($object) ) {
    
    SNA's avatar
    SNA committed
                return false;
    
    SNA's avatar
    SNA committed
    
            // Inserting object
    
            $preparation = self::insert_prepare(
                $object, self::$computed_properties
            );
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            
    
            $query = "insert into $tableName (" . $preparation['properties']
                   . ") values(" . $preparation['values'] . ")";
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            self::$db = new Database();
            
            $stmt = self::$db->query($query, $preparation['arrayValues']);
            
            if ($stmt) {
    
                $result = true;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            } else {
    
                $result = false;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            }    
            
    
    SNA's avatar
    SNA committed
            return $result;
        }
    
        /**
         * Prepare two strings for insert query :
         * - 'properties' for properties field of insert query,
         * - 'values' for values field of insert query.
         * Needs list of values to _exclude_ of insert query (i.e.
         * usually values computed in the get() function of controler).
         * Result in an array.
         * @param Any $object
         * @param string[] $computed_properties
         * @return string[]
         */
    
        protected function insert_prepare($object, $computed_properties)
        {
            $result = array();
            $properties = array();
            $values = array();
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $arrayValues = array();
    
            foreach ($object->getArray() as $key => $value) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                if(!in_array($key,$computed_properties)) {
    
    SNA's avatar
    SNA committed
                    // Adding property
    
                    $properties[] = $key;
    
    SNA's avatar
    SNA committed
                    // Adding property value
    
                    if (substr_compare($key, '_id', -3) == 0
                        || substr_compare($key, '_number', -7) == 0) {
                        if (in_array($key, self::$foolish_ids)) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            //$values[] = "'" . $value . "'";
    
    SNA's avatar
    SNA committed
                        } else {
                            // Number
    
                            if (empty($value)) {
    
    SNA's avatar
    SNA committed
                                // Default value
    
                                $value = 0;
    
    SNA's avatar
    SNA committed
                            }
                        }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        $arrayValues[] = $value;
                        $values[] = '?';
    
                    } elseif(substr_compare($key, "is_", 0, 3) == 0
                        || substr_compare($key, "can_", 0, 4) == 0) {
    
    SNA's avatar
    SNA committed
                        // Boolean
    
                        if ($value === true) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $boolValue = "Y";
    
                        } elseif ($value === false) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $boolValue = "N";
    
                        } else {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $boolValue = $value;
    
    SNA's avatar
    SNA committed
                        }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        $values[] = '?';
                        $arrayValues[] = $boolValue;
    
    SNA's avatar
    SNA committed
                    } else {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        if (
                            $value == 'CURRENT_TIMESTAMP'
                            || $value == 'SYSDATE'
                        ) {
                            $values[] = $value;
                        } else {
                            $values[] = '?';
                            $arrayValues[] = $value;
                        }
    
    SNA's avatar
    SNA committed
                    }
                }
            }
    
            $result['properties'] = implode(",", $properties);
            $result['values'] = implode(",", $values);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $result['arrayValues'] = $arrayValues;
    
    SNA's avatar
    SNA committed
            return $result;
        }
    
        /**
         * Update given object in given table, according
         * with given table id name.
         * Return updated object if succeeded.
         * @param unknown_type $object
         * @return unknown_type
         */
    
        protected function advanced_update($object)
        {
            if (!isset($object)){
    
    SNA's avatar
    SNA committed
                return false;
    
            }
            $tableName = get_class($object);
            $table_id = $tableName .'_id';
    
    SNA's avatar
    SNA committed
    
    
            if (isset(self::$specific_id) && !empty(self::$specific_id)){
    
    SNA's avatar
    SNA committed
                $table_id = self::$specific_id;
    
    SNA's avatar
    SNA committed
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $prep_query = self::update_prepare($object, self::$computed_properties);
    
    
            $prep_query['arrayValues'][] = $object->{$table_id};
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
            $query = "update $tableName set "
                   . $prep_query['query']
                   . " where $table_id=?";
    
            self::$db = new Database();        
            $stmt = self::$db->query($query, $prep_query['arrayValues']);
            
            if ($stmt) {
    
                $result = true;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            } else {
    
                $result = false;
    
    SNA's avatar
    SNA committed
            return $result;
        }
    
        /**
         * Prepare string for update query
         * @param Any $object
         * @param string[] $computed_properties
         * @return String
         */
    
        private function update_prepare($object, $computed_properties)
        {
            $result = array();
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $arrayValues=array();
    
            foreach ($object->getArray() as $key => $value) {
                if (!in_array($key,$computed_properties)) {
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
                    if($key == self::$specific_id) {
                        // do not update key
                    } elseif (substr_compare($key, '_id', -3) == 0
    
                        || substr_compare($key, '_number', -7) == 0) {
                        if (in_array($key, self::$foolish_ids)) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            //$result[] = $key . "='" . $value . "'";
    
    SNA's avatar
    SNA committed
                        } else {
                            // Number
    
                            if (empty($value)) {
    
    SNA's avatar
    SNA committed
                                // Default value
    
                                $value = 0;
    
    SNA's avatar
    SNA committed
                            }
                        }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        $result[] = $key . "=?";
                        $arrayValues[]=$value;
    
                    } elseif (substr_compare($key, 'is_', 0, 3) == 0
                        || substr_compare($key, 'can_', 0, 4) == 0) {
    
    SNA's avatar
    SNA committed
                        // Boolean
    
                        if ($value === true) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $boolValue = "Y";
    
                        } elseif ($value === false) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $boolValue = "N";
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        } else {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $boolValue = $value;
    
    SNA's avatar
    SNA committed
                        }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        $result[] = $key . "=?";
                        $arrayValues[] = $boolValue;
    
    SNA's avatar
    SNA committed
                    } else {
                        // Character or date
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        if (
                            $value == 'CURRENT_TIMESTAMP'
                            || $value == 'SYSDATE'
                        ) {
                        $result[] = $key . "=" . $value;
                        } else {
                            $result[] = $key . "=?";
                            $arrayValues[] = $value;
                        }
    
    SNA's avatar
    SNA committed
                    }
                }
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $theResult['query'] = implode(",", $result);
            $theResult['arrayValues'] = $arrayValues;
            return $theResult;
    
    SNA's avatar
    SNA committed
        }
    
        /**
         * Get object of given class with given id from
         * good table and according with given class name.
         * Can return null if no corresponding object.
         * @param long $id Id of object to get
         * @param string $class_name
         * @return unknown_type
         */
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        protected function advanced_get($id, $table_name)
    
        {
            if (strlen($id) == 0) {
    
    SNA's avatar
    SNA committed
                return null;
            }
    
            $object_name = $table_name;
            $table_id = $table_name . '_id';
    
    SNA's avatar
    SNA committed
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            if(isset(self::$specific_id) && !empty(self::$specific_id)) {
    
    SNA's avatar
    SNA committed
                $table_id = self::$specific_id;
    
    SNA's avatar
    SNA committed
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            self::$db = new Database();
            
            $select = "select * from $table_name where $table_id=?";
    
            $stmt = self::$db->query($select, array($id));
            if ($stmt->rowCount() == 0) {
                return null;
            } else {
                // Constructing result
                $object = new $object_name();
                $queryResult = $stmt->fetchObject();
                foreach ((array)$queryResult as $key => $value) {
                    if ($value == 't') {          /* BUG FROM PGSQL DRIVER! */
                        $value = true;            /*                        */
                    } elseif ($value == 'f') {    /*                        */
                        $value = false;           /*                        */
                    }                            /**************************/
    
                    $object->{$key} = $value;
    
    SNA's avatar
    SNA committed
                }
            }
    
            return $object;
        }
    
    
        /**
         * Get object of given class with given id from
         * good table and according with given class name.
         * Can return null if no corresponding object.
         * @param long $id Id of object to get
         * @param string $class_name
         * @return unknown_type
         */
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        protected function advanced_getWithComp($id, $table_name, $whereComp='', $params=array())
    
        {
            if (strlen($id) == 0) {
                return null;
            }
    
            $object_name = $table_name;
            $table_id = $table_name . '_id';
    
            if(isset(self::$specific_id) && !empty(self::$specific_id)) {
                $table_id = self::$specific_id;
            }
    
            $database = new Database();
    
            if($table_name == 'users'){ 
                $theQuery = "SELECT * FROM $table_name WHERE upper($table_id) = upper(:id) " . $whereComp;
            }else{
                $theQuery = "SELECT * FROM $table_name WHERE $table_id = :id " . $whereComp;
            }
            //$theQuery = "SELECT * FROM $table_name WHERE $table_id = :id " . $whereComp;
    
            $queryParams = array(':id' => $id);
    
    Damien's avatar
    Damien committed
            if (!empty($params) && is_array($params) && count($params) > 0) {
    
                foreach ($params as $keyParam => $keyValue) {
    
                    $queryParams[":" . $keyParam] = $keyValue;
    
    
            $stmt = $database->query($theQuery, $queryParams);
    
                return null;
            } else {
                // Constructing result
                $object = new $object_name();
    
                $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);    
    
                
                for ($cpt=0;$cpt<count($rows);$cpt++) {
                    foreach ($rows[$cpt] as $key => $value) {
                        if ($value == 't') {          /* BUG FROM PGSQL DRIVER! */
                            $value = true;            /*                        */
                        } elseif ($value == 'f') {    /*                        */
                            $value = false;           /*                        */
                        }                            /**************************/
    
                        $object->{$key} = $value;
    
    SNA's avatar
    SNA committed
         * Delete given object from given table, according with
         * given table id name.
         * Return true if succeeded.
         * @param Any $object
         * @return boolean
         */
    
        protected function advanced_delete($object)
        {
            if (!isset($object)){
    
    SNA's avatar
    SNA committed
                return false;
    
            }
            $table_name = get_class($object);
            $table_id = $table_name . '_id';
    
    SNA's avatar
    SNA committed
    
    
            if (isset(self::$specific_id) && !empty(self::$specific_id)) {
    
    SNA's avatar
    SNA committed
                $table_id = self::$specific_id;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            self::$db = new Database();
        
            $query = "delete from $table_name where $table_id=?";
        
    
            $stmt = self::$db->query($query, array($object->{$table_id}));
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            
            if ($stmt) {
    
                $result = true;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            } else {
    
                $result = false;
    
    SNA's avatar
    SNA committed
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
    SNA's avatar
    SNA committed
            return $result;
        }
    
        /**
         * Enable given object from given table, according with
         * given table id name.
         * Return true if succeeded.
         * @param Any $object
         * @return boolean
         */
    
        protected function advanced_enable($object)
        {
            if (!isset($object)) {
    
    SNA's avatar
    SNA committed
                return false;
    
            }
            $table_name = get_class($object);
            $table_id = $table_name . '_id';
    
    SNA's avatar
    SNA committed
    
    
            if (isset(self::$specific_id) && !empty(self::$specific_id)) {
    
    SNA's avatar
    SNA committed
                $table_id = self::$specific_id;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            self::$db = new Database();
            
            $query="update $table_name set enabled = 'Y' where $table_id=?";
            
    
            $stmt = self::$db->query($query, array($object->{$table_id}));
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            
            if ($stmt) {
    
                $result = true;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            } else {
    
                $result = false;
    
    SNA's avatar
    SNA committed
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            
    
    SNA's avatar
    SNA committed
            return $result;
        }
    
    
        /**
         * Reactivate given object from given table, according with
         * given table id name.
         * Return true if succeeded.
         * @param Any $object
         * @return boolean
         */
        protected function advanced_reactivate($object)
        {
            if (!isset($object)) {
                return false;
            }
            $table_name = get_class($object);
    
            if (isset(self::$specific_id) && !empty(self::$specific_id)) {
                $table_id = self::$specific_id;
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            self::$db = new Database();
            
    
            $query="update $table_name set status = 'OK' where user_id = lower(?)";
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    
    
            $stmt = self::$db->query($query, array($object->{$table_id}));
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            
            if ($stmt) {
    
                $result = true;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            } else {
    
                $result = false;
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
    SNA's avatar
    SNA committed
        /**
         * Disable given object from given table, according with
         * given table id name.
         * Return true if succeeded.
         * @param Any $object
         * @return boolean
         */
    
        protected function advanced_disable($object)
        {
            if (!isset($object)) {
    
    SNA's avatar
    SNA committed
                return false;
    
            }
            $table_name = get_class($object);
    
    SNA's avatar
    SNA committed
            $table_id=$table_name."_id";
    
    
            if (isset(self::$specific_id) && !empty(self::$specific_id)) {
    
    SNA's avatar
    SNA committed
                $table_id = self::$specific_id;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            self::$db = new Database();
            
            $query = "update $table_name set enabled = 'N' where $table_id=?";
            
    
            $stmt = self::$db->query($query, array($object->{$table_id}));
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            
            if ($stmt) {
    
                $result = true;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            } else {
    
                $result = false;
    
    SNA's avatar
    SNA committed
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
    SNA's avatar
    SNA committed
            return $result;
        }
    
    SNA's avatar
    SNA committed
    }