Ejemplo n.º 1
0
 protected function getInput()
 {
     // Initialize some field attributes.
     $format = $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
     // Build the attributes array.
     $attributes = array();
     if ($this->element['size']) {
         $attributes['size'] = (int) $this->element['size'];
     }
     if ($this->element['maxlength']) {
         $attributes['maxlength'] = (int) $this->element['maxlength'];
     }
     if ($this->element['class']) {
         $attributes['class'] = (string) $this->element['class'];
     }
     if ((string) $this->element['readonly'] == 'true') {
         $attributes['readonly'] = 'readonly';
     }
     if ((string) $this->element['disabled'] == 'true') {
         $attributes['disabled'] = 'disabled';
     }
     if ($this->element['onchange']) {
         $attributes['onchange'] = (string) $this->element['onchange'];
     }
     // Handle the special case for "now".
     if (strtoupper($this->value) == 'NOW') {
         $this->value = strftime($format);
     }
     // Get some system objects.
     $config = MFactory::getConfig();
     $user = MFactory::getUser();
     // If a known filter is given use it.
     switch (strtoupper((string) $this->element['filter'])) {
         case 'SERVER_UTC':
             // Convert a date to UTC based on the server timezone.
             if (intval($this->value)) {
                 // Get a date object based on the correct timezone.
                 $date = MFactory::getDate($this->value, 'UTC');
                 $date->setTimezone(new DateTimeZone($config->get('offset')));
                 // Transform the date string.
                 $this->value = $date->format('Y-m-d H:i:s', true, false);
             }
             break;
         case 'USER_UTC':
             // Convert a date to UTC based on the user timezone.
             if (intval($this->value)) {
                 // Get a date object based on the correct timezone.
                 $date = MFactory::getDate($this->value, 'UTC');
                 $date->setTimezone(new DateTimeZone($user->getParam('timezone', $config->get('offset'))));
                 // Transform the date string.
                 $this->value = $date->format('Y-m-d H:i:s', true, false);
             }
             break;
     }
     return MHtml::_('calendar', $this->value, $this->name, $this->id, $format, $attributes);
 }
Ejemplo n.º 2
0
 public static function published($value, $i, $prefix = '', $enabled = true, $checkbox = 'cb', $publish_up = null, $publish_down = null)
 {
     if (is_array($prefix)) {
         $options = $prefix;
         $enabled = array_key_exists('enabled', $options) ? $options['enabled'] : $enabled;
         $checkbox = array_key_exists('checkbox', $options) ? $options['checkbox'] : $checkbox;
         $prefix = array_key_exists('prefix', $options) ? $options['prefix'] : '';
     }
     $states = array(1 => array('unpublish', 'MPUBLISHED', 'MLIB_HTML_UNPUBLISH_ITEM', 'MPUBLISHED', false, 'publish', 'publish'), 0 => array('publish', 'MUNPUBLISHED', 'MLIB_HTML_PUBLISH_ITEM', 'MUNPUBLISHED', false, 'unpublish', 'unpublish'), 2 => array('unpublish', 'MARCHIVED', 'MLIB_HTML_UNPUBLISH_ITEM', 'MARCHIVED', false, 'archive', 'archive'), -2 => array('publish', 'MTRASHED', 'MLIB_HTML_PUBLISH_ITEM', 'MTRASHED', false, 'trash', 'trash'));
     // Special state for dates
     if ($publish_up || $publish_down) {
         $nullDate = MFactory::getDBO()->getNullDate();
         $nowDate = MFactory::getDate()->toUnix();
         $tz = new DateTimeZone(MFactory::getUser()->getParam('timezone', MFactory::getConfig()->get('offset')));
         $publish_up = $publish_up != $nullDate ? MFactory::getDate($publish_up, 'UTC')->setTimeZone($tz) : false;
         $publish_down = $publish_down != $nullDate ? MFactory::getDate($publish_down, 'UTC')->setTimeZone($tz) : false;
         // Create tip text, only we have publish up or down settings
         $tips = array();
         if ($publish_up) {
             $tips[] = MText::sprintf('MLIB_HTML_PUBLISHED_START', $publish_up->format(MDate::$format, true));
         }
         if ($publish_down) {
             $tips[] = MText::sprintf('MLIB_HTML_PUBLISHED_FINISHED', $publish_down->format(MDate::$format, true));
         }
         $tip = empty($tips) ? false : implode('<br/>', $tips);
         // Add tips and special titles
         foreach ($states as $key => $state) {
             // Create special titles for published items
             if ($key == 1) {
                 $states[$key][2] = $states[$key][3] = 'MLIB_HTML_PUBLISHED_ITEM';
                 if ($publish_up > $nullDate && $nowDate < $publish_up->toUnix()) {
                     $states[$key][2] = $states[$key][3] = 'MLIB_HTML_PUBLISHED_PENDING_ITEM';
                     $states[$key][5] = $states[$key][6] = 'pending';
                 }
                 if ($publish_down > $nullDate && $nowDate > $publish_down->toUnix()) {
                     $states[$key][2] = $states[$key][3] = 'MLIB_HTML_PUBLISHED_EXPIRED_ITEM';
                     $states[$key][5] = $states[$key][6] = 'expired';
                 }
             }
             // Add tips to titles
             if ($tip) {
                 $states[$key][1] = MText::_($states[$key][1]);
                 $states[$key][2] = MText::_($states[$key][2]) . '::' . $tip;
                 $states[$key][3] = MText::_($states[$key][3]) . '::' . $tip;
                 $states[$key][4] = true;
             }
         }
         return self::state($states, $value, $i, array('prefix' => $prefix, 'translate' => !$tip), $enabled, true, $checkbox);
     }
     return self::state($states, $value, $i, $prefix, $enabled, true, $checkbox);
 }
