Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Description of CEntity
- *
- * @author Gabriel Carignano
- * @version 1.0
- * @package SocialNetwork
- *
- */
- abstract class CEntity
- {
- private static $_lastError;
- protected $_DATA;
- const TABLE = 'm_social_entity';
- /**
- * The constructur can recive 3 types or param.
- * 1- NULL : dont do anything.
- * 2- Integer: will try to load the entity information and then fill the
- * DATA array with the info.
- * 3- Array: will use the passed array to fill the DATA ARRAY
- *
- * The $loadExtended param tell wheter the class should load o not
- * the extended info of the entity based on the entity type
- *
- * @param Integer or Array $id
- * @param Bool $loadExtended
- */
- public function __construct($id = null)
- {
- $this->_DATA = null;
- if ($id) {
- if (is_array($id) && count($id) > 0) {
- $this->_buildFromArray($id);
- } else {
- $this->_DATA = self::getById($id);
- }
- }
- }
- /**
- * Fill the DATA atribute with the passed array.
- * @param array $data
- */
- protected function _buildFromArray(Array $data)
- {
- $this->_DATA = $data;
- if (!$this->_DATE['ENTITY_TYPE'] || !is_array($this->_DATE['ENTITY_TYPE']) || !count($this->_DATE['ENTITY_TYPE']) > 0) {
- $this->_DATA["ENTITY_TYPE"] = CEntityType::getById($this->_DATA["ENTITY_TYPE_ID"]);
- $this->_DATA["DEFINED_PARENT"] = $this->_DATA["ENTITY_TYPE"]["DEFINED_PARENT"];
- }
- }
- /**
- * Recives an EntityID and return a new CEntity Object
- *
- * @param Integer $entityId
- *
- * @return Extended EntityType Obj.
- */
- public static function createFromId($entityId)
- {
- if (self::_isValid() ) {
- if ($entityId > 0) {
- $class = static::CLASS_NAME;
- return new $class($entityId);
- }
- }
- return null;
- }
- /**
- * Recives an Array with the entity DATA and return a new CEntity Object
- *
- * @param array $data
- *
- * @return CEntity
- */
- public static function createFromArray(Array $data)
- {
- if ($data && is_array($data) && count($data) > 0 && $data['ID']) {
- $class = static::CLASS_NAME;
- return new $class($data);
- }
- return null;
- }
- /**
- * Will return a CDBResult element with a list of result entities matching
- * the passed filter if any.
- * Alse you need to specified the extenend table to join to
- *
- * @param String $table
- * @param Arrat $arrFilter
- *
- * @return CDBResult
- */
- public static function getList($arrFilter = null)
- {
- if (self::_isValid() ) {
- global $DB;
- if ($arrFilter) {
- $arr = $DB->buildSQLFilter($arrFilter, 0);
- $sqlProp2 = " AND (" . $arr["query"] . ") ";
- }
- $sqlString = "SELECT MSEE.*, MSE.* FROM " . static::TABLE . " MSEE
- INNER JOIN " . self::TABLE . " MSE ON
- MSEE.ENTITY_ID=MSE.ID WHERE 1 $sqlProp2";
- $rslt = $DB->query(array("QUERY" => $sqlString));
- if (!$rslt) {
- self::setError($DB->getError());
- return false;
- }
- }
- return $rslt;
- }
- /**
- * Will return an array with the fields of the entity and extended entity
- * matching with $entityId
- *
- *
- * @assert (13) == 1
- * @param int $entityId
- * @return Array | false
- */
- public static function getById($entityId)
- {
- if (self::_isValid() ) {
- if ($entityId > 0) {
- $rslt = self::getList(array("FIELD_ID" => $entityId));
- if ($rslt && $rslt->size() > 0) {
- $extended = $rslt->fetch();
- $extended["ENTITY_TYPE"] = CEntityType::getById($extended["ENTITY_TYPE_ID"]);
- $extended["DEFINED_PARENT"] = $extended["ENTITY_TYPE"]["DEFINED_PARENT"];
- return $extended;
- }
- self::setError($DB->getError());
- } else {
- self::setError("EntityID not specified");
- }
- }
- return null;
- }
- /**
- * Return the Entity data as an array
- * @return Array
- */
- public function toArray()
- {
- return $this->_DATA;
- }
- /*
- * Return the Entity data as an array
- *
- * @param Encoding Option. Default=null
- * Posible values: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS,
- * JSON_FORCE_OBJECT.
- *
- * @return json Obj
- */
- public function toJson( $option = null )
- {
- if ($this->_DATA && is_array($this->_DATA) && count($this->_DATA) > 0) {
- return json_encode($this->_DATA, $option);
- }
- return null;
- }
- /**
- *
- * Adds an entity to the database. If successful will return the newly
- * inserted entity, else returns false.
- *
- * <p>Structure of $arrFields</p>
- *
- * - $arrFields["ENTITY_TYPE_ID"] (int) - Id of the entity type - required
- * - $arrFields["OWNER_ID"] (int) - Id of the owner of the entity - required
- * - $arrFields["ACTIVE"] (char) - Y | N - required
- *
- * @global CDB
- * @param array $arrFields
- * @return int | false
- */
- public static function add($arrFields)
- {
- global $DB;
- $arrFields["~DATE_CREATED"] = "now()";
- $arrFields["~MODIFIED_BY_ID"] = $arrFields["OWNER_ID"];
- $arInsert = $DB->prepareInsert(self::TABLE, $arrFields);
- $sqlString = "INSERT INTO " . self::TABLE . " (" . $arInsert[0] . ") VALUES (" . $arInsert[1] . ")";
- $rslt = $DB->execute(array("QUERY" => $sqlString));
- if (!$rslt) {
- self::setError($DB->getError());
- return false;
- }
- $entityId = $DB->getLastInsertID();
- return $entityId;
- }
- /**
- *
- * Updates an entity on the database. If successful will return true,
- * else returns false.
- *
- * <p>Structure of $arrFields</p>
- *
- * - $arrFields["ENTITY_TYPE_ID"] (int) - Id of the entity type - optional
- * - $arrFields["ACTIVE"] (char) - Y | N - optional
- * - $arrFields["MODIFIED_BY_ID"] (int) - Id of the user updating the record - required
- *
- * @global CDB
- * $param int $entityId
- * @param array $arrFields
- * @return bool
- */
- public static function update($entityId, $arrFields)
- {
- global $DB;
- $arrFields["DATE_MODIFIED"] = date("d.m.Y h:i:s");
- if (is_numeric($entityId)) {
- $strUpdate = $DB->prepareUpdate(self::TABLE, $arrFields);
- $sqlString = "UPDATE " . self::TABLE . " SET " . $strUpdate . " WHERE ID=" . $DB->prepareSql($entityId);
- $rslt = $DB->execute(array("QUERY" => $sqlString));
- if ($rslt) {
- return true;
- }
- self::setError($DB->getError());
- }
- self::setError("Entity ID not set");
- return null;
- }
- /**
- *
- * Passing an entity id will delete the record, if successful will return true,
- * else returns false.
- *
- * @global CDB
- * @param int $entityId
- * @return bool
- */
- public static function delete($entityId)
- {
- global $DB;
- $strDelete = "DELETE FROM " . self::TABLE . " WHERE ID=" . $DB->prepareSql($entityId);
- $rslt = $DB->execute(array("QUERY" => $strDelete));
- if (!$rslt) {
- self::setError($DB->getError());
- return false;
- }
- return true;
- }
- /**
- * Will return an array with the fields of the entity matching with $entityId
- *
- * @assert (13) == 1
- * @param int $entityId
- * @return array | false
- */
- public static function getEntityById($entityId)
- {
- $entity = self::getEntityList(array("FIELD_ID" => $entityId));
- if (!$entity || $entity->size() == 0) {
- return false;
- }
- $entArr = $entity->fetch();
- $entArr["ENTITY_TYPE"] = CEntityType::getById($entArr["ENTITY_TYPE_ID"]);
- $entArr["DEFINED_PARENT"] = $entArr["ENTITY_TYPE"]["DEFINED_PARENT"];
- return $entArr;
- }
- /**
- * Will return a CDBResult element with a list of result entities matching
- * the passed filter if any.
- *
- * <p>Possible $arrFilter keys</p>
- *
- * - $arrFilter["FIELD_ID"]
- * - $arrFilter["FIELD_DATE_CREATED"]
- * - $arrFilter["FIELD_OWNER_ID"]
- * - $arrFilter["FIELD_DATE_MODIFIED"]
- * - $arrFilter["FIELD_MODIFIED_BY_ID"]
- * - $arrFilter["FIELD_ACTIVE"]
- * - $arrFilter["FIELD_ENTITY_TYPE_ID"]
- *
- *
- * @global CDB
- * @param array $arrFilter
- * @return CDBResult | false
- */
- public static function getEntityList($arrFilter = null)
- {
- global $DB;
- if ($arrFilter) {
- $arr = $DB->buildSQLFilter($arrFilter, 0);
- $sqlProp2 = " AND (" . $arr["query"] . ") ";
- }
- $sqlString = "SELECT * FROM " . self::TABLE . " WHERE 1 $sqlProp2";
- $rslt = $DB->query(array("QUERY" => $sqlString));
- if (!$rslt) {
- self::setError($DB->getError());
- return false;
- }
- return $rslt;
- }
- /**
- * Will set the current Entity object to Active=Y
- *
- * The Entity object should be instatiated and loaded from the DB
- * before this method can be used.
- *
- * @return bool
- */
- public function enable()
- {
- return $this->update($this->getId(), array("ACTIVE" => "Y"));
- }
- /**
- * Will set the current Entity object to Active=N
- *
- * The Entity object should be instatiated and loaded from the DB
- * before this method can be used.
- *
- * @return bool
- */
- public function disable()
- {
- return $this->update($this->getId(), array("ACTIVE" => "N"));
- }
- public function reload()
- {
- $this->__construt($this->_DATA['ID']);
- }
- //ERROR HANDLER
- /**
- * Chekcs if the method calling is valid.
- * This function is used to prevent direct method calling on CSEntity.
- *
- * @return Bool
- */
- private static function _isValid()
- {
- if (__CLASS__ == get_called_class()) {
- self::setError('This method can only be called from a extended class');
- return null;
- }
- return true;
- }
- /**
- * This function return the last error message generated.
- *
- * @return String
- */
- public static function getError()
- {
- return self::$_lastError;
- }
- /**
- * Set the last error message var
- *
- * @param String $errorMessage
- */
- public static function setError($errorMessage)
- {
- self::$_lastError = $errorMessage;
- }
- //MAGIC METHODS
- /**
- * Magic toString method. Used when the obj is treated as a regular var.
- */
- public function __toString()
- {
- new dBug($this->_DATA);
- //echo '<pre>' . print_r($this->_DATA) . '</pre>';
- }
- /**
- * Magic GETTER method
- */
- public function __get( $name )
- {
- return $this->_DATA[ $name ];
- }
- /**
- * Magic SETTER method
- */
- public function __set( $name, $value )
- {
- $this->_DATA[ $name ] = $value;
- }
- /**
- * Magic IS SET method
- */
- public function __isset( $name )
- {
- return isset( $this->_DATA[ $name ] );
- }
- /**
- * Magic UNSET method
- */
- public function __unset( $name )
- {
- unset( $this->_DATA[ $name ] );
- }
- }
Add Comment
Please, Sign In to add comment