From 541bb378ddece2eab135a8066a16994e94436dea Mon Sep 17 00:00:00 2001 From: Giulio Cesare Solaroli Date: Mon, 03 Oct 2011 16:04:12 +0000 Subject: Merge pull request #1 from gcsolaroli/master First version of the restructured repository --- (limited to 'backend/php/src/objects/class.pog_base.php') diff --git a/backend/php/src/objects/class.pog_base.php b/backend/php/src/objects/class.pog_base.php new file mode 100644 index 0000000..6a8f570 --- a/dev/null +++ b/backend/php/src/objects/class.pog_base.php @@ -0,0 +1,143 @@ +Execute(); + } + + /** + * constructor + * + * @return POG_Base + */ + private function POG_Base() + { + } + + + function SetFieldAttribute($fieldName, $attributeName, $attributeValue) + { + if (isset($this->pog_attribute_type[$fieldName]) && isset($this->pog_attribute_type[$fieldName][$attributeName])) + { + $this->pog_attribute_type[$fieldName][$attributeName] = $attributeValue; + } + } + + function GetFieldAttribute($fieldName, $attributeName) + { + if (isset($this->pog_attribute_type[$fieldName]) && isset($this->pog_attribute_type[$fieldName][$attributeName])) + { + return $this->pog_attribute_type[$fieldName][$attributeName]; + } + return null; + } + + /////////////////////////// + // Data manipulation + /////////////////////////// + + /** + * This function will try to encode $text to base64, except when $text is a number. This allows us to Escape all data before they're inserted in the database, regardless of attribute type. + * @param string $text + * @return string encoded to base64 + */ + public function Escape($text) + { + if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text)) + { + return base64_encode($text); + } + return addslashes($text); + } + + /** + * Enter description here... + * + * @param unknown_type $text + * @return unknown + */ + public function Unescape($text) + { + if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text)) + { + return base64_decode($text); + } + return stripcslashes($text); + } + + + //////////////////////////////// + // Table -> Object Mapping + //////////////////////////////// + + /** + * Executes $query against database and returns the result set as an array of POG objects + * + * @param string $query. SQL query to execute against database + * @param string $objectClass. POG Object type to return + * @param bool $lazy. If true, will also load all children/sibling + */ + public function FetchObjects($query, $objectClass, $lazy = true) + { + $databaseConnection = Database::Connect(); + $result = Database::Query($query, $databaseConnection); + $objectList = $this->CreateObjects($result, $objectClass, $lazy); + return $objectList; + } + + private function CreateObjects($mysql_result, $objectClass, $lazyLoad = true) + { + $objectList = array(); + while ($row = mysql_fetch_assoc($mysql_result)) + { + $pog_object = new $objectClass(); + $this->PopulateObjectAttributes($row, $pog_object); + $objectList[] = $pog_object; + } + return $objectList; + } + + private function PopulateObjectAttributes($fetched_row, $pog_object) + { + foreach ($this->GetAttributes($pog_object) as $column) + { + $pog_object->{$column} = $this->Unescape($fetched_row[strtolower($column)]); + } + return $pog_object; + } + + private function GetAttributes($object) + { + $columns = array(); + foreach ($object->pog_attribute_type as $att => $properties) + { + if ($properties['db_attributes'][0] != 'OBJECT') + { + $columns[] = $att; + } + } + return $columns; + } + + //misc + public static function IsColumn($value) + { + if (strlen($value) > 2) + { + if (substr($value, 0, 1) == '`' && substr($value, strlen($value) - 1, 1) == '`') + { + return true; + } + return false; + } + return false; + } +} +?> \ No newline at end of file -- cgit v0.9.0.2