function testSetLocale() { $ok = Raxan::setLocale('fr'); $this->ok($ok, 'Set new local'); $v = Raxan::locale('yes'); $this->compare($v, 'Oui', 'Change Locale to fr'); Raxan::setLocale('en'); }
/** * Parses a date string and returns an array containing the date parts otherwise false * It's works great with date values returned from MSSQL, MySQL and others. * @return Array Returns an array that contains the date parts: year, month, mday, minutes,hours and seconds * @param $str String Supported Date/Time string format */ public function parse($str) { $delim = ''; $dpart = array('minutes' => '', 'hour' => '', 'seconds' => ''); $dt = preg_replace('/(\\s)+/', ' ', $str); // remove extra white spaces if (strpos($dt, '-') > 0) { $delim = '-'; } if (strpos($dt, '/') > 0) { $delim = '/'; } if (!$delim && ($d = strpos($dt, '.')) > 0) { $c = strpos($dt, ':'); if (!$c || $c > $d) { $delim = '.'; } } if ($delim == '-' || $delim == '/' || $delim == '.') { @(list($date, $time) = explode(' ', $dt)); $date = explode($delim, $date); $date[] = $time; } else { $date = explode(' ', $dt, 4); } foreach ($date as $i => $v) { $date[$i] = trim(trim($v, ',')); } @(list($d1, $d2, $d3, $time) = $date); if (!self::$months) { self::$months = Raxan::locale('months.short'); } $months = self::$months; // get year if ($d1 > 1000) { $dpart['year'] = $d1; unset($date[0]); } if ($d3 > 1000) { $dpart['year'] = $d3; unset($date[2]); } if (!isset($dpart['year'])) { $dpart['year'] = date('Y'); } // get month - defaults to mm-dd-yyyy if (!is_numeric($d1)) { for ($i = 0; $i < 12; $i++) { // mmm dd yyyy if (stristr($d1, $months[$i]) != false) { $dpart['mon'] = $i + 1; unset($date[0]); break; } } } else { if (!is_numeric($d2)) { for ($i = 0; $i < 12; $i++) { if (stristr($d2, $months[$i]) != false) { $dpart['mon'] = $i + 1; unset($date[1]); break; } } } else { if ($d2 <= 12 && $d1 >= 1500) { $dpart['mon'] = $d2; unset($date[1]); } // yyyy-mm-dd if ($d1 <= 12 && $d3 >= 1500) { $dpart['mon'] = $d1; unset($date[0]); } else { if ($d1 > 12 && $d3 >= 1500) { $dpart['mon'] = $d2; unset($date[1]); } } // dd-mm-yyyy } } // get day unset($date[3]); $dpart['mday'] = implode('', $date); if (!is_numeric($dpart['mday']) || $dpart['mday'] > 31) { return false; } // get time info. use 1 jan 2008 as a starting date $t = strtotime('1-jan-2008 ' . $time); if ($t) { $t = getdate($t); $dpart['hours'] = $t['hours']; $dpart['minutes'] = $t['minutes']; $dpart['seconds'] = $t['seconds']; } return $dpart; }
/** * Returns formatted number value based on locale settings * @param string $key Key name or input value (direct input must be enabled) * @param int $decimal Optional. Total number of decimal places to return * @return string If input is an array then an array of formatted numeric values is returned */ public function formatNumber($key, $decimal = null) { $ds = Raxan::locale('decimal.separator'); $ts = Raxan::locale('thousand.separator'); $v = $this->value($key); if (!is_array($v)) { $v = is_numeric($v) ? number_format($v, $decimal, $ds, $ts) : ''; } else { foreach ($v as $k => $b) { $v[$k] = is_numeric($b) ? number_format($b, $decimal, $ds, $ts) : ''; } } return $v; }
/** * Check validity on form fields. * Supports basic HTML5 validation for email, date, number and url input types. * The pattern, required, min and max attributes are also supported. Currenly min/max is only supported for numeric inputs (type=number) * @param boolean $markInvalid Optional. Mark invalid form fields * @param string $cssClass Optional. Required class * @param mixed $callback Optional. Custom validation callback */ public function checkValidity($markInvalid = false, $cssClass = null, $callback = null) { $errors = $values = array(); $page = $this->doc->page; $xpath = '//*[@name][not(descendant-or-self::*[@disabled])]'; // find form elements that are not disabled $isAjax = $page->isAjaxRequest; $fields = $this->findByXPath($xpath, true); $sanitize = Raxan::getSharedSanitizer(); $fn = $callback && is_callable($callback) ? $callback : null; if ($fn) { $callByArray = is_array($fn); } $cssClass = $cssClass ? $cssClass : 'required'; foreach ($fields as $fld) { $invalid = ''; $rt = null; $pattern = $fld->getAttribute("pattern"); $required = $fld->hasAttribute("required"); $maxlength = $fld->getAttribute("maxlength"); $checked = $fld->hasAttribute("checked"); $novalidate = $fld->hasAttribute("novalidate"); $min = $fld->getAttribute("min"); $max = $fld->getAttribute("max"); $id = $fld->getAttribute("id"); if ($novalidate) { continue; } $name = $fld->getAttribute("name"); if (($lbPos = strpos($name, '[')) !== false) { $name = substr($name, 0, $lbPos); } // remove [] from name $type = $inpType = $fld->getAttribute("type"); if ($type == '') { $type = $fld->nodeName; } $type = trim(strtolower($type)); if ($type == 'button' || $type == 'submit' || $type == 'image' || $type == 'object') { continue; } if ($type == 'textarea') { $value = $page->nodeContent($fld); } else { if ($type == 'select') { $oElm = $this->elms; $this->elms = array($fld); $value = $this->val(); // get selected values if ($lbPos && is_array($value)) { $lbPos = false; } // set lbPos so it's not detected when storeing $value $this->elms = $oElm; } else { $value = $fld->getAttribute("value"); } } // value missing if ($required && ($value == '' || ($type == 'checkbox' || $type == 'radio') && ($checked != 'checked' && !isset($_POST[$name])))) { $invalid = 'valueMissing'; } // pattern mismatch if ($pattern && !preg_match($pattern, (string) $value)) { $invalid = 'patternMismatch'; } // too long - maxlength if ($maxlength && ($type == 'text' || $type == 'textarea') && strlen((string) $value) > $maxlength) { $invalid = 'tooLong'; } // type mismatched if ($value !== '' && ($type == 'email' && !$sanitize->isEmail($value) || $type == 'url' && !$sanitize->isUrl($value) || $type == 'number' && !$sanitize->isNumeric($value) || $type == 'date' && !$sanitize->isDate($value, 'Y-m-d') || $type == 'month' && !$sanitize->isDate($value, 'Y-m'))) { $invalid = 'typeMismatch'; } // range - min / max if ($value && $type == 'number' && !$invalid) { if ($min && $value < $min) { $invalid = 'rangeUnderflow'; } if ($max && $value > $max) { $invalid = 'rangeOverflow'; } } // callback if ($fn) { if (!$callByArray) { $rt = $fn($fld, $name, $value, $markInvalid, $invalid); } else { $rt = $fn[0]->{$fn[1]}($fld, $name, $value, $markInvalid, $invalid); } // object callback // $rt returned values: false (invalid), true (valid) or string (custom message) if ($rt === false || is_string($rt)) { $this->_customValidity[$name] = $rt || 'customError'; } } // custom error if (isset($this->_customValidity[$name])) { $invalid = $this->_customValidity[$name]; } // store input error message if ($invalid) { $value = htmlspecialchars($value); $errors[$name] = ($msg = Raxan::locale($invalid, $value)) ? $msg : $invalid; } else { // store input values if ($type == 'select' && !$value) { $value = null; } else { if (($inpType == 'checkbox' || $inpType == 'radio') && $checked == '') { $value = null; } else { if (!isset($values[$name])) { $values[$name] = $lbPos ? array($value) : $value; } else { // append multiple values if (!is_array($values[$name])) { $values[$name] = array($values[$name]); } $values[$name][] = $value; } } } } // mark invalid fields if ($markInvalid) { if ($invalid) { if ($id && $isAjax) { c("#{$id}")->addClass($cssClass); } else { $fld->setAttribute('class', $fld->getAttribute('class') . ' ' . $cssClass); } } else { if ($id && $isAjax) { c("#{$id}")->removeClass($cssClass); } } } } $this->_validInp = $values; $this->_invalidInp = $errors; return count($errors) == 0 ? true : false; }
/** * Localize nodes */ public static function nodeL10n($nodes) { if ($nodes) { foreach ($nodes as $n) { $id = $n->getAttribute('langid'); $v = Raxan::locale($id); if ($v) { if (!is_array($v)) { if ($n->nodeName == 'input') { $n->setAttribute("value", $v); } else { $n->nodeValue = $v; } } else { foreach ($v as $name => $attr) { if ($name == 'html') { $n->nodeValue = $v; } else { $n->setAttribute($name, $attr); } } } if ($id) { $n->removeAttribute('langid'); } } } } }