Ejemplo n.º 3
0
 public static function relative($date, $unit = null, $time = null)
 {
     if (is_null($time)) {
         // Get now
         $time = MFactory::getDate('now');
     }
     // Get the difference in seconds between now and the time
     $diff = strtotime($time) - strtotime($date);
     // Less than a minute
     if ($diff < 60) {
         return MText::_('MLIB_HTML_DATE_RELATIVE_LESSTHANAMINUTE');
     }
     // Round to minutes
     $diff = round($diff / 60);
     // 1 to 59 minutes
     if ($diff < 60 || $unit == 'minute') {
         return MText::plural('MLIB_HTML_DATE_RELATIVE_MINUTES', $diff);
     }
     // Round to hours
     $diff = round($diff / 60);
     // 1 to 23 hours
     if ($diff < 24 || $unit == 'hour') {
         return MText::plural('MLIB_HTML_DATE_RELATIVE_HOURS', $diff);
     }
     // Round to days
     $diff = round($diff / 24);
     // 1 to 6 days
     if ($diff < 7 || $unit == 'day') {
         return MText::plural('MLIB_HTML_DATE_RELATIVE_DAYS', $diff);
     }
     // Round to weeks
     $diff = round($diff / 7);
     // 1 to 4 weeks
     if ($diff <= 4 || $unit == 'week') {
         return MText::plural('MLIB_HTML_DATE_RELATIVE_WEEKS', $diff);
     }
     // Over a month, return the absolute time
     return MHtml::_('date', $date);
 }
