__('Page Type', __FILE__), // getModuleInfo title 'version' => 101, 'summary' => __('List, Edit and Add pages of a specific type', __FILE__), // getModuleInfo summary 'permanent' => true, 'useNavJSON' => true, ); } protected $pages; protected $template = null; protected $editor = null; public function __construct() { $this->set('showFields', array('name')); $this->set('addLabel', $this->_('Add New')); $this->set('jsonListLabel', 'name'); // what to use for 'label' property in JSON nav data } public function init() { $this->config->scripts->add($this->config->urls->ProcessPageType . 'ProcessPageType.js'); $this->config->styles->add($this->config->urls->ProcessPageType . 'ProcessPageType.css'); $this->pages = $this->wire($this->page->name); if(is_null($this->pages) || !$this->pages instanceof PagesType) { // $this->error("Unable to find API variable named '{$this->page->name}' (of type: PagesType)", Notice::debug); $this->pages = $this->wire('pages'); } if($this->pages instanceof PagesType) $this->template = $this->pages->getTemplate(); parent::init(); } public function ___execute() { return $this->executeList(); } public function ___executeList() { return $this->renderList("limit=25, status<" . Page::statusMax); } /** * Output JSON list of navigation items for this (intended to for ajax use) * */ public function ___executeNavJSON(array $options = array()) { $limit = (int) $this->wire('input')->get('limit'); if(!$limit || $limit > 100) $limit = 100; $start = (int) $this->wire('input')->get('start'); $pages = $this->pages->find("start=$start, limit=$limit"); foreach($pages as $page) { if(!$page->editable()) $pages->remove($page); } $options['add'] = 'add/'; $options['items'] = $pages; $options['edit'] = "edit/?id={id}"; $options['itemLabel'] = $this->jsonListLabel; return parent::___executeNavJSON($options); } public function ___executeEdit() { $this->wire('breadcrumbs')->add(new Breadcrumb('../', $this->page->get('title|name'))); $this->editor = $this->modules->get("ProcessPageEdit"); return $this->editor->execute(); } public function ___executeAdd() { $this->wire('breadcrumbs')->add(new Breadcrumb('../', $this->page->get('title|name'))); $editor = $this->modules->get("ProcessPageAdd"); $editor->template = $this->template; $editor->parent_id = $this->page->id; return $editor->execute(); } protected function renderList($selector = '', $pagerOptions = array()) { $out = ''; if(!$this->pages instanceof PagesType || !$this->pages->getTemplate()) { $form = $this->getTemplateFilterForm(); $out = $form->render(); } $table = $this->modules->get("MarkupAdminDataTable"); $table->setEncodeEntities(false); $fieldNames = $this->showFields; $fieldLabels = $fieldNames; foreach($fieldLabels as $key => $name) { if($name == 'name') { $fieldLabels[$key] = $this->_('Name'); // Label for 'name' field continue; } $field = wire('fields')->get($name); if($field) { $label = $field->getLabel(); $fieldLabels[$key] = htmlentities($label, ENT_QUOTES, "UTF-8"); } } $table->headerRow($fieldLabels); $pages = $this->pages->find($selector); $numRows = 0; foreach($pages as $page) { if(!$page->editable()) continue; $n = 0; $row = array(); foreach($fieldNames as $name) { if(!$n) { $value = htmlentities($page->getUnformatted($name), ENT_QUOTES, 'UTF-8') . ' '; $status = ''; if($page->is(Page::statusUnpublished)) $status .= 'PageListStatusUnpublished '; if($page->is(Page::statusHidden)) $status .= 'PageListStatusHidden '; if($status) $value = "$value"; $row[$value] = "edit/?id={$page->id}"; } else { $row[] = $this->renderListFieldValue($name, $page->getUnformatted($name)); } $n++; } $table->row($row); $numRows++; } if($this->page->addable()) $table->action(array($this->addLabel => 'add/')); if($pages->getTotal() > count($pages)) { $pager = $this->modules->get("MarkupPagerNav"); $out .= $pager->render($pages, $pagerOptions); } if(!$numRows) $out .= $this->renderEmptyList($pages); $out .= $table->render(); return $out; } protected function renderEmptyList(PageArray $pages) { $out = "

" . $this->_('No items to display yet.') . "

"; return $out; } protected function renderListFieldValue($name, $value) { if(is_string($value) || is_int($value)) return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); if(is_array($value)) return htmlspecialchars(print_r($value, true), ENT_QUOTES, 'UTF-8'); if(is_object($value)) { if($value instanceof WireArray) { $out = ''; foreach($value as $k => $v) { $out .= $v->name . ", "; } return nl2br(rtrim($out, ", ")); } else if($value instanceof Wire) { if($value->name) return $value->name; return (string) $value; } } return ''; } protected function getTemplateFilterForm() { $form = $this->modules->get("InputfieldForm"); $form->attr('id', 'template_filter_form'); $form->attr('method', 'get'); $form->attr('action', './list'); $field = $this->modules->get("InputfieldSelect"); $field->attr('id+name', 'templates_id'); $field->label = $this->_('Filter by Template'); $field->addOption('', $this->_('Show All')); $field->collapsed = Inputfield::collapsedBlank; foreach($this->templates as $template) { $field->addOption($template->id, $template->name); } $filterName = $this->className . 'TemplatesID'; if(isset($this->input->get->templates_id)) { $this->session->set($filterName, (int) $this->input->get->templates_id); } $filterValue = (int) $this->session->$filterName; if($filterValue) $this->template = $this->templates->get($filterValue); $field->attr('value', $filterValue); $form->append($field); return $form; } static public function getModuleConfigInputfields(array $data) { $showFields = isset($data['showFields']) ? $data['showFields'] : array(); $fields = array('name'); foreach(wire('fields') as $field) $fields[] = $field->name; $inputfields = new InputfieldWrapper(); $f = wire('modules')->get('InputfieldAsmSelect'); $f->label = __("What fields should be displayed in the page listing?"); $f->attr('id+name', 'showFields'); foreach($fields as $name) $f->addOption($name); $f->attr('value', $showFields); $inputfields->add($f); return $inputfields; } public function getPage() { if($this->editor) return $this->editor->getPage(); return new NullPage(); } }