/** * Escapes a string, converting all HTML entities * into plain text equivalents. * @method text * @static * @param {string} $content The string to escape * @param {string} [$convert=array()] An array of additional characters to convert. Can include "\n" and " ". * @param {string} [$unconvert=array()] An array of from => to pairs to unconvert back. * @return {string} */ static function text($content, $convert = array(), $unconvert = array()) { if (!is_array($convert)) { $convert = array(); } $result = htmlentities(Q::t($content), ENT_QUOTES, 'UTF-8'); if ($convert or $unconvert) { $conversions = array("\n" => "<br>", " " => " "); foreach ($convert as $c) { $convert_to[] = $conversions[$c]; } foreach ($unconvert as $from => $to) { $convert[] = $from; $convert_to[] = $to; } $result = str_replace($convert, $convert_to, $result); } return $result; }
/** * Renders a date selector * @method date * @static * @param {string} $name The name of the input * @param {string} [$value=null] You can put a date here, as a string * @param {array} [$options=null] Options include the following: * * * "year_from" => the first year in the selector * * "year_to" => the last year in the selector * * @param {array} [$attributes=array()] An array of additional attributes to render. Consists of name => value pairs. * @return {string} The generated markup */ static function date($name, $value = null, $options = null, $attributes = array()) { if (empty($value)) { $value = null; } $id = isset($attributes['id']) ? $attributes['id'] : ''; $year_from = isset($options['year_from']) ? $options['year_from'] : 1900; $year_to = isset($options['year_to']) ? $options['year_to'] : date('Y'); for ($i = $year_to; $i >= $year_from; --$i) { $years[$i] = (string) $i; } $months = array("01" => 'January', "02" => 'February', "03" => 'March', "04" => 'April', "05" => 'May', "06" => 'June', "07" => 'July', "08" => 'August', "09" => 'September', "10" => 'October', "11" => 'November', "12" => 'December'); $days = array(); for ($i = 1; $i <= 31; ++$i) { $days[sprintf("%02d", $i)] = (string) $i; } if (!isset($value)) { $year = $month = $day = null; } else { $v = is_numeric($value) ? $value : strtotime($value); $dp = getdate($v); $year = isset($dp['year']) ? sprintf("%02d", $dp['year']) : null; $month = isset($dp['mon']) ? sprintf("%02d", $dp['mon']) : null; $day = isset($dp['mday']) ? sprintf("%02d", $dp['mday']) : null; } $attributes['name'] = $name . '_year'; if ($id) { $attributes['id'] = $id . '_year'; } $year_select = self::tag('select', $attributes) . self::options($years, $id, $year, Q::t('year')) . "</select>"; $attributes['name'] = $name . '_month'; if ($id) { $attributes['id'] = $id . '_month'; } $month_select = self::tag('select', $attributes) . self::options($months, $id, $month, Q::t('month')) . "</select>"; $attributes['name'] = $name . '_day'; if ($id) { $attributes['id'] = $id . '_day'; } $day_select = self::tag('select', $attributes) . self::options($days, $id, $day, Q::t('day')) . "</select>"; $language = Q::ifset($_SERVER, 'HTTP_ACCEPT_LANGUAGE', 'en-US'); $mdy_countries = array('en-us', 'en-bz'); if (in_array(strtolower(substr($language, 0, 5)), $mdy_countries) !== false) { return "{$month_select}{$day_select}{$year_select}"; } else { $ymd_countries = array('ch', 'ko', 'hu', 'fa', 'ja', 'lt', 'mn'); if (in_array(strtolower(substr($language, 0, 2)), $ymd_countries) !== false) { return "{$year_select}{$day_select}{$month_select}"; } else { return "{$day_select}{$month_select}{$year_select}"; } } }