Guest User

Untitled

a guest
Oct 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.78 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Description of CEntity
  5.  *
  6.  * @author Gabriel Carignano
  7.  * @version 1.0
  8.  * @package SocialNetwork
  9.  *
  10.  */
  11. abstract class CEntity
  12. {
  13.     private static $_lastError;
  14.    
  15.     protected $_DATA;
  16.    
  17.     const TABLE = 'm_social_entity';
  18.    
  19.     /**
  20.      * The constructur can recive 3 types or param.
  21.      * 1- NULL : dont do anything.
  22.      * 2- Integer: will try to load the entity information and then fill the
  23.      * DATA array with the info.
  24.      * 3- Array: will use the passed array to fill the DATA ARRAY
  25.      *
  26.      * The $loadExtended param tell wheter the class should load o not
  27.      * the extended info of the entity based on the entity type
  28.      *
  29.      * @param Integer or Array $id
  30.      * @param Bool $loadExtended
  31.      */
  32.     public function __construct($id = null)
  33.     {
  34.         $this->_DATA = null;
  35.         if ($id) {
  36.             if (is_array($id) && count($id) > 0) {
  37.                 $this->_buildFromArray($id);
  38.             } else {
  39.                 $this->_DATA = self::getById($id);    
  40.             }
  41.            
  42.         }
  43.     }
  44.    
  45.     /**
  46.      * Fill the DATA atribute with the passed array.
  47.      * @param array $data
  48.      */
  49.     protected function _buildFromArray(Array $data)
  50.     {
  51.         $this->_DATA = $data;
  52.         if (!$this->_DATE['ENTITY_TYPE'] || !is_array($this->_DATE['ENTITY_TYPE']) || !count($this->_DATE['ENTITY_TYPE']) > 0) {
  53.             $this->_DATA["ENTITY_TYPE"] = CEntityType::getById($this->_DATA["ENTITY_TYPE_ID"]);
  54.             $this->_DATA["DEFINED_PARENT"] = $this->_DATA["ENTITY_TYPE"]["DEFINED_PARENT"];                                    
  55.         }
  56.     }
  57.    
  58.     /**
  59.      * Recives an EntityID and return a new CEntity Object
  60.      *
  61.      * @param Integer $entityId
  62.      *
  63.      * @return Extended EntityType Obj.
  64.      */
  65.     public static function createFromId($entityId)
  66.     {
  67.         if (self::_isValid() ) {
  68.             if ($entityId > 0) {
  69.                 $class = static::CLASS_NAME;    
  70.                 return new $class($entityId);            
  71.             }            
  72.         }        
  73.         return null;
  74.     }    
  75.    
  76.    /**
  77.      * Recives an Array with the entity DATA and return a new CEntity Object
  78.      *
  79.      * @param array $data
  80.      *
  81.      * @return CEntity
  82.      */
  83.     public static function createFromArray(Array $data)
  84.     {
  85.         if ($data && is_array($data) && count($data) > 0 && $data['ID']) {
  86.             $class = static::CLASS_NAME;
  87.             return new $class($data);
  88.         }
  89.         return null;
  90.     }    
  91.    
  92.     /**
  93.      * Will return a CDBResult element with a list of result entities matching
  94.      * the passed filter if any.
  95.      * Alse you need to specified the extenend table to join to    
  96.      *
  97.      * @param String $table
  98.      * @param Arrat $arrFilter
  99.      *
  100.      * @return CDBResult
  101.      */
  102.     public static function getList($arrFilter = null)
  103.     {
  104.         if (self::_isValid() ) {
  105.             global $DB;
  106.             if ($arrFilter) {
  107.                 $arr = $DB->buildSQLFilter($arrFilter, 0);
  108.                 $sqlProp2 = " AND (" . $arr["query"] . ") ";
  109.             }
  110.             $sqlString = "SELECT MSEE.*, MSE.* FROM " . static::TABLE . " MSEE
  111.                INNER JOIN " . self::TABLE . " MSE ON
  112.                MSEE.ENTITY_ID=MSE.ID WHERE 1 $sqlProp2";
  113.             $rslt = $DB->query(array("QUERY" => $sqlString));
  114.             if (!$rslt) {
  115.                 self::setError($DB->getError());
  116.                 return false;
  117.             }
  118.         }        
  119.         return $rslt;
  120.     }    
  121.    
  122.     /**
  123.      * Will return an array with the fields of the entity and extended entity
  124.      *  matching with $entityId
  125.      *
  126.      *
  127.      * @assert (13) == 1
  128.      * @param int $entityId
  129.      * @return Array | false
  130.      */    
  131.     public static function getById($entityId)
  132.     {
  133.         if (self::_isValid() ) {
  134.             if ($entityId > 0) {
  135.                 $rslt = self::getList(array("FIELD_ID" => $entityId));
  136.                 if ($rslt && $rslt->size() > 0) {
  137.                     $extended = $rslt->fetch();
  138.                     $extended["ENTITY_TYPE"] = CEntityType::getById($extended["ENTITY_TYPE_ID"]);
  139.                     $extended["DEFINED_PARENT"] = $extended["ENTITY_TYPE"]["DEFINED_PARENT"];                        
  140.                     return $extended;
  141.                 }
  142.                 self::setError($DB->getError());
  143.             } else {
  144.                 self::setError("EntityID not specified");
  145.             }
  146.         }        
  147.         return null;      
  148.     }    
  149.    
  150.     /**
  151.      * Return the Entity data as an array
  152.      * @return Array
  153.      */
  154.     public function toArray()
  155.     {
  156.         return $this->_DATA;
  157.     }
  158.    
  159.     /*
  160.      * Return the Entity data as an array
  161.      *
  162.      * @param Encoding Option.  Default=null
  163.      * Posible values: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS,
  164.      * JSON_FORCE_OBJECT.
  165.      *
  166.      * @return json Obj
  167.      */
  168.     public function toJson( $option = null )
  169.     {
  170.         if ($this->_DATA && is_array($this->_DATA) && count($this->_DATA) > 0) {
  171.             return json_encode($this->_DATA, $option);
  172.         }
  173.         return null;
  174.     }    
  175.    
  176.     /**
  177.      *
  178.      * Adds an entity to the database. If successful will return the newly
  179.      * inserted entity, else returns false.
  180.      *
  181.      * <p>Structure of $arrFields</p>
  182.      *
  183.      * - $arrFields["ENTITY_TYPE_ID"] (int) - Id of the entity type - required
  184.      * - $arrFields["OWNER_ID"] (int) - Id of the owner of the entity - required
  185.      * - $arrFields["ACTIVE"] (char) - Y | N - required
  186.      *
  187.      * @global CDB
  188.      * @param array $arrFields
  189.      * @return int | false
  190.      */
  191.     public static function add($arrFields)
  192.     {
  193.         global $DB;
  194.         $arrFields["~DATE_CREATED"] = "now()";
  195.         $arrFields["~MODIFIED_BY_ID"] = $arrFields["OWNER_ID"];
  196.         $arInsert = $DB->prepareInsert(self::TABLE, $arrFields);
  197.         $sqlString = "INSERT INTO " . self::TABLE . " (" . $arInsert[0] . ") VALUES (" . $arInsert[1] . ")";
  198.         $rslt = $DB->execute(array("QUERY" => $sqlString));
  199.         if (!$rslt) {
  200.             self::setError($DB->getError());
  201.             return false;
  202.         }
  203.         $entityId = $DB->getLastInsertID();
  204.         return $entityId;
  205.     }
  206.  
  207.     /**
  208.      *
  209.      * Updates an entity on the database. If successful will return true,
  210.      * else returns false.
  211.      *
  212.      * <p>Structure of $arrFields</p>
  213.      *
  214.      * - $arrFields["ENTITY_TYPE_ID"] (int) - Id of the entity type - optional
  215.      * - $arrFields["ACTIVE"] (char) - Y | N - optional
  216.      * - $arrFields["MODIFIED_BY_ID"] (int) - Id of the user updating the record - required
  217.      *
  218.      * @global CDB
  219.      * $param int $entityId
  220.      * @param array $arrFields
  221.      * @return bool
  222.      */
  223.     public static function update($entityId, $arrFields)
  224.     {
  225.         global $DB;
  226.         $arrFields["DATE_MODIFIED"] = date("d.m.Y h:i:s");
  227.         if (is_numeric($entityId)) {
  228.             $strUpdate = $DB->prepareUpdate(self::TABLE, $arrFields);
  229.             $sqlString = "UPDATE " . self::TABLE . " SET " . $strUpdate . " WHERE ID=" . $DB->prepareSql($entityId);
  230.             $rslt = $DB->execute(array("QUERY" => $sqlString));
  231.             if ($rslt) {
  232.                 return true;
  233.             }
  234.             self::setError($DB->getError());
  235.         }
  236.         self::setError("Entity ID not set");
  237.         return null;
  238.     }
  239.  
  240.     /**
  241.      *
  242.      * Passing an entity id will delete the record, if successful will return true,
  243.      * else returns false.
  244.      *
  245.      * @global CDB
  246.      * @param int $entityId
  247.      * @return bool
  248.      */
  249.     public static function delete($entityId)
  250.     {
  251.         global $DB;
  252.         $strDelete = "DELETE FROM " . self::TABLE . " WHERE ID=" . $DB->prepareSql($entityId);
  253.         $rslt = $DB->execute(array("QUERY" => $strDelete));
  254.         if (!$rslt) {
  255.             self::setError($DB->getError());
  256.             return false;
  257.         }
  258.         return true;
  259.     }
  260.  
  261.     /**
  262.      * Will return an array with the fields of the entity matching with $entityId
  263.      *
  264.      * @assert (13) == 1
  265.      * @param int $entityId
  266.      * @return array | false
  267.      */
  268.     public static function getEntityById($entityId)
  269.     {
  270.         $entity = self::getEntityList(array("FIELD_ID" => $entityId));
  271.         if (!$entity || $entity->size() == 0) {
  272.             return false;
  273.         }
  274.         $entArr = $entity->fetch();
  275.         $entArr["ENTITY_TYPE"] = CEntityType::getById($entArr["ENTITY_TYPE_ID"]);
  276.         $entArr["DEFINED_PARENT"] = $entArr["ENTITY_TYPE"]["DEFINED_PARENT"];
  277.         return $entArr;
  278.     }    
  279.    
  280.     /**
  281.      * Will return a CDBResult element with a list of result entities matching
  282.      * the passed filter if any.
  283.      *
  284.      * <p>Possible $arrFilter keys</p>
  285.      *
  286.      * - $arrFilter["FIELD_ID"]
  287.      * - $arrFilter["FIELD_DATE_CREATED"]
  288.      * - $arrFilter["FIELD_OWNER_ID"]
  289.      * - $arrFilter["FIELD_DATE_MODIFIED"]
  290.      * - $arrFilter["FIELD_MODIFIED_BY_ID"]
  291.      * - $arrFilter["FIELD_ACTIVE"]
  292.      * - $arrFilter["FIELD_ENTITY_TYPE_ID"]
  293.      *
  294.      *
  295.      * @global CDB
  296.      * @param array $arrFilter
  297.      * @return CDBResult | false
  298.      */
  299.     public static function getEntityList($arrFilter = null)
  300.     {
  301.         global $DB;
  302.         if ($arrFilter) {
  303.             $arr = $DB->buildSQLFilter($arrFilter, 0);
  304.             $sqlProp2 = " AND (" . $arr["query"] . ") ";
  305.         }
  306.         $sqlString = "SELECT * FROM " . self::TABLE . " WHERE 1 $sqlProp2";
  307.         $rslt = $DB->query(array("QUERY" => $sqlString));
  308.         if (!$rslt) {
  309.             self::setError($DB->getError());
  310.             return false;
  311.         }
  312.         return $rslt;
  313.     }
  314.  
  315.     /**
  316.      * Will set the current Entity object to Active=Y
  317.      *
  318.      * The Entity object should be instatiated and loaded from the DB
  319.      * before this method can be used.
  320.      *
  321.      * @return bool
  322.      */
  323.     public function enable()
  324.     {
  325.         return $this->update($this->getId(), array("ACTIVE" => "Y"));
  326.     }
  327.  
  328.     /**
  329.      * Will set the current Entity object to Active=N
  330.      *
  331.      * The Entity object should be instatiated and loaded from the DB
  332.      * before this method can be used.
  333.      *
  334.      * @return bool
  335.      */
  336.     public function disable()
  337.     {
  338.         return $this->update($this->getId(), array("ACTIVE" => "N"));
  339.     }
  340.    
  341.     public function reload()
  342.     {
  343.         $this->__construt($this->_DATA['ID']);
  344.     }
  345.    
  346.     //ERROR HANDLER
  347.    
  348.     /**
  349.      * Chekcs if the method calling is valid.
  350.      * This function is used to prevent direct method calling on CSEntity.
  351.      *
  352.      * @return Bool
  353.      */
  354.     private static function _isValid()
  355.     {
  356.         if (__CLASS__ == get_called_class()) {
  357.            self::setError('This method can only be called from a extended class');
  358.            return null;
  359.         }
  360.         return true;
  361.     }
  362.    
  363.     /**
  364.      * This function return the last error message generated.
  365.      *
  366.      * @return String
  367.      */
  368.     public static function getError()
  369.     {
  370.         return self::$_lastError;
  371.     }
  372.    
  373.     /**
  374.      * Set the last error message var
  375.      *
  376.      * @param String $errorMessage
  377.      */
  378.     public static function setError($errorMessage)
  379.     {
  380.         self::$_lastError = $errorMessage;
  381.     }    
  382.    
  383.     //MAGIC METHODS
  384.     /**
  385.      * Magic toString method. Used when the obj is treated as a regular var.
  386.      */    
  387.     public function __toString()
  388.     {
  389.         new dBug($this->_DATA);
  390.         //echo '<pre>' . print_r($this->_DATA) . '</pre>';
  391.     }
  392.    
  393.     /**
  394.      * Magic GETTER method
  395.      */
  396.     public function __get( $name )
  397.     {
  398.         return $this->_DATA[ $name ];
  399.     }
  400.  
  401.     /**
  402.      * Magic SETTER method
  403.      */    
  404.     public function __set( $name, $value )
  405.     {
  406.         $this->_DATA[ $name ] = $value;
  407.     }
  408.  
  409.     /**
  410.      * Magic IS SET method
  411.      */    
  412.     public function  __isset( $name )
  413.     {
  414.        return isset( $this->_DATA[ $name ] );
  415.     }
  416.  
  417.     /**
  418.      * Magic UNSET method
  419.      */    
  420.     public function  __unset( $name )
  421.     {
  422.       unset( $this->_DATA[ $name ] );
  423.     }  
  424. }
Add Comment
Please, Sign In to add comment