/components/P2Helper.php
PHP | 681 lines | 538 code | 43 blank | 100 comment | 50 complexity | 6333220226185f3f445852bdb4f5555d MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
- <?php
- /**
- * Class File
- *
- * @author Tobias Munk <schmunk@usrbin.de>
- * @link http://www.phundament.com/
- * @copyright Copyright © 2005-2010 diemeisterei GmbH
- * @license http://www.phundament.com/license/
- */
- /**
- * Helper class
- *
- * Detailed info
- * <pre>
- * $var = code_example();
- * </pre>
- * {@link DefaultController}
- *
- * @author Tobias Munk <schmunk@usrbin.de>
- * @version $Id: P2Helper.php 547 2010-03-31 22:54:57Z schmunk $
- * @package p2.components
- * @since 2.0
- */
- class P2Helper {
- const MODULE_NAME = 'p2';
- const MODULE_VERSION = '2.1-b9';
- public static function return_url($encode = false) {
- // fixme? REQUEST, GET or POST?
- if (isset($_REQUEST['return_url'])) {
- if ($encode)
- return $_REQUEST['return_url'];
- else
- return base64_decode(urldecode($_REQUEST['return_url']));
- }
- else {
- return false;
- }
- }
- /**
- * Return the current requestUri
- *
- * @return string
- */
- public static function uri() {
- return urlencode(base64_encode(Yii::app()->request->requestUri));
- }
- public static function cleanUrlString($str) {
- $str = strtr(utf8_decode($str),
- utf8_decode('ĽľŔÁÂĂÄĹĆÇČÉĘËĚÍÎĎĐŃŇÓÔŐÖŘŮÚŰÜÝßŕáâăäĺćçčéęëěíîďđńňóôőöřůúűüý˙'),
- 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
- return preg_replace("/[^a-zA-Z0-9_-]+/", "-", $str);
- }
- public static function xmlspecialchars($text) {
- return str_replace(''', ''', htmlspecialchars($text, ENT_QUOTES));
- }
- public static function numeric_entities($string) {
- $mapping_hex = array();
- $mapping_dec = array();
- foreach (get_html_translation_table(HTML_ENTITIES, ENT_QUOTES) as $char => $entity) {
- $mapping_hex[html_entity_decode($entity, ENT_QUOTES, "UTF-8")] = '&#x' . strtoupper(dechex(ord(html_entity_decode($entity, ENT_QUOTES)))) . ';';
- $mapping_dec[html_entity_decode($entity, ENT_QUOTES, "UTF-8")] = '&#' . ord(html_entity_decode($entity, ENT_QUOTES)) . ';';
- }
- $string = str_replace(array_values($mapping_hex), array_keys($mapping_hex), $string);
- $string = str_replace(array_values($mapping_dec), array_keys($mapping_dec), $string);
- return $string;
- }
- /**
- * Returns a clearfloat HTML snip
- *
- * @return string
- */
- public static function clearfloat() {
- Yii::app()->clientScript->registerCss('P2Helper.clearfloat', '.clearfloat{ clear:both; height:0; font-size: 1px; line-height: 0px; }');
- return "<div class='clearfloat'></div>";
- }
- public static function writeFileLogs() {
- $logger = Yii::getLogger();
- foreach (Yii::app()->log->routes as $route) {
- if ($route->enabled && $route instanceof CFileLogRoute) {
- $route->collectLogs($logger, true);
- }
- }
- }
- /**
- * Return HTML markup for a key value pair array.
- *
- * @param <type> $data
- * @return string
- */
- public static function table($data) {
- if (!is_array($data)) {
- return 'Empty table.';
- }
- $return = '<table class="simpleTable">';
- foreach ($data AS $key => $value) {
- $return .= "<tr>";
- $return .= "<th>" . $key . "</th>";
- $return .= "<td>" . $value . "</td>";
- $return .= "</tr>";
- }
- $return .= "</table>";
- return $return;
- }
- public static function juiButtonCssClass($icon = false) {
- ob_start();
- Yii::app()->controller->widget("P2JuiTheme");
- ob_end_clean();
- $return = 'button ui-state-default ui-corner-all ';
- $return .= ( $icon) ? ' ui-icon-container' : '';
- return $return;
- }
- public static function juiIcon($icon, $applyStyle = true) {
- ob_start();
- Yii::app()->controller->widget("P2JuiTheme");
- ob_end_clean();
- $style = ($applyStyle) ? 'style="margin-right: 0.5em; display: inline-block; float:left"' : 'style=" float:left; display: inline-block;"';
- return '<span class="ui-icon ui-icon-' . $icon . '" ' . $style . '> </span>';
- }
- public static function icon($key, $type = null) {
- if ($type == 'flag')
- return Yii::app()->theme->baseUrl . "/flags/" . $key . ".gif";
- else
- return Yii::app()->theme->baseUrl . "/icons/" . $key . ".png";
- }
- public static function language($type = null) {
- if ($type == 'UC_ALL') {
- return str_replace(' ', '', ucwords(str_replace('_', ' ', Yii::app()->language)));
- } else {
- return Yii::app()->language;
- }
- }
- /**
- * A wrapper for Yii::t(), you may use this method for special translations, like dynamic values.
- *
- * @param <type> $category
- * @param <type> $message
- * @param <type> $params
- * @param <type> $source
- * @param <type> $language
- * @return <type>
- */
- public static function t($category, $message, $params = array(), $source = null, $language = null) {
- return Yii::t($category, $message, $params, $source, $language);
- }
- public static function widget($className, $properties) {
- ob_start();
- Yii::app()->controller->widget($className, $properties);
- return ob_get_clean();
- }
- // FIXME
- public static function relation($relation, $property) {
- if ($relation !== null)
- return CHtml::link($relation->$property, array(P2Helper::lcfirst(get_class($relation)) . "/show", "id" => $relation->id));
- else
- return "";
- }
- // FIXME
- public static function hasMany($relation) {
- if (!isset($relation[0]))
- return;
- else
- $controller = P2Helper::lcfirst(get_class($relation[0]));
- $return = "";
- foreach (CHtml::listData($relation, 'id', 'title') AS $id => $name) {
- $return .= CHtml::link($name, array($controller . '/update', 'id' => $id)) . ', ';
- };
- return $return;
- }
- /**
- *
- * @return <type>
- */
- public static function powered() {
- return 'Powered by <a class="poweredByPhundament" href="http://phundament.com">phundament</a>.';
- }
- /**
- *
- * @param <type> $relativePath
- * @return <type>
- */
- public static function publishAsset($relativePath = null) {
- if ($relativePath === null) {
- Yii::log('Relative path can not be null', CLogger::LEVEL_WARNING, "p2.helper");
- } else {
- return Yii::app()->assetManager->publish(Yii::app()->basePath . DS . $relativePath);
- }
- }
- public static function registerJsExtension($path = null) {
- if ($path === null) {
- throw new Exception('No plugin path specified!');
- }
- $script = self::findModule()->basePath . DS . "extensions" . DS . $path;
- $url = Yii::app()->assetManager->publish($script);
- Yii::app()->clientScript->registerScriptFile($url);
- }
- /**
- * Addiditional config string parsing
- *
- * @param <type> $key
- * @return <type>
- */
- public static function configure($key) {
- $args = func_get_args();
- $data = null;
- switch ($args[0]) {
- case 'jquery:loadingStart':
- $data = '$("' . $args[1] . '").fadeTo("fast", 0.5);';
- break;
- case 'jquery:loadingEnd':
- $data = '$("' . $args[1] . '").fadeTo("normal", 1);';
- break;
- case 'cTabView:cssFile':
- if (Yii::app()->theme) {
- $url = Yii::app()->theme->baseUrl . DS . 'css' . DS . 'jquery.yiitab.css';
- $path = Yii::app()->theme->basePath . DS . 'css' . DS . 'jquery.yiitab.css';
- $data = (is_file($path)) ? $url : null;
- }
- break;
- case 'ckeditor:config':
- // FIXME: ckeditor-3.0.1 no baseUrl, bodyId
- if (Yii::app()->theme) {
- if (is_array($args[1]['contentsCss'])) {
- foreach ($args[1]['contentsCss'] AS $css) {
- $parse['contentsCss'][] = "http://" . $_SERVER['SERVER_NAME'] . "/" . Yii::app()->theme->baseUrl . "/" . $css . "";
- }
- } else {
- $parse['contentsCss'] = "http://" . $_SERVER['SERVER_NAME'] . "/" . Yii::app()->theme->baseUrl . "/" . $args[1]['contentsCss'];
- }
- }
- #exit;
- // parse assets
- $parse['stylesCombo_stylesSet'] = 'default:' . Yii::app()->assetManager->publish(Yii::app()->basePath . DS . $args[1]['stylesCombo_stylesSet']);
- $parse['templates'] = 'default';
- $parse['templates_files'] = array(Yii::app()->assetManager->publish(Yii::app()->basePath . DS . $args[1]['templates_files']));
- // parse URLs
- $parse['filebrowserBrowseUrl'] = Yii::app()->controller->createUrl($args[1]['filebrowserBrowseUrl']);
- $parse['filebrowserImageBrowseUrl'] = Yii::app()->controller->createUrl($args[1]['filebrowserImageBrowseUrl']);
- $parse['filebrowserFlashBrowseUrl'] = Yii::app()->controller->createUrl($args[1]['filebrowserFlashBrowseUrl']);
- $data = array_merge($args[1], $parse);
- break;
- default:
- throw new Exception('Config key not found!');
- }
- return $data;
- }
- /**
- * Finds a module by id, returns 'p2' if no id given
- *
- * @param string $id
- * @return CModule
- */
- public static function findModule($id = null) {
- if ($id === null)
- $id = self::MODULE_NAME;
- return Yii::app()->findModule($id);
- }
- // BIG TODO here - how to check classes ?
- public static function classExists($classPath) {
- $class = Yii::import($classPath);
- #echo $class; exit;
- if (class_exists($class) == true) {
- return $class;
- } else {
- return false;
- throw new CException();
- }
- }
- /**
- * Lowercases only the first character of a string
- *
- * @param <type> $string
- * @return string
- */
- public static function lcfirst($string) {
- return strtolower(substr($string, 0, 1)) . substr($string, 1);
- }
- /**
- * Retrieves $_POST variable by array values
- *
- * @param array $array "the key-chain"
- * @return string
- */
- public static function getPostVar($array) {
- switch (count($array)) {
- case 0:
- $msg = "Unable to determine POST vars!";
- throw new Exception($msg);
- break;
- case 1:
- $return = $_POST[$array[0]];
- break;
- case 2:
- $return = $_POST[$array[0]][$array[1]];
- break;
- case 3:
- $return = $_POST[$array[0]][$array[1]][$array[2]];
- break;
- default:
- $msg = "Too many POST fields specified!";
- throw new Exception($msg);
- break;
- }
- return $return;
- }
- public static function hash($input) {
- return md5($input);
- }
- public static function getClassProperties($classPath) {
- $class = self::classExists($classPath);
- if ($class) {
- return get_class_vars($class);
- }
- }
- public static function getModules() {
- $dir = Yii::app()->basePath;
- foreach (scandir($dir . DS . "modules") AS $module) {
- if ((($module != ".") && ($module != "..")) && (is_dir($dir . DS . "modules" . DS . $module) && strstr($module, ".") === false)) {
- Yii::import("application.modules." . $module . ".controllers.*");
- $return[] = $module;
- }
- }
- return $return;
- }
- public static function getControllers($module = null) {
- $return = array();
- if ($module === null) {
- $dir = Yii::app()->basePath . DS . "controllers";
- } else {
- $dir = Yii::app()->basePath . DS . "modules" . DS . $module . DS . "controllers";
- }
- foreach (scandir($dir) AS $controller) {
- if (!is_dir($dir . DS . $controller)) {
- if (substr($controller, 0, 1) == ".")
- continue;
- Yii::import("application.controllers.*");
- #self::getActions(str_replace(".php","",$controller));
- $trans = array("Controller.php" => "", "\\" => "/");
- if ($module !== null)
- $return[strtr($controller, $trans)] = $module . "/" . P2Helper::lcfirst(strtr($controller, $trans));
- else
- $return[strtr($controller, $trans)] = P2Helper::lcfirst(strtr($controller, $trans));
- }
- }
- return $return;
- }
- public static function getActions($controller) {
- $return = array();
- Yii::trace("Scanning actions for $controller ...");
- $methods = get_class_methods(ucfirst(basename($controller) . "Controller"));
- foreach ($methods AS $method) {
- if ($method == "actions")
- continue;
- if ($method == "actionDelete")
- continue;
- if ($method == "actionShow")
- continue;
- if ($method == "actionUpdate")
- continue;
- if (substr($method, 0, 6) == "action") {
- $key = P2Helper::lcfirst(str_replace("action", "", $method));
- $trans = array("action" => "", "\\" => "/");
- $value = strtr(Yii::app()->createUrl($controller . DS . $method), $trans);
- $return[$key] = $value;
- }
- }
- return $return;
- }
- public static function html_convert_entities($string) {
- return preg_replace_callback('/&([a-zA-Z][a-zA-Z0-9]+);/',
- array('P2Helper','convert_entity'), $string);
- }
- /**
- *
- * from http://inanimatt.com/php-convert-entities.php
- *
- * @staticvar <type> $table
- * @param <type> $matches
- * @return <type>
- */
- public static function convert_entity($matches) {
- static $table = array('quot' => '"',
- 'amp' => '&',
- 'lt' => '<',
- 'gt' => '>',
- 'OElig' => 'Œ',
- 'oelig' => 'œ',
- 'Scaron' => 'Š',
- 'scaron' => 'š',
- 'Yuml' => 'Ÿ',
- 'circ' => 'ˆ',
- 'tilde' => '˜',
- 'ensp' => ' ',
- 'emsp' => ' ',
- 'thinsp' => ' ',
- 'zwnj' => '‌',
- 'zwj' => '‍',
- 'lrm' => '‎',
- 'rlm' => '‏',
- 'ndash' => '–',
- 'mdash' => '—',
- 'lsquo' => '‘',
- 'rsquo' => '’',
- 'sbquo' => '‚',
- 'ldquo' => '“',
- 'rdquo' => '”',
- 'bdquo' => '„',
- 'dagger' => '†',
- 'Dagger' => '‡',
- 'permil' => '‰',
- 'lsaquo' => '‹',
- 'rsaquo' => '›',
- 'euro' => '€',
- 'fnof' => 'ƒ',
- 'Alpha' => 'Α',
- 'Beta' => 'Β',
- 'Gamma' => 'Γ',
- 'Delta' => 'Δ',
- 'Epsilon' => 'Ε',
- 'Zeta' => 'Ζ',
- 'Eta' => 'Η',
- 'Theta' => 'Θ',
- 'Iota' => 'Ι',
- 'Kappa' => 'Κ',
- 'Lambda' => 'Λ',
- 'Mu' => 'Μ',
- 'Nu' => 'Ν',
- 'Xi' => 'Ξ',
- 'Omicron' => 'Ο',
- 'Pi' => 'Π',
- 'Rho' => 'Ρ',
- 'Sigma' => 'Σ',
- 'Tau' => 'Τ',
- 'Upsilon' => 'Υ',
- 'Phi' => 'Φ',
- 'Chi' => 'Χ',
- 'Psi' => 'Ψ',
- 'Omega' => 'Ω',
- 'alpha' => 'α',
- 'beta' => 'β',
- 'gamma' => 'γ',
- 'delta' => 'δ',
- 'epsilon' => 'ε',
- 'zeta' => 'ζ',
- 'eta' => 'η',
- 'theta' => 'θ',
- 'iota' => 'ι',
- 'kappa' => 'κ',
- 'lambda' => 'λ',
- 'mu' => 'μ',
- 'nu' => 'ν',
- 'xi' => 'ξ',
- 'omicron' => 'ο',
- 'pi' => 'π',
- 'rho' => 'ρ',
- 'sigmaf' => 'ς',
- 'sigma' => 'σ',
- 'tau' => 'τ',
- 'upsilon' => 'υ',
- 'phi' => 'φ',
- 'chi' => 'χ',
- 'psi' => 'ψ',
- 'omega' => 'ω',
- 'thetasym' => 'ϑ',
- 'upsih' => 'ϒ',
- 'piv' => 'ϖ',
- 'bull' => '•',
- 'hellip' => '…',
- 'prime' => '′',
- 'Prime' => '″',
- 'oline' => '‾',
- 'frasl' => '⁄',
- 'weierp' => '℘',
- 'image' => 'ℑ',
- 'real' => 'ℜ',
- 'trade' => '™',
- 'alefsym' => 'ℵ',
- 'larr' => '←',
- 'uarr' => '↑',
- 'rarr' => '→',
- 'darr' => '↓',
- 'harr' => '↔',
- 'crarr' => '↵',
- 'lArr' => '⇐',
- 'uArr' => '⇑',
- 'rArr' => '⇒',
- 'dArr' => '⇓',
- 'hArr' => '⇔',
- 'forall' => '∀',
- 'part' => '∂',
- 'exist' => '∃',
- 'empty' => '∅',
- 'nabla' => '∇',
- 'isin' => '∈',
- 'notin' => '∉',
- 'ni' => '∋',
- 'prod' => '∏',
- 'sum' => '∑',
- 'minus' => '−',
- 'lowast' => '∗',
- 'radic' => '√',
- 'prop' => '∝',
- 'infin' => '∞',
- 'ang' => '∠',
- 'and' => '∧',
- 'or' => '∨',
- 'cap' => '∩',
- 'cup' => '∪',
- 'int' => '∫',
- 'there4' => '∴',
- 'sim' => '∼',
- 'cong' => '≅',
- 'asymp' => '≈',
- 'ne' => '≠',
- 'equiv' => '≡',
- 'le' => '≤',
- 'ge' => '≥',
- 'sub' => '⊂',
- 'sup' => '⊃',
- 'nsub' => '⊄',
- 'sube' => '⊆',
- 'supe' => '⊇',
- 'oplus' => '⊕',
- 'otimes' => '⊗',
- 'perp' => '⊥',
- 'sdot' => '⋅',
- 'lceil' => '⌈',
- 'rceil' => '⌉',
- 'lfloor' => '⌊',
- 'rfloor' => '⌋',
- 'lang' => '〈',
- 'rang' => '〉',
- 'loz' => '◊',
- 'spades' => '♠',
- 'clubs' => '♣',
- 'hearts' => '♥',
- 'diams' => '♦',
- 'nbsp' => ' ',
- 'iexcl' => '¡',
- 'cent' => '¢',
- 'pound' => '£',
- 'curren' => '¤',
- 'yen' => '¥',
- 'brvbar' => '¦',
- 'sect' => '§',
- 'uml' => '¨',
- 'copy' => '©',
- 'ordf' => 'ª',
- 'laquo' => '«',
- 'not' => '¬',
- 'shy' => '­',
- 'reg' => '®',
- 'macr' => '¯',
- 'deg' => '°',
- 'plusmn' => '±',
- 'sup2' => '²',
- 'sup3' => '³',
- 'acute' => '´',
- 'micro' => 'µ',
- 'para' => '¶',
- 'middot' => '·',
- 'cedil' => '¸',
- 'sup1' => '¹',
- 'ordm' => 'º',
- 'raquo' => '»',
- 'frac14' => '¼',
- 'frac12' => '½',
- 'frac34' => '¾',
- 'iquest' => '¿',
- 'Agrave' => 'À',
- 'Aacute' => 'Á',
- 'Acirc' => 'Â',
- 'Atilde' => 'Ã',
- 'Auml' => 'Ä',
- 'Aring' => 'Å',
- 'AElig' => 'Æ',
- 'Ccedil' => 'Ç',
- 'Egrave' => 'È',
- 'Eacute' => 'É',
- 'Ecirc' => 'Ê',
- 'Euml' => 'Ë',
- 'Igrave' => 'Ì',
- 'Iacute' => 'Í',
- 'Icirc' => 'Î',
- 'Iuml' => 'Ï',
- 'ETH' => 'Ð',
- 'Ntilde' => 'Ñ',
- 'Ograve' => 'Ò',
- 'Oacute' => 'Ó',
- 'Ocirc' => 'Ô',
- 'Otilde' => 'Õ',
- 'Ouml' => 'Ö',
- 'times' => '×',
- 'Oslash' => 'Ø',
- 'Ugrave' => 'Ù',
- 'Uacute' => 'Ú',
- 'Ucirc' => 'Û',
- 'Uuml' => 'Ü',
- 'Yacute' => 'Ý',
- 'THORN' => 'Þ',
- 'szlig' => 'ß',
- 'agrave' => 'à',
- 'aacute' => 'á',
- 'acirc' => 'â',
- 'atilde' => 'ã',
- 'auml' => 'ä',
- 'aring' => 'å',
- 'aelig' => 'æ',
- 'ccedil' => 'ç',
- 'egrave' => 'è',
- 'eacute' => 'é',
- 'ecirc' => 'ê',
- 'euml' => 'ë',
- 'igrave' => 'ì',
- 'iacute' => 'í',
- 'icirc' => 'î',
- 'iuml' => 'ï',
- 'eth' => 'ð',
- 'ntilde' => 'ñ',
- 'ograve' => 'ò',
- 'oacute' => 'ó',
- 'ocirc' => 'ô',
- 'otilde' => 'õ',
- 'ouml' => 'ö',
- 'divide' => '÷',
- 'oslash' => 'ø',
- 'ugrave' => 'ù',
- 'uacute' => 'ú',
- 'ucirc' => 'û',
- 'uuml' => 'ü',
- 'yacute' => 'ý',
- 'thorn' => 'þ',
- 'yuml' => 'ÿ'
- );
- // Entity not found? Destroy it.
- return isset($table[$matches[1]]) ? $table[$matches[1]] : '';
- }
- }
- ?>