Ejemplo n.º 4
0
 protected function filterField($element, $value)
 {
     // Make sure there is a valid SimpleXMLElement.
     if (!$element instanceof SimpleXMLElement) {
         return false;
     }
     // Get the field filter type.
     $filter = (string) $element['filter'];
     // Process the input value based on the filter.
     $return = null;
     switch (strtoupper($filter)) {
         // Access Control Rules.
         case 'RULES':
             $return = array();
             foreach ((array) $value as $action => $ids) {
                 // Build the rules array.
                 $return[$action] = array();
                 foreach ($ids as $id => $p) {
                     if ($p !== '') {
                         $return[$action][$id] = $p == '1' || $p == 'true' ? true : false;
                     }
                 }
             }
             break;
             // Do nothing, thus leaving the return value as null.
         // Do nothing, thus leaving the return value as null.
         case 'UNSET':
             break;
             // No Filter.
         // No Filter.
         case 'RAW':
             $return = $value;
             break;
             // Filter the input as an array of integers.
         // Filter the input as an array of integers.
         case 'INT_ARRAY':
             // Make sure the input is an array.
             if (is_object($value)) {
                 $value = get_object_vars($value);
             }
             $value = is_array($value) ? $value : array($value);
             MArrayHelper::toInteger($value);
             $return = $value;
             break;
             // Filter safe HTML.
         // Filter safe HTML.
         case 'SAFEHTML':
             $return = MFilterInput::getInstance(null, null, 1, 1)->clean($value, 'string');
             break;
             // Convert a date to UTC based on the server timezone offset.
         // Convert a date to UTC based on the server timezone offset.
         case 'SERVER_UTC':
             if (intval($value) > 0) {
                 // Get the server timezone setting.
                 $offset = MFactory::getConfig()->get('offset');
                 // Return an SQL formatted datetime string in UTC.
                 $return = MFactory::getDate($value, $offset)->toSql();
             } else {
                 $return = '';
             }
             break;
             // Convert a date to UTC based on the user timezone offset.
         // Convert a date to UTC based on the user timezone offset.
         case 'USER_UTC':
             if (intval($value) > 0) {
                 // Get the user timezone setting defaulting to the server timezone setting.
                 $offset = MFactory::getUser()->getParam('timezone', MFactory::getConfig()->get('offset'));
                 // Return a MySQL formatted datetime string in UTC.
                 $return = MFactory::getDate($value, $offset)->toSql();
             } else {
                 $return = '';
             }
             break;
             // Ensures a protocol is present in the saved field. Only use when
             // the only permitted protocols requre '://'. See MFormRuleUrl for list of these.
         // Ensures a protocol is present in the saved field. Only use when
         // the only permitted protocols requre '://'. See MFormRuleUrl for list of these.
         case 'URL':
             if (empty($value)) {
                 return false;
             }
             $value = MFilterInput::getInstance()->clean($value, 'html');
             $value = trim($value);
             // <>" are never valid in a uri see http://www.ietf.org/rfc/rfc1738.txt.
             $value = str_replace(array('<', '>', '"'), '', $value);
             // Check for a protocol
             $protocol = parse_url($value, PHP_URL_SCHEME);
             // If there is no protocol and the relative option is not specified,
             // we assume that it is an external URL and prepend http://.
             if ($element['type'] == 'url' && !$protocol && !$element['relative'] || !$element['type'] == 'url' && !$protocol) {
                 $protocol = 'http';
                 // If it looks like an internal link, then add the root.
                 if (substr($value, 0) == 'index.php') {
                     $value = MURI::root() . $value;
                 }
                 // Otherwise we treat it is an external link.
                 // Put the url back together.
                 $value = $protocol . '://' . $value;
             } elseif (!$protocol && $element['relative']) {
                 $host = MURI::getInstance('SERVER')->gethost();
                 // If it starts with the host string, just prepend the protocol.
                 if (substr($value, 0) == $host) {
                     $value = 'http://' . $value;
                 } else {
                     $value = MURI::root() . $value;
                 }
             }
             $return = $value;
             break;
         case 'TEL':
             $value = trim($value);
             // Does it match the NANP pattern?
             if (preg_match('/^(?:\\+?1[-. ]?)?\\(?([2-9][0-8][0-9])\\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/', $value) == 1) {
                 $number = (string) preg_replace('/[^\\d]/', '', $value);
                 if (substr($number, 0, 1) == 1) {
                     $number = substr($number, 1);
                 }
                 if (substr($number, 0, 2) == '+1') {
                     $number = substr($number, 2);
                 }
                 $result = '1.' . $number;
             } elseif (preg_match('/^\\+(?:[0-9] ?){6,14}[0-9]$/', $value) == 1) {
                 $countrycode = substr($value, 0, strpos($value, ' '));
                 $countrycode = (string) preg_replace('/[^\\d]/', '', $countrycode);
                 $number = strstr($value, ' ');
                 $number = (string) preg_replace('/[^\\d]/', '', $number);
                 $result = $countrycode . '.' . $number;
             } elseif (preg_match('/^\\+[0-9]{1,3}\\.[0-9]{4,14}(?:x.+)?$/', $value) == 1) {
                 if (strstr($value, 'x')) {
                     $xpos = strpos($value, 'x');
                     $value = substr($value, 0, $xpos);
                 }
                 $result = str_replace('+', '', $value);
             } elseif (preg_match('/[0-9]{1,3}\\.[0-9]{4,14}$/', $value) == 1) {
                 $result = $value;
             } else {
                 $value = (string) preg_replace('/[^\\d]/', '', $value);
                 if ($value != null && strlen($value) <= 15) {
                     $length = strlen($value);
                     // if it is fewer than 13 digits assume it is a local number
                     if ($length <= 12) {
                         $result = '.' . $value;
                     } else {
                         // If it has 13 or more digits let's make a country code.
                         $cclen = $length - 12;
                         $result = substr($value, 0, $cclen) . '.' . substr($value, $cclen);
                     }
                 } else {
                     $result = '';
                 }
             }
             $return = $result;
             break;
         default:
             // Check for a callback filter.
             if (strpos($filter, '::') !== false && is_callable(explode('::', $filter))) {
                 $return = call_user_func(explode('::', $filter), $value);
             } elseif (function_exists($filter)) {
                 $return = call_user_func($filter, $value);
             } else {
                 $return = MFilterInput::getInstance()->clean($value, $filter);
             }
             break;
     }
     return $return;
 }
