CrugeUtil.php 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
<?php /**
CrugeUtil

funciones variadas que se usan durante toda la aplicacion.

@author: Christian Salazar H. <christiansalazarh@gmail.com> @salazarchris74
@license protected/modules/cruge/LICENSE
 */
class CrugeUtil extends CComponent
{

    public static function config()
    {
        return Yii::app()->getModule('cruge');
    }

    public static function factory()
    {
        return Yii::app()->getModule('cruge')->factory;
    }

    /*
        crea una URL normalizada relativa al action UiController
    */
    public static function uiaction($actionName, $params = array())
    {
        return Yii::app()->createUrl('/' . self::config()->id . '/ui/' . $actionName, $params);
    }

    public static function passwordGenerator()
    {
        return substr(self::hash(rand() . rand()), 0, 8);
    }

    /* normaliza el nombre de la tabla anexandole el prefijo y aplicando mapping

    */
    public static function getTableName($tableName)
    {
        $prfx = self::config()->tableprefix;
        $_tableName = trim(strtolower($tableName));
        if (isset(self::config()->maptables[$_tableName])) {
            $_tableName = self::config()->maptables[$_tableName];
        }
        return $prfx . $_tableName;
    }

    public static function isPhpFile($filename)
    {
        return "php" === strtolower(trim(strrev(substr(strrev(trim($filename)), 0, 3))));
    }

    public static function getClassNameFromPhp($filename)
    {
        $noext = trim(substr(strrev(trim($filename)), 4, strlen(trim($filename)) - 4));
        $k = 0;
        for ($i = 0; $i < strlen($noext); $i++) {
            if (($noext[$i] == '\\') || ($noext[$i] == '/')) {
                $k = $i;
            }
            if ($k > 0) {
                break;
            }
        }
        if ($k == 0) {
            $k = strlen($noext);
        }

        return strrev(substr($noext, 0, $k));
    }

    public static function now()
    {
        return time();
    }

    public static function makeExpirationDateTime($minutesPlus)
    {
        return time() + ($minutesPlus * 60);
    }

    public static function isExpired($expirationdate)
    {
        return !(self::now() <= $expirationdate);
    }

    public static function getIpAddress()
    {
        return Yii::app()->request->userHostAddress;
    }

    /**
    toma una tira de valores:
    "1, azul\n2, rojo\n3, verde"

    y devuelve un array asi:

    ar[1] = azul
    ar[2] = rojo
    ar[3] = verde
     */
    public static function explodeOptions($listValues)
    {
        $lista = explode("\n", $listValues);
        $ar = array();
        foreach ($lista as $item) {
            $k = explode(",", $item);
            $val = "";
            $text = "";
            if (count($k) == 2) {
                $val = trim($k[0]);
                $text = trim($k[1]);
            } else {
                $val = "0";
                $text = trim($k[0]);
            }
            $ar[$val] = $text;
        }
        return $ar;
    }

    /**
     * Devuelve el hash del valor en el parámetro $value
     * @param $value
     * @return string
     */
    public static function hash($value)
    {
        $algo = self::config()->hash;
        return hash($algo, $value);
    }

}