/**
  * get templates data
  *
  * @param boolean $advanced
  * @return array
  */
 private function getTemplateData($advanced)
 {
     $content = array();
     $advanced = wire('config')->advanced || $advanced;
     foreach (wire('templates') as $t) {
         if (!$advanced && $t->flags & \Template::flagSystem) {
             continue;
         }
         $content[] = array($t->name, count($t->fieldgroup), $t->getNumPages(), wireRelativeTimeStr($t->modified), $t->flags & \Template::flagSystem ? '✖' : '');
     }
     return $content;
 }
Beispiel #2
0
/**
 * Format a date, using PHP date(), strftime() or other special strings (see arguments).
 * 
 * This is designed to work the same wa as PHP's date() but be able to accept any common format
 * used in ProcessWire. This is helpful in reducing code in places where you might have logic 
 * determining when to use date(), strftime(), or wireRelativeTimeStr(). 
 * 
 * @param string|int $format Use one of the following:
 *  - PHP date() format
 * 	- PHP strftime() format (detected by presence of a '%' somewhere in it)
 * 	- 'relative': for a relative date/time string.
 *  - 'relative-': for a relative date/time string with no tense. 
 * 	- 'rel': for an abbreviated relative date/time string.
 * 	- 'rel-': for an abbreviated relative date/time string with no tense.
 * 	- 'r': for an extra-abbreviated relative date/time string.
 * 	- 'r-': for an extra-abbreviated relative date/time string with no tense.
 * 	- 'ts': makes it return a unix timestamp
 * 	- '': blank string makes it use the system date format ($config->dateFormat) 
 * 	- If given an integer and no second argument specified, it is assumed to be the second ($ts) argument. 
 * @param int|string|null $ts Optionally specify the date/time stamp or strtotime() compatible string. 
 * 	If not specified, current time is used.
 * @return string|bool Formatted date/time, or boolean false on failure
 * 
 */
