/** * Convert a color string from hexadecimal to RGB format * * @param string $color an hexadecimal color (#FFFFFF, #FFF, AAAAAA are available formats) * @return array|boolean an array with r,g,b result, or false if color format was not correct */ public static function hexaToRGB($color) { if ($color[0] == '#') { $color = StringTool::substr($color, 1); } if (StringTool::strlen($color) == 6) { list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); } elseif (StringTool::strlen($color) == 3) { list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); } else { return false; } $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); return array($r, $g, $b); }
/** * Returns request parameter value * @param string $paramName The parameter name * @param string $type The parameter type (RequestTool::PARAM_TYPE_INT, RequestTool::PARAM_TYPE_STRING, etc) * @param boolean $isMandatory Specifies if the parameter is mandatory (raises an exception if not set) * @param string $defaultValue The default value returned if parameter is not set and not mandatory * @param string $method The method used to pass parameters in the request (GET or POST) * @return string The parameter value */ public static function &getParameter($paramName, $type, $isMandatory = true, $defaultValue = NULL, $method = RequestTool::METHOD_GET) { // If method is not specified, try both if ($method === RequestTool::METHOD_POST_OR_GET) { try { // Tries POST method, considers param as mandatory. return RequestTool::getParameter($paramName, $type, TRUE, $defaultValue, RequestTool::METHOD_POST); } catch (Exception $e) { // If nothing is found with POST method, as it is mandatory, an exception as raised. Try to find it with GET method. return RequestTool::getParameter($paramName, $type, $isMandatory, $defaultValue, RequestTool::METHOD_GET); } } // Get parameter list $paramList = RequestTool::getParameterList($method); // Checks if parameter is set if (ArrayTool::array_key_exists($paramName, $paramList)) { if ($type == RequestTool::PARAM_TYPE_ARRAY) { $paramValue = $paramList[$paramName]; } else { if (StringTool::strlen($paramList[$paramName]) > 0) { $paramValue = $paramList[$paramName]; } } } // Parameter is not set if (!isset($paramValue)) { // Parameter is mandatory, raising exception if ($isMandatory) { throw new ParameterException('"' . $paramName . '" request ' . $method . ' parameter is mandatory and not set'); } // else returns default value return $defaultValue; } // if magic quotes activated by server configuration, stripslashes if (get_magic_quotes_gpc()) { $paramValue = stripslashes($paramValue); } // Checks if parameter value format is correct RequestTool::checkParameterValue($paramValue, $type); return $paramValue; }
/** * Property writing accessor * @param string $propertyName The property name * @param string $value The property value */ public function setProperty($propertyName, $value) { // Non-null value if ($value !== NULL) { // Numeric value if (StringTool::isInt($value)) { $value = StringTool::toInt($value, false); } else { if (StringTool::isFloat($value, FALSE)) { $value = StringTool::toFloat($value, false); } else { if (StringTool::endsWith($propertyName, 'date')) { // Date has a 10 length (YYYY-mm-dd) if (StringTool::strlen($value) == 10) { $value = DateTool::stringToTimestamp($value, DateTool::FORMAT_MYSQL_DATE); } else { $value = DateTool::stringToTimestamp($value); } } } } // Day property type } // Removes table name at the beginning of field name, not for id fields nor xxx_has_xxx tables $tableName = DatabaseFactory::getElementTableName($this->getElementClass()); if (!StringTool::contains($tableName, ElementFactory::TABLE_JOIN_SEPARATOR)) { $tablePrefix = $tableName . '_'; $tableIdField = $tablePrefix . 'id'; if (StringTool::startsWith($propertyName, $tablePrefix) && (!StringTool::endsWith($propertyName, '_id') || $propertyName == $tableIdField)) { $propertyName = StringTool::truncateFirstChars($propertyName, StringTool::strlen($tablePrefix)); } } // Updates original property list if (!ArrayTool::array_key_exists($propertyName, $this->propertyList)) { // It's the first time this property gets a value, it will be updated (set a null value to original property list) $this->originalPropertyList[$propertyName] = NULL; } else { if (ArrayTool::array_key_exists($propertyName, $this->originalPropertyList)) { // Attribute value had already changed (originalPropertyList already has a value for this property) // If value has been reset to original value, removes the update of the property if ($value == $this->originalPropertyList[$propertyName]) { unset($this->originalPropertyList[$propertyName]); } } else { if ($value !== $this->propertyList[$propertyName]) { // If value has changed, updates original value $this->originalPropertyList[$propertyName] = $this->propertyList[$propertyName]; } } } // Sets property new value $this->propertyList[$propertyName] = $value; }
/** * Returns string without N last chars * @param string $string The original string * @param string $charsNumber The number of chars to trunc * @return string The modified string */ public static function truncateLastChars($string, $charsNumber) { return StringTool::substr($string, 0, StringTool::strlen($string) - $charsNumber); }
/** * Gets element list link to an element * @param string $elementClass The element class searched * @param string $parentElement The parent element to get list of * @param string $conditions The conditions string to apply * @param string $orderBy The order string to apply) * @return array The element list array */ public static function &getElementListFromParent($elementClass, $parentElement, $conditions = NULL, $orderBy = NULL, $join = NULL) { $parentClass = $parentElement->getElementClass(); $parentId = $parentElement->id; // Builds parent id conditions $parentIdFieldName = DatabaseFactory::getParentIdColumnName($parentClass); $tableName = DatabaseFactory::getElementTableName($elementClass); $parentIdCondition = $tableName . '.' . $parentIdFieldName . '=' . $parentId; if ($conditions !== NULL && StringTool::strlen($conditions) > 0) { $conditions = $parentIdCondition . ' AND (' . $conditions . ')'; } else { $conditions = $parentIdCondition; } $elementList = ElementFactory::getElementList($elementClass, $conditions, $orderBy, $join); return $elementList; }