Example #1
0
 /**
  * Formats the given date and time `$string` based on the given `$format`.
  * Optionally the result will be localized and respect a timezone differing
  * from the system default. The default output is ISO 8601.
  *
  * @since Symphony 2.2.1
  * @param string $string (optional)
  *  A string containing date and time, defaults to the current date and time
  * @param string $format (optional)
  *  A valid PHP date format, defaults to ISO 8601
  * @param boolean $localize (optional)
  *  Localizes the output, if true, defaults to true
  * @param string $timezone (optional)
  *  The timezone associated with the timestamp
  * @return string|boolean
  *  The formatted date, or if the date could not be parsed, false.
  */
 public static function format($string = 'now', $format = DateTime::ISO8601, $localize = true, $timezone = null)
 {
     // Parse date
     $date = self::parse($string);
     if ($date === false) {
         return false;
     }
     // Timezone
     // If a timezone was given, apply it
     if (!is_null($timezone)) {
         $date->setTimezone(new DateTimeZone($timezone));
         // No timezone given, apply the default timezone
     } elseif (isset(self::$settings['timezone'])) {
         $date->setTimezone(new DateTimeZone(self::$settings['timezone']));
     }
     // Format date
     $date = $date->format($format);
     // Localize date
     // Convert date string from English back to the activated Language
     if ($localize === true) {
         $date = Lang::localizeDate($date);
     }
     // Return custom formatted date, use ISO 8601 date by default
     return $date;
 }
