コード例 #1
0
ファイル: TemplateData.php プロジェクト: NerdZombies/MateCode
 function logic($type, $object, $field)
 {
     $return = NULL;
     /*
     				TODO: Add multiple modifiers (separados por :??)
     */
     if ($type[0] == "\$") {
         $parameters = explode(",", substr($type, 1));
         $method = array_shift($parameters);
         $return = call_user_func_array(array($object, $method), $parameters);
     } else {
         $parameters = explode(",", $type);
         $type = array_shift($parameters);
         if (is_numeric($type)) {
             $return = $object[$type];
         } else {
             switch (strToLower($type)) {
                 // DATE CASES
                 /*case "byLanguage":
                 		$return = "hola";
                 		break;*/
                 /*case "first":
                 		$return = array_shift($object);
                 		break;*/
                 case "class":
                     $return = get_class($object);
                     break;
                 case "seofriendly":
                     $return = implode("-", explode(" ", $object)) . VIEW_SEO_EXTENSION;
                     break;
                 case "rpad":
                     $return = str_pad($object, $parameters[0], $parameters[1], STR_PAD_RIGHT);
                     break;
                 case "lpad":
                     $return = str_pad($object, $parameters[0], $parameters[1], STR_PAD_LEFT);
                     break;
                 case "divide":
                     $return = $object / $parameters[0];
                     break;
                 case "sum":
                     $return = intval($object) + $parameters[0];
                     break;
                 case "bool":
                     $return = $object ? "Yes" : "No";
                     break;
                 case "abs":
                     $return = abs($object);
                     break;
                 case "round":
                     $return = round($object * 10) / 10;
                     break;
                 case "multiply":
                     $return = $object * $parameters[0];
                     break;
                 case "md5":
                     $return = md5($object);
                     break;
                 case "diffnow":
                     $return = mktime() - strtotime($object);
                     break;
                 case "value":
                     if ($object != NULL) {
                         $return = $object;
                     } else {
                         $return = 0;
                     }
                     break;
                 case "urlname":
                     $return = strtolower(str_replace(" ", "_", $object));
                     break;
                 case "urlencode":
                     $return = urldecode($object);
                     break;
                 case "mdydate":
                     $return = date("m/d/Y", strtotime($object));
                     break;
                 case "dmydate":
                     $return = date("d/m/Y", strtotime($object));
                     break;
                 case "hmdate":
                     $return = date("H:i", strtotime($object));
                     break;
                 case "month":
                     $return = date("m", strtotime($object));
                     break;
                 case "fullmonth":
                     $return = date("F", strtotime($object));
                     break;
                 case "year":
                     $return = date("Y", strtotime($object));
                     break;
                 case "age":
                     //$return = abs(mktime(0,0,0,date("m",strtotime($object)),date("d",strtotime($object)),date("Y",strtotime($object))) - mktime(4,12,0,date("m"),date("d"),date("Y")) )/(60 * 60 * 24 * 356);
                     $return = date("Y") - date("Y", strtotime($object));
                     if (date("m", strtotime($object)) > date("m") || date("m", strtotime($object)) == date("m") && date("d", strtotime($object)) > date("d")) {
                         $return--;
                     }
                     break;
                 case "date":
                     $return = date("d", strtotime($object));
                     break;
                     /* mailto */
                 /* mailto */
                 case "mailto":
                     $return = mail_tag($object);
                     break;
                 case "empty":
                     $return = intval($object == NULL);
                     break;
                 case "elapsed":
                     import("date.TimeDate");
                     $return = TimeDate::elapsed($object);
                     break;
                 case "human":
                     import("date.TimeDate");
                     $return = TimeDate::human($object);
                     break;
                 case "count":
                     if (is_array($object)) {
                         $return = intval(count($object));
                     } else {
                         if (is_string($object)) {
                             $return = strlen($object);
                         } else {
                             $return = 0;
                         }
                     }
                     break;
                 case "toup":
                     $return = strToUpper($object);
                     break;
                 case "tolow":
                     $return = strToLower($object);
                     break;
                 case "resume":
                     array_splice($parameters, 0, 0, $object);
                     $return = call_user_func_array("resume", $parameters);
                     break;
                 case "custom":
                     //array_splice($parameters,0,0,$object);
                     $mth = array_shift($parameters);
                     $return = call_user_func_array($mth, $object);
                     break;
                 case "min":
                     $min1 = array_shift($parameters);
                     $return = min($min1, $object);
                     break;
                 case "notextile":
                     $return = removeTextile($object);
                     break;
                 case "textile":
                     $return = textile($object);
                     break;
                 case "thousands":
                     $parts = array();
                     while ($object > 1000) {
                         $parts[] = sprintf("%03s", $object % 1000);
                         $object = floor($object / 1000);
                     }
                     $parts[] = sprintf("%3d", $object);
                     $return = join(".", array_reverse($parts));
                     //(round($object*10))/10 . " ".$sizeNames[$s];
                     break;
                 case "humansize":
                     $sizeNames = array("bytes", "Kb", "Mb", "Gb", "Tb");
                     $s = 0;
                     while ($object > 1024) {
                         $object = $object / 1024;
                         $s++;
                     }
                     $return = round($object * 10) / 10 . " " . $sizeNames[$s];
                     break;
                 case "ajaxtoggle":
                     $return = linkTo($object, $this->data["controller"] . "/toggle/" . $this->data["item"]->id . "/{$field}", array("class" => "ajaxToggle tbutton"));
                     /*<mate:link to="{controller}/toggle/{item.id}/commentable" options="class:ajaxToggle tbutton">{item.commentable:bool}</mate:link>*/
                     break;
                 case "gotomodel":
                     $return = linkTo($object, $this->data["controller"] . "/view/" . $this->data["item"]->id);
                     /*<mate:link to="{controller}/toggle/{item.id}/commentable" options="class:ajaxToggle tbutton">{item.commentable:bool}</mate:link>*/
                     break;
                 case "random":
                     if (is_array($object)) {
                         $return = $object[rand(0, count($object))];
                     } else {
                         $return = rand(0, $object);
                     }
                     break;
                 case "export":
                     $return = var_export($object, true);
                     break;
                 default:
                     $return = $this->verbose ? $obj_path : "";
                     break;
             }
         }
     }
     return $return;
 }