function wireDate($format = '', $ts = null)
{
    if (is_null($ts)) {
        // ts not specified, or it was specified in $format
        if (ctype_digit("{$format}")) {
            // ts specified in format
            $ts = (int) $format;
            $format = '';
        } else {
            // use current timestamp
            $ts = time();
        }
    } else {
        if (is_string($ts) && ctype_digit("{$ts}")) {
            // ts is a digit string, convert to integer
            $ts = (int) $ts;
        } else {
            if (is_string($ts)) {
                // ts is a non-integer string, we assume to be a strtotime() compatible formatted date
                $ts = strtotime($ts);
            }
        }
    }
    if ($format == '') {
        $format = wire('config')->dateFormat;
    }
    if ($format == 'relative') {
        $value = wireRelativeTimeStr($ts);
    } else {
        if ($format == 'relative-') {
            $value = wireRelativeTimeStr($ts, false, false);
        } else {
            if ($format == 'rel') {
                $value = wireRelativeTimeStr($ts, true);
            } else {
                if ($format == 'rel-') {
                    $value = wireRelativeTimeStr($ts, true, false);
                } else {
                    if ($format == 'r') {
                        $value = wireRelativeTimeStr($ts, 1);
                    } else {
                        if ($format == 'r-') {
                            $value = wireRelativeTimeStr($ts, 1, false);
                        } else {
                            if ($format == 'ts') {
                                $value = $ts;
                            } else {
                                if (strpos($format, '%') !== false) {
                                    $value = strftime($format, $ts);
                                } else {
                                    $value = date($format, $ts);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $value;
}
Beispiel #3
0
 /**
  * Get information about all the caches in this WireCache
  * 
  * @param bool $verbose Whether to be more verbose for human readability
  * @param string $name Optionally specify name of cache to get info. If ommitted, all caches are included.
  * @return array of arrays of cache info
  * 
  */
 public function getInfo($verbose = true, $name = '')
 {
     $all = array();
     $sql = "SELECT name, data, expires FROM caches ";
     if ($name) {
         $sql .= "WHERE name=:name";
     }
     $query = $this->wire('database')->prepare($sql);
     if ($name) {
         $query->bindValue(":name", $name);
     }
     $query->execute();
     while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
         $info = array('name' => $row['name'], 'type' => 'string', 'expires' => '');
         $c = substr($row['data'], 0, 1);
         if ($c == '{' || $c == '[') {
             // json encoded
             $data = json_decode($row['data'], true);
             if (is_array($data)) {
                 if (array_key_exists('WireCache', $data)) {
                     if (isset($data['selector'])) {
                         $selector = $data['selector'];
                         $info['expires'] = $verbose ? 'when selector matches modified page' : 'selector';
                         $info['selector'] = $selector;
                     }
                     $data = $data['WireCache'];
                 }
                 if (is_array($data) && array_key_exists('PageArray', $data)) {
                     $info['type'] = 'PageArray';
                     if ($verbose) {
                         $info['type'] .= ' (' . count($data['PageArray']) . ' pages)';
                     }
                 } else {
                     if (is_array($data)) {
                         $info['type'] = 'array';
                         if ($verbose) {
                             $info['type'] .= ' (' . count($data) . ' items)';
                         }
                     }
                 }
             }
         }
         if (empty($info['expires'])) {
             if ($row['expires'] == self::expireNever) {
                 $info['expires'] = $verbose ? 'never' : '';
             } else {
                 if ($row['expires'] == self::expireSave) {
                     $info['expires'] = $verbose ? 'when any page or template is modified' : 'save';
                 } else {
                     if ($row['expires'] < time()) {
                         $t = strtotime($row['expires']);
                         foreach ($this->wire('templates') as $template) {
                             if ($template->id == $t) {
                                 $info['expires'] = $verbose ? "when '{$template->name}' page or template is modified" : 'save';
                                 $info['template'] = $template->id;
                                 break;
                             }
                         }
                     }
                 }
             }
             if (empty($info['expires'])) {
                 $info['expires'] = $row['expires'];
                 if ($verbose) {
                     $info['expires'] .= " (" . wireRelativeTimeStr($row['expires']) . ")";
                 }
             }
         }
         if ($verbose) {
             $info['size'] = strlen($row['data']);
         }
         $all[] = $info;
     }
     $query->closeCursor();
     return $all;
 }
 /**
  * Execute export
  *
  * @return string
  *
  */
 public function ___buildExport()
 {
     $form = $this->wire('modules')->get('InputfieldForm');
     $form->action = './';
     $form->method = 'post';
     $exportTemplates = $this->wire('input')->post('export_templates');
     if (empty($exportTemplates)) {
         $f = $this->wire('modules')->get('InputfieldSelectMultiple');
         $f->attr('id+name', 'export_templates');
         $f->label = $this->_('Select the templates that you want to export');
         $f->icon = 'copy';
         $maxName = 0;
         $maxLabel = 0;
         $numTemplates = 0;
         foreach ($this->wire('templates') as $template) {
             if (strlen($template->name) > $maxName) {
                 $maxName = strlen($template->name);
             }
             $label = $template->getLabel();
             if (strlen($label) > $maxLabel) {
                 $maxLabel = strlen($label);
             }
             $numTemplates++;
         }
         $templateName = $this->_('NAME') . ' ';
         $templateLabel = $this->_('LABEL') . ' ';
         $numFields = $this->_('FIELDS') . ' ';
         $modified = $this->_('MODIFIED');
         $label = $templateName . ' ' . str_repeat('.', $maxName - strlen($templateName) + 10) . ' ' . $templateLabel . str_repeat('.', $maxLabel - strlen($templateLabel) + 10) . ' ' . str_pad($numFields, 13, '.') . ' ' . $modified;
         $f->addOption(0, $label, array('disabled' => 'disabled'));
         foreach ($this->wire('templates') as $template) {
             //if(!is_object($template->fieldgroup)) $this->error("Template: $template has no fieldgroup");
             $templateName = $template->name . ' ';
             $templateLabel = $template->getLabel() . ' ';
             if ($templateLabel == $templateName) {
                 $templateLabel = '';
             }
             $numFields = count($template->fieldgroup) . ' ';
             $modified = $template->modified ? wireRelativeTimeStr($template->modified) : '';
             $label = $templateName . str_repeat('.', $maxName - strlen($templateName) + 10) . ' ' . $templateLabel . str_repeat('.', $maxLabel - strlen($templateLabel) + 10) . ' ' . str_pad($numFields, 13, '.') . ' ' . $modified;
             $f->addOption($template->name, $label);
         }
         $f->notes = $this->_('Shift+Click to select multiple in sequence. Ctrl+Click (or Cmd+Click) to select multiple individually. Ctrl+A (or Cmd+A) to select all.');
         $f->attr('size', $numTemplates + 1);
         $form->add($f);
         $f = $this->wire('modules')->get('InputfieldSubmit');
         $f->attr('name', 'submit_export');
         $f->attr('value', $this->_x('Export', 'button'));
         $form->add($f);
     } else {
         $form = $this->wire('modules')->get('InputfieldForm');
         $f = $this->wire('modules')->get('InputfieldTextarea');
         $f->attr('id+name', 'export_data');
         $f->label = $this->_('Export Data');
         $f->description = $this->_('Copy and paste this data into the "Import" box of another installation.');
         $f->notes = $this->_('Click anywhere in the box to select all export data. Once selected, copy the data with CTRL-C or CMD-C.');
         $f->attr('value', wireEncodeJSON($this->getExportData($exportTemplates), true, true));
         $form->add($f);
         $f = $this->wire('modules')->get('InputfieldButton');
         $f->href = './';
         $f->value = $this->_x('Ok', 'button');
         $form->add($f);
     }
     return $form;
 }