Exemplo n.º 1
0
 /**
  * Returns or sets the non-html text value for a form element
  * @return RaxanElement or string
  */
 public function textVal($txt = null)
 {
     $s = Raxan::getSharedSanitizer();
     return $txt === null ? $s->textVal($this->val()) : $this->val($s->textVal($txt));
 }
Exemplo n.º 2
0
 /**
  * Binds an Array or a PDO result set to a template
  * @returns string
  */
 public static function bindTemplate($rows, $options)
 {
     $rowType = '';
     if ($rows instanceof PDOStatement) {
         $rows = $rows->fetchAll(PDO::FETCH_ASSOC);
     } else {
         if ($rows instanceof RaxanElement) {
             $rows = $rows->get();
             // get all matched elements
             $rowType = 'raxanElement';
         }
     }
     if (empty($rows) || !is_array($rows)) {
         return '';
     }
     $initRowCount = 1;
     $removeTags = true;
     // Remove unused tags by default
     $opt = $tplAlt = $tpl = $options;
     $truncStr = '...';
     $page = $size = $trunc = $tr1 = $tr2 = 0;
     $tplF = $tplL = $tplS = $tplE = $key = $edited = $selected = '';
     $fn = $delimiter = $callByVariable = $rtArr = '';
     $itemClass = $altClass = $editClass = $selectClass = '';
     $firstClass = $lastClass = '';
     $fmt = $format = $fmtr = '';
     if (is_array($opt)) {
         if (isset($opt[0])) {
             // fetch templates from index based array
             $tplAlt = isset($opt[1]) ? $opt[1] : $opt[0];
             $tpl = $opt[0];
         } else {
             // fetch options from assoc array
             $tpl = isset($opt['tpl']) ? $opt['tpl'] : '';
             $tplAlt = isset($opt['tplAlt']) ? $opt['tplAlt'] : $tpl;
             $tplF = isset($opt['tplFirst']) ? $opt['tplFirst'] : '';
             $tplL = isset($opt['tplLast']) ? $opt['tplLast'] : '';
             $tplS = isset($opt['tplSelected']) ? $opt['tplSelected'] : '';
             $tplE = isset($opt['tplEdit']) ? $opt['tplEdit'] : '';
             $itemClass = isset($opt['itemClass']) ? $opt['itemClass'] : '';
             $altClass = isset($opt['altClass']) ? $opt['altClass'] : '';
             $selectClass = isset($opt['selectClass']) ? $opt['selectClass'] : '';
             $editClass = isset($opt['editClass']) ? $opt['editClass'] : '';
             $firstClass = isset($opt['firstClass']) ? $opt['firstClass'] : '';
             $lastClass = isset($opt['lastClass']) ? $opt['lastClass'] : '';
             $key = isset($opt['key']) ? $opt['key'] : '';
             $edited = isset($opt['edited']) ? $opt['edited'] : '';
             $selected = isset($opt['selected']) ? $opt['selected'] : '';
             $page = isset($opt['page']) ? (int) $opt['page'] : 0;
             $size = isset($opt['pageSize']) ? (int) $opt['pageSize'] : 0;
             $delimiter = isset($opt['delimiter']) ? $opt['delimiter'] : '';
             $fn = isset($opt['callback']) ? $opt['callback'] : 0;
             $rtArr = isset($opt['returnArray']) ? true : false;
             $format = isset($opt['format']) ? $opt['format'] : '';
             $truncStr = isset($opt['truncString']) ? $opt['truncString'] : $truncStr;
             $trunc = isset($opt['truncate']) ? (double) $opt['truncate'] : 0;
             $removeTags = isset($opt['removeUnusedTags']) ? $opt['removeUnusedTags'] : $removeTags;
             $initRowCount = isset($opt['initRowCount']) ? $opt['initRowCount'] : $initRowCount;
             $tr1 = intval($trunc);
             $tr2 = abs(str_replace('0.', '', $trunc - $tr1));
             // get truncate values
             if ($selected && !is_array($selected)) {
                 $selected = array($selected);
             }
             if ($fn) {
                 if (!is_callable($fn)) {
                     Raxan::throwCallbackException($fn);
                 }
                 $callByVariable = is_string($fn);
             }
         }
     }
     // setup css classes
     $cssClasses = array(self::TPL_FIRST => $firstClass, self::TPL_LAST => $lastClass, self::TPL_ALT => $altClass, self::TPL_ITEM => $itemClass, self::TPL_SELECT => $selectClass, self::TPL_EDIT => $editClass);
     // fix: using {tags} in <a> tags. E.g.  <a href="{name}">
     if (strpos($tpl . $tplAlt . $tplF . $tplL . $tplS, '%7B') !== false) {
         $a1 = array('%7B', '%7D');
         $a2 = array('{', '}');
         list($tpl, $tplAlt, $tplF, $tplL, $tplS) = str_replace($a1, $a2, array($tpl, $tplAlt, $tplF, $tplL, $tplS));
     }
     // get record size if not set
     if (!$size && ($tplL || $lastClass || $page || $trunc != 0)) {
         $size = count($rows);
     }
     // finalize row setup
     $rc = $page ? ($page - 1) * $size : $initRowCount - 1;
     // initail row count
     $rt = array();
     $isIndex = false;
     $startTrunc = '';
     if ($rowType != 'raxanElement') {
         if (!isset($rows[0])) {
             $rows = array($rows);
         } else {
             if (is_object($rows[0])) {
                 // check for an array of objects
                 $rowType = 'object';
             } else {
                 if (!is_array($rows[0])) {
                     $isIndex = true;
                 }
             }
         }
     }
     // create empty format array when using callback
     if ($fn) {
         $format = $format ? $format : array();
     }
     // bind rows to template
     foreach ($rows as $i => $row) {
         if ($page && $i < ($page - 1) * $size) {
             continue;
         } else {
             if ($page && $i > $page * $size - 1) {
                 break;
             }
         }
         if ($trunc != 0) {
             // truncate rows
             if ($tr1 > 0 && $i + 1 > ($page - 1) * $size + $tr2 && $i + 1 <= ($page - 1) * $size + $tr1 + $tr2 || $tr1 < 0 && $i + 1 > $page * $size + $tr1 - $tr2 && $i + 1 <= $page * $size - $tr2) {
                 if (!$startTrunc) {
                     $rt[] = $truncStr;
                 }
                 $startTrunc = true;
                 continue;
             } else {
                 $startTrunc = false;
             }
         }
         $rc++;
         // increment row count
         // set template
         if ($rc == 1) {
             // first
             $t = $tplF ? $tplF : $tpl;
             $tplType = self::TPL_FIRST;
         } else {
             if ($rc == $size) {
                 // last
                 $t = $tplL ? $tplL : $tpl;
                 $tplType = self::TPL_LAST;
             } else {
                 if ($i % 2) {
                     // alt
                     $t = $tplAlt ? $tplAlt : $tpl;
                     $tplType = self::TPL_ALT;
                 } else {
                     // item
                     $t = $tpl;
                     $tplType = self::TPL_ITEM;
                 }
             }
         }
         // check if row selected
         if ($selected && ($key || $isIndex)) {
             $v = isset($row[$key]) ? $row[$key] : $row;
             if (in_array($v, $selected)) {
                 $t = $tplS ? $tplS : $t;
                 $tplType = self::TPL_SELECT;
             }
         }
         // check if row should be edited
         if ($edited && ($key || $isIndex)) {
             $v = isset($row[$key]) ? $row[$key] : $row;
             if ($v == $edited) {
                 $t = $tplE ? $tplE : $t;
                 $tplType = self::TPL_EDIT;
             }
         }
         // check if row is an element
         if ($rowType == 'object') {
             $row = (array) $row;
         } else {
             if ($rowType == 'raxanElement') {
                 $v = array('INDEX' => $i, 'VALUE' => $row->nodeValue);
                 $row = $row->attributes;
                 foreach ($row as $attr) {
                     $v[$attr->name] = $attr->value;
                 }
                 $row = $v;
                 $v = null;
                 $attr = null;
             } else {
                 if ($isIndex) {
                     // setup index row
                     $row = array('INDEX' => $i, 'VALUE' => $row);
                 }
             }
         }
         $fmt = $format;
         // reset format before callback
         $cssClass = $cssClasses[$tplType] ? $cssClasses[$tplType] : $cssClasses[self::TPL_ITEM];
         // callback handler
         if ($fn) {
             $rtn = $callByVariable ? $fn($row, $i, $t, $tplType, $fmt, $cssClass) : $fn[0]->{$fn[1]}($row, $i, $t, $tplType, $fmt, $cssClass);
             if ($rtn === false) {
                 continue;
             } else {
                 if ($rtn !== null) {
                     // check if a value was returned. if not, let the binder handle the insert
                     $rt[] = $rtn;
                     continue;
                 }
             }
         }
         // sanitizer
         if ($fmt && !$fmtr) {
             $fmtr = Raxan::getSharedSanitizer();
             // sanitizer with direct input
             $fmtrParam = array();
         }
         // format & sanitize row values
         foreach ($row as $n => $v) {
             if (!isset($row[$n])) {
                 continue;
             } else {
                 if (!is_scalar($row[$n])) {
                     $row[$n] = '';
                     continue;
                     // skip nested arays and objects
                 }
             }
             // format row value
             $fv = isset($fmt[$n]) ? $fmt[$n] : '';
             if (!$fv) {
                 $row[$n] = htmlspecialchars($v);
             } else {
                 // format value
                 if (!isset($fmtrParam[$fv])) {
                     $f = $fv;
                     if ($f === 'longdate' || $f === 'shortdate') {
                         $f = 'date:' . substr($f, 0, -4);
                     }
                     $v = ($p = strpos($f, ':')) ? substr($f, $p + 1) : null;
                     $f = $fmt[$n] = $p ? substr($f, 0, $p) : $f;
                     if ($f == 'replace') {
                         $v = explode(',', $v, 2);
                         if (!isset($v[1])) {
                             $v[1] = '';
                         }
                     }
                     $fmtrParam[$fv] = $v;
                     $fmtrParam[$fv . '.type'] = $f;
                 }
                 $p = $fmtrParam[$fv];
                 // get parameter
                 $f = $fmtrParam[$fv . '.type'];
                 // get type
                 if ($f === 'int' || $f === 'integer') {
                     $row[$n] = $fmtr->intVal($row[$n]);
                 } else {
                     if ($f === 'float') {
                         $row[$n] = $fmtr->floatVal($row[$n]);
                     } else {
                         if ($f === 'text') {
                             $row[$n] = $fmtr->textVal($row[$n]);
                         } else {
                             if ($f === 'escape') {
                                 $row[$n] = $fmtr->escapeVal($row[$n]);
                             } else {
                                 if ($f === 'money') {
                                     $row[$n] = $fmtr->formatMoney($row[$n], $p);
                                 } else {
                                     if ($f === 'date') {
                                         $row[$n] = $fmtr->formatDate($row[$n], $p);
                                     } else {
                                         if ($f === 'number') {
                                             $row[$n] = $fmtr->formatNumber($row[$n], $p);
                                         } else {
                                             if ($f === 'percentage') {
                                                 $row[$n] = $row[$n] * 100 . '%';
                                             } else {
                                                 if ($f === 'capitalize') {
                                                     $row[$n] = ucwords($row[$n]);
                                                 } else {
                                                     if ($f === 'replace') {
                                                         $row[$n] = preg_replace('/' . $p[0] . '/i', $p[1], $row[$n]);
                                                     } else {
                                                         if ($f === 'lower') {
                                                             $row[$n] = strtolower($row[$n]);
                                                         } else {
                                                             if ($f === 'upper') {
                                                                 $row[$n] = strtoupper($row[$n]);
                                                             } else {
                                                                 if ($f == 'html') {
                                                                     $row[$n] = $fmtr->htmlVal($row[$n]);
                                                                 } else {
                                                                     if ($f != 'raw') {
                                                                         $row[$n] = '';
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 // check if we should allow raw values
             }
             // set format style. Remove illegal quotes (") from color an style
             if (isset($fmt[$n . ' bold'])) {
                 $row[$n] = '<strong>' . $row[$n] . '</strong>';
             }
             if (isset($fmt[$n . ' color'])) {
                 $row[$n] = '<span style="color:' . str_replace('"', '', $fmt[$n . ' color']) . '">' . $row[$n] . '</span>';
             }
             if (isset($fmt[$n . ' style'])) {
                 $row[$n] = '<span style="' . str_replace('"', '', $fmt[$n . ' style']) . '">' . $row[$n] . '</span>';
             }
         }
         $keys = !isset($keys) ? explode(',', '{' . implode('},{', array_keys($row)) . '},{ROWCOUNT},{ROWCLASS}') : $keys;
         $values = array_values($row);
         $values[] = $rc;
         // assign row count
         $values[] = $cssClass;
         // assign row css class
         $rt[] = str_replace($keys, $values, $t);
         // replace template fields {name:integer}
     }
     // return array or string - remove {tags}
     if ($rtArr) {
         return $rt;
     } else {
         $rt = implode($delimiter, $rt);
         return $removeTags ? preg_replace('/(\\{[a-zA-Z0-9._-]+\\})/', '', $rt) : $rt;
     }
 }
Exemplo n.º 3
0
 /**
  * Returns the non-html text from the callback value
  * @return string
  */
 public function textVal()
 {
     $s = Raxan::getSharedSanitizer();
     return $s->textVal($this->value);
 }