Ejemplo n.º 5
0
 public function checkOut($userId, $pk = null)
 {
     // If there is no checked_out or checked_out_time field, just return true.
     if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time')) {
         return true;
     }
     // Initialise variables.
     $k = $this->_tbl_key;
     $pk = is_null($pk) ? $this->{$k} : $pk;
     // If no primary key is given, return false.
     if ($pk === null) {
         $e = new MException(MText::_('MLIB_DATABASE_ERROR_NULL_PRIMARY_KEY'));
         $this->setError($e);
         return false;
     }
     // Get the current time in MySQL format.
     $time = MFactory::getDate()->toSql();
     // Check the row out by primary key.
     $query = $this->_db->getQuery(true);
     $query->update($this->_tbl);
     $query->set($this->_db->quoteName('checked_out') . ' = ' . (int) $userId);
     $query->set($this->_db->quoteName('checked_out_time') . ' = ' . $this->_db->quote($time));
     $query->where($this->_tbl_key . ' = ' . $this->_db->quote($pk));
     $this->_db->setQuery($query);
     if (!$this->_db->execute()) {
         $e = new MException(MText::sprintf('MLIB_DATABASE_ERROR_CHECKOUT_FAILED', get_class($this), $this->_db->getErrorMsg()));
         $this->setError($e);
         return false;
     }
     // Set table values in the object.
     $this->checked_out = (int) $userId;
     $this->checked_out_time = $time;
     return true;
 }
Ejemplo n.º 6
0
 public static function date($input = 'now', $format = null, $tz = true, $gregorian = false)
 {
     // Get some system objects.
     if (!($offset = MFactory::getWOption('timezone_string'))) {
         $offset = self::getTimezoneString();
     }
     $user = MFactory::getUser();
     // UTC date converted to user time zone.
     if ($tz === true) {
         // Get a date object based on UTC.
         $date = MFactory::getDate($input, 'UTC');
         // Set the correct time zone based on the user configuration.
         $date->setTimeZone(new DateTimeZone($user->getParam('timezone', $offset)));
     } elseif ($tz === false) {
         // Get a date object based on UTC.
         $date = MFactory::getDate($input, 'UTC');
         // Set the correct time zone based on the server configuration.
         $date->setTimeZone(new DateTimeZone($offset));
     } elseif ($tz === null) {
         $date = MFactory::getDate($input);
     } else {
         // Get a date object based on UTC.
         $date = MFactory::getDate($input, 'UTC');
         // Set the correct time zone based on the server configuration.
         $date->setTimeZone(new DateTimeZone($tz));
     }
     // If no format is given use the default locale based format.
     if (!$format) {
         $format = MText::_('DATE_FORMAT_LC1');
     } elseif (MFactory::getLanguage()->hasKey($format)) {
         $format = MText::_($format);
     }
     if ($gregorian) {
         return $date->format($format, true);
     } else {
         return $date->calendar($format, true);
     }
 }