Example #2
0
 /**
  * Generates a XMLElement representation of a `<time>`
  *
  * @since Symphony 2.3
  * @param string $string
  *  A string containing date and time, defaults to the current date and time
  * @param string $format (optional)
  *  A valid PHP date format, defaults to `__SYM_TIME_FORMAT__`
  * @param boolean $pubdate (optional)
  *  A flag to make the given date a publish date
  * @return XMLElement
  */
 public static function Time($string, $format = __SYM_TIME_FORMAT__, $pubdate = false)
 {
     // Parse date
     $date = DateTimeObj::parse($string);
     // Create element
     $obj = new XMLElement('time', Lang::localizeDate($date->format($format)));
     $obj->setAttribute('datetime', $date->format(DateTime::ISO8601));
     $obj->setAttribute('utc', $date->format('U'));
     // Pubdate?
     if ($pubdate === true) {
         $obj->setAttribute('pubdate', 'pubdate');
     }
     return $obj;
 }
 /**
  * Formats the given date and time `$string` based on the given `$format`.
  * Optionally the result will be localized and respect a timezone differing
  * from the system default. The default output is ISO 8601.
  * Please note that for best compatibility with European dates it is recommended
  * that your site be in a PHP5.3 environment.
  *
  * @since Symphony 2.2.1
  * @param string $string (optional)
  *  A string containing date and time, defaults to the current date and time
  * @param string $format (optional)
  *  A valid PHP date format, defaults to ISO 8601
  * @param boolean $localize (optional)
  *  Localizes the output, if true, defaults to true
  * @param string $timezone (optional)
  *  The timezone associated with the timestamp
  * @return string|boolean
  *  The formatted date, of if the date could not be parsed, false.
  */
 public static function format($string = 'now', $format = DateTime::ISO8601, $localize = true, $timezone = null)
 {
     // Current date and time
     if ($string == 'now' || empty($string)) {
         $date = new DateTime();
     } elseif (is_numeric($string)) {
         $date = new DateTime('@' . $string);
     } else {
         // Standardize date
         // Convert date string to English
         $string = Lang::standardizeDate($string);
         // PHP 5.3: Apply Symphony date format using `createFromFormat`
         if (method_exists('DateTime', 'createFromFormat')) {
             $date = DateTime::createFromFormat(self::$settings['datetime_format'], $string);
             if ($date === false) {
                 $date = DateTime::createFromFormat(self::$settings['date_format'], $string);
             }
             // Handle dates that are in a different format to Symphony's config
             // DateTime is much the same as `strtotime` and will handle relative
             // dates.
             if ($date === false) {
                 try {
                     $date = new DateTime($string);
                 } catch (Exception $ex) {
                     // Invalid date, it can't be parsed
                     return false;
                 }
             }
         } else {
             try {
                 $date = new DateTime($string);
             } catch (Exception $ex) {
                 // Invalid date, it can't be parsed
                 return false;
             }
         }
         // If the date is still invalid, just return false.
         if ($date === false) {
             return false;
         }
     }
     // Timezone
     // If a timezone was given, apply it
     if ($timezone !== null) {
         $date->setTimezone(new DateTimeZone($timezone));
     } else {
         if (isset(self::$settings['timezone'])) {
             $date->setTimezone(new DateTimeZone(self::$settings['timezone']));
         }
     }
     // Format date
     $date = $date->format($format);
     // Localize date
     // Convert date string from English back to the activated Language
     if ($localize === true) {
         $date = Lang::localizeDate($date);
     }
     // Return custom formatted date, use ISO 8601 date by default
     return $date;
 }
 /**
  * This function contains the minimal amount of logic for generating the
  * index table of a given `$resource_type`. The table has name, source, pages
  * release date and author columns. The values for these columns are determined
  * by the resource's `about()` method.
  *
  * As Datasources types can be installed using Providers, the Source column
  * can be overridden with a Datasource's `getSourceColumn` method (if it exists).
  *
  * @param integer $resource_type
  *  Either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DATASOURCE`
  */
 public function __viewIndex($resource_type)
 {
     $manager = ResourceManager::getManagerFromType($resource_type);
     $this->setPageType('table');
     Sortable::initialize($this, $resources, $sort, $order, array('type' => $resource_type));
     $columns = array(array('label' => __('Name'), 'sortable' => true, 'handle' => 'name'), array('label' => __('Source'), 'sortable' => true, 'handle' => 'source'), array('label' => __('Pages'), 'sortable' => false), array('label' => __('Release Date'), 'sortable' => true, 'handle' => 'release-date'), array('label' => __('Author'), 'sortable' => true, 'handle' => 'author'));
     $aTableHead = Sortable::buildTableHeaders($columns, $sort, $order, isset($_REQUEST['filter']) ? '&amp;filter=' . $_REQUEST['filter'] : '');
     $aTableBody = array();
     if (!is_array($resources) || empty($resources)) {
         $aTableBody = array(Widget::TableRow(array(Widget::TableData(__('None found.'), 'inactive', NULL, count($aTableHead))), 'odd'));
     } else {
         foreach ($resources as $r) {
             // Resource name
             $action = isset($r['can_parse']) && $r['can_parse'] === true ? 'edit' : 'info';
             $name = Widget::TableData(Widget::Anchor($r['name'], SYMPHONY_URL . $_REQUEST['symphony-page'] . $action . '/' . $r['handle'] . '/', $r['handle']));
             // Resource type/source
             if (isset($r['source'], $r['source']['id'])) {
                 $section = Widget::TableData(Widget::Anchor($r['source']['name'], SYMPHONY_URL . '/blueprints/sections/edit/' . $r['source']['id'] . '/', $r['source']['handle']));
             } else {
                 if (isset($r['source']) && class_exists($r['source']['name']) && method_exists($r['source']['name'], 'getSourceColumn')) {
                     $class = call_user_func(array($manager, '__getClassName'), $r['handle']);
                     $section = Widget::TableData(call_user_func(array($class, 'getSourceColumn'), $r['handle']));
                 } else {
                     if (isset($r['source'], $r['source']['name'])) {
                         $section = Widget::TableData($r['source']['name']);
                     } else {
                         $section = Widget::TableData(__('Unknown'), 'inactive');
                     }
                 }
             }
             // Attached pages
             $pages = ResourceManager::getAttachedPages($resource_type, $r['handle']);
             $pagelinks = array();
             $i = 0;
             foreach ($pages as $p) {
                 ++$i;
                 $pagelinks[] = Widget::Anchor($p['title'], SYMPHONY_URL . '/blueprints/pages/edit/' . $p['id'] . '/')->generate() . (count($pages) > $i ? $i % 10 == 0 ? '<br />' : ', ' : '');
             }
             $pages = implode('', $pagelinks);
             if ($pages == '') {
                 $pagelinks = Widget::TableData(__('None'), 'inactive');
             } else {
                 $pagelinks = Widget::TableData($pages, 'pages');
             }
             // Release date
             $releasedate = Widget::TableData(Lang::localizeDate(DateTimeObj::format($r['release-date'], __SYM_DATETIME_FORMAT__)));
             // Authors
             $author = $r['author']['name'];
             if ($author) {
                 if (isset($r['author']['website'])) {
                     $author = Widget::Anchor($r['author']['name'], General::validateURL($r['author']['website']));
                 } else {
                     if (isset($r['author']['email'])) {
                         $author = Widget::Anchor($r['author']['name'], 'mailto:' . $r['author']['email']);
                     }
                 }
             }
             $author = Widget::TableData($author);
             $author->appendChild(Widget::Input('items[' . $r['handle'] . ']', null, 'checkbox'));
             $aTableBody[] = Widget::TableRow(array($name, $section, $pagelinks, $releasedate, $author));
         }
     }
     $table = Widget::Table(Widget::TableHead($aTableHead), NULL, Widget::TableBody($aTableBody), 'selectable');
     $this->Form->appendChild($table);
     $tableActions = new XMLElement('div');
     $tableActions->setAttribute('class', 'actions');
     $options = array(array(NULL, false, __('With Selected...')), array('delete', false, __('Delete'), 'confirm'));
     $pages = $this->pagesFlatView();
     $group_attach = array('label' => __('Attach to Page'), 'options' => array());
     $group_detach = array('label' => __('Detach from Page'), 'options' => array());
     $group_attach['options'][] = array('attach-all-pages', false, __('All'));
     $group_detach['options'][] = array('detach-all-pages', false, __('All'));
     foreach ($pages as $p) {
         $group_attach['options'][] = array('attach-to-page-' . $p['id'], false, $p['title']);
         $group_detach['options'][] = array('detach-from-page-' . $p['id'], false, $p['title']);
     }
     $options[] = $group_attach;
     $options[] = $group_detach;
     /**
      * Allows an extension to modify the existing options for this page's
      * With Selected menu. If the `$options` parameter is an empty array,
      * the 'With Selected' menu will not be rendered.
      *
      * @delegate AddCustomActions
      * @since Symphony 2.3.2
      * @param string $context
      * '/blueprints/datasources/' or '/blueprints/events/'
      * @param array $options
      *  An array of arrays, where each child array represents an option
      *  in the With Selected menu. Options should follow the same format
      *  expected by `Widget::__SelectBuildOption`. Passed by reference.
      */
     Symphony::ExtensionManager()->notifyMembers('AddCustomActions', $_REQUEST['symphony-page'], array('options' => &$options));
     if (!empty($options)) {
         $tableActions->appendChild(Widget::Apply($options));
         $this->Form->appendChild($tableActions);
     }
 }
 /**
  * This function contains the minimal amount of logic for generating the
  * index table of a given `$resource_type`. The table has name, source, pages
  * release date and author columns. The values for these columns are determined
  * by the resource's `about()` method.
  *
  * As Datasources types can be installed using Providers, the Source column
  * can be overridden with a Datasource's `getSourceColumn` method (if it exists).
  *
  * @param integer $resource_type
  *  Either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DATASOURCE`
  */
 public function __viewIndex($resource_type)
 {
     $manager = ResourceManager::getManagerFromType($resource_type);
     $this->setPageType('table');
     Sortable::initialize($this, $resources, $sort, $order, array('type' => $resource_type));
     $columns = array(array('label' => __('Name'), 'sortable' => true, 'handle' => 'name'), array('label' => __('Source'), 'sortable' => true, 'handle' => 'source'), array('label' => __('Pages'), 'sortable' => false), array('label' => __('Release Date'), 'sortable' => true, 'handle' => 'release-date'), array('label' => __('Author'), 'sortable' => true, 'handle' => 'author'));
     $aTableHead = Sortable::buildTableHeaders($columns, $sort, $order, isset($_REQUEST['filter']) ? '&amp;filter=' . $_REQUEST['filter'] : '');
     $aTableBody = array();
     if (!is_array($resources) || empty($resources)) {
         $aTableBody = array(Widget::TableRow(array(Widget::TableData(__('None found.'), 'inactive', NULL, count($aTableHead))), 'odd'));
     } else {
         foreach ($resources as $r) {
             // Resource name
             $action = $r['can_parse'] ? 'edit' : 'info';
             $name = Widget::TableData(Widget::Anchor($r['name'], SYMPHONY_URL . $_REQUEST['symphony-page'] . $action . '/' . $r['handle'] . '/', $r['handle']));
             // Resource type/source
             if (isset($r['source']['id'])) {
                 $section = Widget::TableData(Widget::Anchor($r['source']['name'], SYMPHONY_URL . '/blueprints/sections/edit/' . $r['source']['id'] . '/', $r['source']['handle']));
             } else {
                 if (class_exists($r['source']['name']) && method_exists($r['source']['name'], 'getSourceColumn')) {
                     $class = call_user_func(array($manager, '__getClassName'), $r['handle']);
                     $section = Widget::TableData(call_user_func(array($class, 'getSourceColumn'), $r['handle']));
                 } else {
                     if (isset($r['source']['name'])) {
                         $section = Widget::TableData($r['source']['name']);
                     } else {
                         $section = Widget::TableData(__('Unknown'), 'inactive');
                     }
                 }
             }
             // Attached pages
             $pages = ResourceManager::getAttachedPages($resource_type, $r['handle']);
             $pagelinks = array();
             $i = 0;
             foreach ($pages as $p) {
                 ++$i;
                 $pagelinks[] = Widget::Anchor($p['title'], SYMPHONY_URL . '/blueprints/pages/edit/' . $p['id'] . '/')->generate() . (count($pages) > $i ? $i % 10 == 0 ? '<br />' : ', ' : '');
             }
             $pages = implode('', $pagelinks);
             if ($pages == '') {
                 $pagelinks = Widget::TableData(__('None'), 'inactive');
             } else {
                 $pagelinks = Widget::TableData($pages, 'pages');
             }
             // Release date
             $releasedate = Widget::TableData(Lang::localizeDate(DateTimeObj::format($r['release-date'], __SYM_DATETIME_FORMAT__)));
             // Authors
             $author = $r['author']['name'];
             if (isset($r['author']['website'])) {
                 $author = Widget::Anchor($r['author']['name'], General::validateURL($r['author']['website']));
             } else {
                 if (isset($r['author']['email'])) {
                     $author = Widget::Anchor($r['author']['name'], 'mailto:' . $r['author']['email']);
                 }
             }
             $author = Widget::TableData($author);
             $author->appendChild(Widget::Input('items[' . $r['handle'] . ']', null, 'checkbox'));
             $aTableBody[] = Widget::TableRow(array($name, $section, $pagelinks, $releasedate, $author));
         }
     }
     $table = Widget::Table(Widget::TableHead($aTableHead), NULL, Widget::TableBody($aTableBody), 'selectable');
     $this->Form->appendChild($table);
     $tableActions = new XMLElement('div');
     $tableActions->setAttribute('class', 'actions');
     $options = array(array(NULL, false, __('With Selected...')), array('delete', false, __('Delete'), 'confirm'));
     $pages = $this->pagesFlatView();
     $group_attach = array('label' => __('Attach to Page'), 'options' => array());
     $group_detach = array('label' => __('Detach from Page'), 'options' => array());
     $group_attach['options'][] = array('attach-all-pages', false, __('All'));
     $group_detach['options'][] = array('detach-all-pages', false, __('All'));
     foreach ($pages as $p) {
         $group_attach['options'][] = array('attach-to-page-' . $p['id'], false, $p['title']);
         $group_detach['options'][] = array('detach-from-page-' . $p['id'], false, $p['title']);
     }
     $options[] = $group_attach;
     $options[] = $group_detach;
     $tableActions->appendChild(Widget::Apply($options));
     $this->Form->appendChild($tableActions);
 }
 /**
  * Formats the given date and time `$string` based on the given `$format`.
  * Optionally the result will be localized and respect a timezone differing
  * from the system default. The default output is ISO 8601.
  *
  * @since Symphony 2.2.1
  * @param string $string (optional)
  *  A string containing date and time, defaults to the current date and time
  * @param string $format (optional)
  *  A valid PHP date format, defaults to ISO 8601
  * @param boolean $localize (optional)
  *  Localizes the output, if true, defaults to true
  * @param string $timezone (optional)
  *  The timezone associated with the timestamp
  * @return string
  *  The formatted date
  */
 public static function format($string = 'now', $format = DateTime::ISO8601, $localize = true, $timezone = null)
 {
     // Current date and time
     if ($string == 'now' || empty($string)) {
         $date = new DateTime();
     } elseif (is_numeric($string)) {
         $date = new Datetime(date(DateTime::ISO8601, $string));
     } else {
         // Standardize date
         // Convert date string to English
         $string = Lang::standardizeDate($string);
         // PHP 5.3: Apply Symphony date format using `createFromFormat`
         if (method_exists('DateTime', 'createFromFormat')) {
             $date = DateTime::createFromFormat(__SYM_DATETIME_FORMAT__, $string);
             if ($date === false) {
                 $date = DateTime::createFromFormat(__SYM_DATE_FORMAT__, $string);
             }
         } else {
             $date = strptime($string, DateTimeObj::dateFormatToStrftime(__SYM_DATETIME_FORMAT__));
             if ($date === false) {
                 $date = DateTimeObj::dateFormatToStrftime(__SYM_DATE_FORMAT__, $string);
             }
             if (is_array($date)) {
                 $date = date(DateTime::ISO8601, mktime($date['tm_hour'], $date['tm_min'], $date['tm_sec'], $date['tm_mon'] + 1, $date['tm_mday'], 1900 + $date['tm_year']));
                 $date = new DateTime($date);
             }
         }
         // Handle non-standard dates (ie. relative dates, tomorrow etc.)
         if ($date === false) {
             $date = new DateTime($string);
         }
     }
     // Timezone
     if ($timezone !== null) {
         $date->setTimezone(new DateTimeZone($timezone));
     }
     // Format date
     $date = $date->format($format);
     // Localize date
     // Convert date string from English back to the activated Language
     if ($localize === true) {
         $date = Lang::localizeDate($date);
     }
     // Return custom formatted date, use ISO 8601 date by default
     return $date;
 }