Exemple #1
0
 /**
  * แทนที่ข้อความด้วยข้อมูลจากแอเรย์ รองรับข้อมูลรูปแบบแอเรย์ย่อยๆ
  *
  * @param string $source ข้อความต้นฉบับ
  * @param array $replace ข้อความที่จะนำมาแทนที่ รูปแบบ array($key1 => $value1, $key2 => $value2) ข้อความใน $source ที่ตรงกับ $key จะถูกแทนที่ด้วย $value
  * @return string
  *
  * @assert ("SELECT * FROM table WHERE id=:id AND lang IN (:lang, '')", array(':id' => 1, array(':lang' => 'th'))) [==] "SELECT * FROM table WHERE id=1 AND lang IN (th, '')"
  */
 public static function replace($source, $replace)
 {
     if (!empty($replace)) {
         $keys = array();
         $values = array();
         ArrayTool::extract($replace, $keys, $values);
         $source = str_replace($keys, $values, $source);
     }
     return $source;
 }
 /**
  * Gets current request complete url
  * @param boolean $includeGetParamList if GET params have to be added to URI
  * @return string The url
  */
 public static function getCurrentURL($includeGetParamList = true)
 {
     $url = 'http' . (!ArrayTool::array_key_exists('HTTPS', $_SERVER) || $_SERVER['HTTPS'] == 'off' ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
     // Gets only string before the "?" if get params are ignored
     if (!$includeGetParamList) {
         list($url) = StringTool::split('\\?', $url);
     }
     return $url;
 }
 /**
  * Checks if a property has changed since it's last update/add
  * @param string $propertyName The property name
  * @return boolean If the property has been updated or not
  */
 public function hasChangedProperty($propertyName)
 {
     return ArrayTool::array_key_exists($propertyName, $this->originalPropertyList);
 }
 /**
  * Adds element list to cache
  * @param string $elementClass The element type to add
  * @param array $elementList The element list to add
  * @param string $conditions The conditions string to apply
  * @param string $orderBy The order string to apply
  */
 public static function addElementList($elementClass, $elementList, $conditions = null, $orderBy = null)
 {
     if (!ArrayTool::array_key_exists($elementClass, self::$cachedElementArray)) {
         self::$cachedElementListArray[$elementClass] = array();
     }
     $elementIdList = array();
     foreach ($elementList as $element) {
         $elementIdList[] = $element->id;
     }
     self::$cachedElementListArray[$elementClass][self::getListIndex($conditions, $orderBy)] = $elementIdList;
     foreach ($elementList as $element) {
         // Caches current element
         self::addElement($element);
     }
 }
 /**
  * Checks if a session parameter is set
  * @param string $paramName The parameter name
  * @return boolean Returns true if the parameter is set, else returns false
  */
 public function issetParameter($paramName)
 {
     return ArrayTool::array_key_exists($paramName, $this->getParameterList());
 }
 /**
  * Gets all elements by type from database
  * @param string $elementClass The element type searched
  * @param string $conditions The conditions string to apply (ex : Region.x < 10 AND World.size = 500)
  * @param string $orderBy The order string to apply (ex : 'Region.x, City.name DESC')
  * @param string $join the joined tables
  * @return array The element list
  */
 public static function getElementList($elementClass, $conditions = NULL, $orderBy = NULL, $join = NULL)
 {
     $tableName = DatabaseFactory::getElementTableName($elementClass);
     if ($join !== NULL) {
         list($select, $from, $joinTableNameList, $joinTableAttachements) = DatabaseFactory::getJoin($tableName, $join);
         $request = 'SELECT ' . $select . $from;
     } else {
         $request = 'SELECT * FROM ' . $tableName;
     }
     if ($conditions !== NULL) {
         $request .= ' WHERE ' . $conditions;
     }
     if ($orderBy !== NULL) {
         $request .= ' ORDER BY ' . $orderBy;
     }
     $databaseConnection = $elementClass::getDatabaseConnection();
     // For join requests, needs to fetch results by index (to handle several fields with same name)
     if ($join === NULL) {
         // Execute request on database
         $resultType = MysqlDatabaseConnectionModel::ARRAY_TYPE_ASSOC;
         $resultList = $databaseConnection->selectRequest($request, $resultType, false, false);
     } else {
         // Gets field list with resut to get them for joined tables
         $resultType = MysqlDatabaseConnectionModel::ARRAY_TYPE_NUM;
         list($resultList, $fieldNameList) = $databaseConnection->selectRequest($request, $resultType, TRUE, FALSE);
         // Builds main element field list
         $fieldIndex = 0;
         $fieldNumber = count($fieldNameList);
         $elementFieldNameList = array();
         while ($fieldIndex < $fieldNumber) {
             $fieldName = $fieldNameList[$fieldIndex++];
             if (StringTool::endsWith($fieldName, DatabaseFactory::JOIN_TABLE_FIELD_SEPARATOR)) {
                 break;
             }
             $elementFieldNameList[$tableName][] = $fieldName;
         }
         // Builds joined elements field list
         foreach ($joinTableNameList as &$joinTableName) {
             while ($fieldIndex < $fieldNumber) {
                 $fieldName = $fieldNameList[$fieldIndex++];
                 if (StringTool::endsWith($fieldName, DatabaseFactory::JOIN_TABLE_FIELD_SEPARATOR)) {
                     break;
                 }
                 $elementFieldNameList[$joinTableName][] = $fieldName;
             }
         }
     }
     // Constructs typed elements from results
     $elementList = array();
     $retrievedElementList = array();
     while ($result = MysqlDatabaseConnectionModel::fetch_array($resultList, $resultType)) {
         $rowElementList = array();
         if ($join === NULL) {
             // Sets element
             $element = Element::getElementFromArray($elementClass, $result);
             $element->setId();
             $elementList[$element->id] = $element;
         } else {
             // Additionnal tables requested
             $fieldIndex = 0;
             $fieldValues = array_values($result);
             $rowElementList = array();
             // Sets elements attributes for each table on the row
             foreach ($elementFieldNameList as $elementTableName => $fieldNameList) {
                 $elementAttributeList = array();
                 foreach ($fieldNameList as $fieldName) {
                     $elementAttributeList[$fieldName] = $fieldValues[$fieldIndex++];
                 }
                 // Go next to avoid separator field
                 ++$fieldIndex;
                 // Sets element
                 $element = Element::getElementFromArray(DatabaseFactory::getElementClassName($elementTableName), $elementAttributeList);
                 $element->setId();
                 // Ignores empty LJ elements from "_has_" tables
                 if ($element->id == '-') {
                     continue;
                 }
                 // Adds it to element list if it is the main table
                 if ($tableName == $elementTableName && !ArrayTool::array_key_exists($element->id, $elementList)) {
                     $elementList[$element->id] = $element;
                 }
                 $rowElementList[$elementTableName] = $element;
             }
             // Attaches elements with each other
             foreach ($rowElementList as $rowElementTable => $rowElement) {
                 CacheFactory::addElement($rowElement);
                 // Checks if element has to be attached to another one
                 if (isset($joinTableAttachements[$rowElementTable])) {
                     foreach ($joinTableAttachements[$rowElementTable] as &$joinTableAttachement) {
                         if (isset($rowElementList[$joinTableAttachement])) {
                             if ($rowElementList[$joinTableAttachement]->getElementClass() != $rowElement->getElementClass() || $rowElementList[$joinTableAttachement]->id != $rowElement->id) {
                                 $retrievedElementList[$joinTableAttachement][$rowElementList[$joinTableAttachement]->id][$rowElementTable][$rowElement->id] = $rowElement;
                             }
                         }
                     }
                 }
             }
         }
     }
     MysqlDatabaseConnectionModel::free_result($resultList);
     return array($elementList, $retrievedElementList);
 }
 /**
  * Traces a log message
  * @param string $message The message
  * @param int $logLevel The log level
  * @color string $color The color of the message in the log
  */
 private function log($message, $logLevel = LOG_MIN_LEVEL, $color = NULL)
 {
     // Tests log level
     if (!IS_FILE_LOG_ENABLED || !$this->logFile) {
         return;
     }
     if ($this->detailedMode || $logLevel >= LOG_MIN_LEVEL && (!$this->silentMode || $logLevel >= LogTool::WARNING_LEVEL)) {
         $logHeaderList = array();
         // Sets headers
         // Log type
         if ($logLevel >= LogTool::ERROR_LEVEL) {
             $logHeaderList[LogTool::TYPE_FIELD] = 'ERROR';
         } else {
             if ($logLevel >= LogTool::WARNING_LEVEL) {
                 $logHeaderList[LogTool::TYPE_FIELD] = 'WARNING';
             } else {
                 if ($logLevel >= LogTool::NOTICE_LEVEL) {
                     $logHeaderList[LogTool::TYPE_FIELD] = 'NOTICE';
                 } else {
                     $logHeaderList[LogTool::TYPE_FIELD] = 'DEBUG';
                 }
             }
         }
         // Remote ip
         if (ArrayTool::array_key_exists('REMOTE_ADDR', $_SERVER)) {
             $logHeaderList[LogTool::REMOTE_IP_FIELD] = $_SERVER['REMOTE_ADDR'];
         } else {
             $logHeaderList[LogTool::REMOTE_IP_FIELD] = '';
         }
         // Current uri/script
         $logHeaderList[LogTool::URI_FIELD] = basename($_SERVER['PHP_SELF']);
         // Current time
         $logHeaderList[LogTool::DATE_FIELD] = DateTool::getTimeString(DateTool::FORMAT_TIME);
         // host
         $logHeaderList[LogTool::HOST_FIELD] = php_uname('n');
         // computes header
         $logHeader = '[' . $logHeaderList[LogTool::DATE_FIELD] . '] ' . $logHeaderList[LogTool::TYPE_FIELD] . ' [' . ($logHeaderList[LogTool::REMOTE_IP_FIELD] ? $logHeaderList[LogTool::REMOTE_IP_FIELD] . ' > ' : '') . $logHeaderList[LogTool::HOST_FIELD] . '] ' . $logHeaderList[LogTool::URI_FIELD];
         // computes log line
         $logLine = $color . $logHeader . ': ' . str_replace(PHP_EOL, PHP_EOL . $logHeader . ': ', $message) . PHP_EOL;
         // Opens new log file if date has changed
         if (DateTool::getTimeString(DateTool::FORMAT_MYSQL_DATE) != $this->currentLogDate) {
             $this->closeLogFile();
             $this->openLogFile();
         }
         // Finally write the line in log file
         fwrite($this->logFile, $logLine);
     }
 }
Exemple #8
0
 public static function convertSetToString($data, $pattern, $keyField = null)
 {
     $result = array();
     if (is_string($pattern)) {
         foreach ($data as $item) {
             if ($keyField) {
                 $key = is_object($item) ? $item->{$keyField} : $item[$keyField];
                 $result[$key] = ArrayTool::convertToString($item, $pattern);
             } else {
                 $result[] = ArrayTool::convertToString($item, $pattern);
             }
         }
     }
     return $result;
 }