<?php namespace ProcessWire;

/**
 * ProcessWire List Process
 *
 * Generates a <dl> definition list of Processes attached to each child page. 
 * 
 * For more details about how Process modules work, please see: 
 * /wire/core/Process.php 
 * 
 * ProcessWire 3.x, Copyright 2016 by Ryan Cramer
 * https://processwire.com
 *
 *
 */

class ProcessList extends Process {

	public static function getModuleInfo() {
		return array(
			'title' => __('List', __FILE__), // getModuleInfo title          
			'summary' => __('Lists the Process assigned to each child page of the current', __FILE__), // getModuleInfo summary
			'version' => 101, 
			'permanent' => true, 
			'permission' => 'page-view',
			);
	}


	public function ___execute() {
		return $this->render();
	}	

	protected function render() {
		$defaults = array(
			'dlClass' => 'nav',
			'dtClass' => '',
			'ddClass' => '',
			'aClass' => 'label',
			'disabledClass' => 'ui-priority-secondary',
			'showIcon' => true, 
		);
		$settings = $this->wire('config')->ProcessList;
		if(!is_array($settings)) $settings = array();
		$settings = array_merge($defaults, $settings);
		$out = "\n<dl class='$settings[dlClass]'>";
		$cnt = 0; 
		
		foreach($this->page->children("check_access=0") as $child) {
			
			if(!$child->viewable()) continue;
			
			$dtClass = $settings['dtClass'];
			$ddClass = $settings['ddClass'];
			$icon = '';
			
			if($child->process) {
				
				$info = $this->modules->getModuleInfoVerbose($child->process, array('noCache' => true)); 	
				if($settings['showIcon']) { 
					$icon = $child->get('page_icon');
					if(!$icon) $icon = $info['icon'];
					if($icon) {
						if(strpos($icon, 'fa-') === 0) list(,$icon) = explode('-', 2);
						$icon = "<i style='float: right;' class='fa fa-3x fa-fw fa-$icon ui-priority-secondary'></i> ";
					}
				}
				
				$title = $child->title;
				if(!strlen($title)) $title = $info['title'];
				if(!strlen($title)) $title = $child->name;
				$titleTranslated = __($title, '/wire/templates-admin/default.php'); 
				if($titleTranslated && $titleTranslated != $title) $title = $titleTranslated; 
				$title = $this->wire('sanitizer')->entities1($title); 
				if($child->summary) {
					$summary = $child->summary;
				} else {
					$summary = $info['summary'];
				}
					
				$summary = $this->wire('sanitizer')->entities1($summary);
				
			} else {
				
				$title = $child->get("title|name"); 
				if($child->template == 'admin') {
					$summary = $this->_('The process module assigned to this page does not appear to be installed.'); 
					$dtClass .= ' ' . $settings['disabledClass'];
					$ddClass .= ' ' . $settings['disabledClass'];
				} else if($child->summary) {
					$summary = $this->wire('sanitizer')->entities($child->getUnformatted('summary')); 
				} else {
					$summary = '<!--' . $this->_('No description available') . '-->';
				}
			}
			
			$dtClass = trim($dtClass);
			$ddClass = trim($ddClass);
			
			$out .= "\n\t<dt class='$dtClass'><a class='$settings[aClass]' href='{$child->url}'>$icon$title</a></dt>";
			if($summary) $out .= "\n\t<dd class='$ddClass'>$summary</dd>";
			$cnt++;
		}

		$out .= "\n</dl>";
		if(!$cnt) $out = '';
		return $out; 
	}
}

