array(
			'controller' 	=> 'page_admin_ui',
			'path' 			=> null,
			'ui' 			=> 'page_admin_form_ui',
			'uipath' 		=> null
		),
		'overview'		=> array(
			'controller' 	=> 'page_admin_ui',
			'path' 			=> null,
			'ui' 			=> 'page_admin_form_ui',
			'uipath' 		=> null
		),
		'cat'		=> array(
			'controller' 	=> 'page_chapters_ui',
			'path' 			=> null,
			'ui' 			=> 'page_chapters_form_ui',
			'uipath' 		=> null
		),
		'menu'		=> array(
			'controller' 	=> 'page_admin_ui',
			'path' 			=> null,
			'ui' 			=> 'page_admin_form_ui',
			'uipath' 		=> null
		),
		'dialog'		=> array(
			'controller' 	=> 'menu_admin_ui',
			'path' 			=> null,
			'ui' 			=> 'menu_admin_form_ui',
			'uipath' 		=> null
		)
			
	);	
	
	protected $adminMenu = array(
		'overview/list'	=> array('caption'=> CUSLAN_1, 'perm' => '5|J'), 
		'page/list'		=> array('caption'=> CUSLAN_48, 'perm' => '5'),
		'menu/list'		=> array('caption'=> CUSLAN_49, 'perm' => 'J', 'tab' => 2),
		'page/create' 	=> array('caption'=> CUSLAN_12, 'perm' => '5'),
		'other' 		=> array('divider'=> true),
		'cat/list' 		=> array('caption'=> CUSLAN_50, 'perm' => '5'), // Create Category.
		'cat/create' 	=> array('caption'=> CUSLAN_51, 'perm' => '5'), // Category List
		'other2' 		=> array('divider'=> true),
	
	//	'menu/create' 	=> array('caption'=> CUSLAN_31, 'perm' => 'J', 'tab' => 2),
		'page/prefs'	=> array('caption'=> LAN_OPTIONS, 'perm' => '0')		
	);
	protected $adminMenuIcon = 'e-custom-24';
	
	protected $adminMenuAliases = array(
		'overview/edit' => 'overview/list',
		'page/edit'		=> 'page/list',
		'menu/edit'		=> 'menu/create',
		'menu/grid'		=> 'menu/list',
		'cat/edit'      => 'cat/list'
	);	
	
	protected $menuTitle = ADLAN_42;
	function init()
	{
	}
}
class page_admin_form_ui extends e_admin_form_ui
{
	
	function page_title($curVal,$mode,$parm)
	{
	
		if($mode == 'read') 
		{
			$id = $this->getController()->getListModel()->get('page_id');
			return "".$curVal."";
		}
			
		if($mode == 'write')
		{
			return null;
		}
			
		if($mode == 'filter')
		{
			return null;
		}
		if($mode == 'batch')
		{
			return null;
		}		
	}
	
		// Override the default Options field. 
	function options($parms, $value, $id, $attributes)
	{
		
		if($attributes['mode'] == 'read')
		{
			parse_str(str_replace('&', '&', e_QUERY), $query); //FIXME - FIX THIS
			$query['action'] = 'edit';
			$query['id'] = $id;
			$query = http_build_query($query, '', '&');
				
			$text = "
						".defset('ADMIN_EDIT_ICON')."";
			if($this->getController()->getMode() === 'overview' && getperms('J1')) // Page/Menu Delete Perms.
			{
				$text .= $this->submit_image('menu_delete['.$id.']', $id, 'delete', LAN_DELETE.' [ ID: '.$id.' ]', array('class' => 'action delete btn btn-danger'));
			}
			return $text;
		}
	}
}
//FIXME - needs a layout similar to the admin sitelinks page. ie. showing chapters as we would 'sublinks'. 
// BOOKS & CHAPTERS 
class page_chapters_ui extends e_admin_ui
{
		protected $pluginTitle	= CUSLAN_59;
		protected $pluginName	= 'core';
		protected $table 		= "page_chapters";
		protected $pid			= "chapter_id";
		protected $perPage 		= 0; //no limit
		protected $batchDelete 	= false;
		protected $batchCopy	= true;	
        protected $batchLink   	= true;
        protected $batchExport  = true;
	//	protected $listQry          = "SELECT a. *, CASE WHEN a.chapter_parent = 0 THEN a.chapter_order ELSE b.chapter_order + (( a.chapter_order)/1000) END AS Sort FROM `#page_chapters` AS a LEFT JOIN `#page_chapters` AS b ON a.chapter_parent = b.chapter_id ";
	protected $listQry = "SELECT a.*, 
                         CASE WHEN a.chapter_parent = 0 THEN a.chapter_order ELSE b.chapter_order + ((a.chapter_order)/1000) END AS Sort,
                         (SELECT COUNT(*) FROM `#page` p WHERE p.page_chapter = a.chapter_id) AS chapter_page_count 
                         FROM `#page_chapters` AS a 
                         LEFT JOIN `#page_chapters` AS b ON a.chapter_parent = b.chapter_id ";
		protected $listOrder		= 'Sort,chapter_order ';
	//	protected $listOrder 	= ' COALESCE(NULLIF(chapter_parent,0), chapter_id), chapter_parent > 0, chapter_order '; //FIXME works with parent/child but doesn't respect parent order.
		protected $url         	= array('route'=>'page/chapter/index', 'vars' => array('id' => 'chapter_id', 'name' => 'chapter_sef'), 'name' => 'chapter_name', 'description' => ''); // 'link' only needed if profile not provided. 
	
		protected $sortField	= 'chapter_order';
		protected $sortParent   = 'chapter_parent';
	//	protected $orderStep 	= 10;
		
		protected $fields = array(
			'checkboxes'				=> array('title'=> '',						'type' => null, 			'width' =>'5%', 'forced'=> TRUE, 'thclass'=>'center', 'class'=>'center'),
			'chapter_id'				=> array('title'=> 'LAN_ID',					'type' => 'number',			'width' =>'5%', 'forced'=> TRUE, 'readonly'=>TRUE),
         	'chapter_icon' 				=> array('title'=> 'LAN_ICON',				'type' => 'icon', 			'data' => 'str',		'width' => '100px',	'thclass' => 'center', 'class'=>'center', 'writeParms'=> 'glyphs=1', 'readonly'=>FALSE,	'batch' => FALSE, 'filter'=>FALSE),
         	'chapter_parent' 			=> array('title'=> 'CUSLAN_52',		   		'type' => 'dropdown',		'width' => 'auto', 'thclass' => 'left', 'readonly'=>FALSE, 'filter'=>true),
         	'chapter_name' 				=> array('title'=> 'CUSLAN_53',	            'type' => 'method',			'width' => 'auto', 'thclass' => 'left', 'readonly'=>FALSE, 'writeParms'=>'size=xxlarge'),
         	'chapter_template' 			=> array('title'=> 'LAN_TEMPLATE', 			'type' => 'dropdown', 		'width' => 'auto','filter' => true, 'batch'=>true, 'inline'=>true, 'writeParms'=>''),
            'chapter_page_count'        => array('title'=> 'CUSLAN_87', 'type' => 'number', 'data'=>false, 'width' => '5%', 'readonly'=>TRUE, 'thclass'=>'center', 'class'=>'page-count center'),
         	'chapter_meta_description'	=> array('title'=> 'LAN_DESCRIPTION',			'type' => 'textarea',		'width' => 'auto', 'thclass' => 'left','readParms' => 'expand=...&truncate=150&bb=1', 'writeParms'=>'size=xxlarge', 'readonly'=>FALSE),
			'chapter_meta_keywords' 	=> array('title'=> 'LAN_KEYWORDS',			'type' => 'tags',			'inline'=>true, 'width' => 'auto', 'thclass' => 'left', 'readonly'=>FALSE),
			'chapter_sef' 				=> array('title'=> 'LAN_SEFURL',	    	    'type' => 'text',			'width' => 'auto', 'readonly'=>FALSE, 'batch'=>true,  'inline'=>true, 'writeParms'=>'size=xxlarge&inline-empty=1&sef=chapter_name',  ), // Display name
			'chapter_manager' 			=> array('title'=> 'CUSLAN_55',		        'type' => 'userclass',		'inline'=>true, 'width' => 'auto', 'data' => 'int','batch'=>TRUE, 'filter'=>TRUE),
			'chapter_order' 			=> array('title'=> 'LAN_ORDER',				'type' => 'text',			'width' => 'auto', 'thclass' => 'right', 'class'=> 'right' ),
			'chapter_visibility' 		=> array('title'=> 'LAN_VISIBILITY',			'type' => 'userclass',		'inline'=>true, 'width' => 'auto', 'data' => 'int','batch'=>TRUE, 'filter'=>TRUE),
			'chapter_fields'            => array('title'=> 'hidden',                 'type'=>'hidden'),
			'chapter_image' 	        => array('title'=> 'LAN_IMAGE',			    'type' => 'image', 			'data' => 'str',		'width' => '100px',	'thclass' => 'center', 'class'=>'center',  'readParms'=>'thumb=140&thumb_urlraw=0&thumb_aw=140', 'writeParms'=>'', 'readonly'=>FALSE,	'batch' => FALSE, 'filter'=>FALSE),
			'options' 					=> array('title'=> 'LAN_OPTIONS',				'type' => 'method',			'width' => '10%', 'forced'=>TRUE, 'thclass' => 'center last', 'class' => 'left', 'readParms'=>'sort=1')
		
		);
		protected $fieldpref = array('checkboxes', 'chapter_icon', 'chapter_id', 'chapter_page_count','chapter_name', 'chapter_description','chapter_template', 'chapter_visibility', 'chapter_order', 'options');
		protected $books = array();
	
		function init()
		{
			$this->addTitle(CUSLAN_63);
		//	e107::getMessage()->addWarning("Experimental: Custom Fields");
			$this->tabs = array(LAN_GENERAL,CUSLAN_4);
			$this->fields['chapter_fields'] = array('title'=>"Fields", 'tab'=>1, 'type'=>'method', 'data'=>'json', 'writeParms'=>array('nolabel'=>2));
			if($this->getAction() === 'list')
			{
				$this->fields['chapter_parent']['title'] = LAN_PARENT;
			}
			elseif(deftrue('e_DEBUG'))
			{
				$this->fields['chapter_sef']['title'] = LAN_SEFURL.' / '.LAN_NAME;
				$this->fields['chapter_sef']['help'] = 'May also be used in shortcode {CHAPTER_MENUS: name=x}';
			}
			$sql = e107::getDb();
			$sql->gen("SELECT chapter_id,chapter_name FROM #page_chapters WHERE chapter_parent =0");
			$this->books[0] = CUSLAN_5;
			
			while($row = $sql->fetch())
			{
				$bk = $row['chapter_id'];
				$this->books[$bk] = $row['chapter_name'];
			}
			
			asort($this->books);
			
			$this->fields['chapter_parent']['writeParms'] = $this->books;	
			
			
			$tmp = e107::getLayouts('', 'chapter', 'front', '', true, false);
			$tmpl = array();
			foreach($tmp as $key=>$val)
			{
				if(substr($key,0,3) != 'nav')
				{
					$tmpl[$key] = $val;	
				}	
			}
			
			$this->fields['chapter_template']['writeParms'] = $tmpl; // e107::getLayouts('', 'chapter', 'front', '', true, false); // e107::getLayouts('', 'page', 'books', 'front', true, false); 
			
		}
		
		
		public function beforeCreate($new_data, $old_data)
		{
			if(empty($new_data['chapter_sef']))
			{
				$new_data['chapter_sef'] = eHelper::title2sef($new_data['chapter_name']);
			}
			else 
			{
				$new_data['chapter_sef'] = eHelper::secureSef($new_data['chapter_sef']);
			}
			
			$sef = e107::getParser()->toDB($new_data['chapter_sef']);
			
			if(e107::getDb()->count('page_chapters', '(*)', "chapter_sef='{$sef}'"))
			{
				e107::getMessage()->addError(CUSLAN_57);
				return false;
			}
			$new_data = e107::getCustomFields()->processConfigPost('chapter_fields', $new_data);
			
			return $new_data;	
		}
		
		
		public function beforeUpdate($new_data, $old_data, $id)
		{	
			// return $this->beforeCreate($new_data);
			$new_data = e107::getCustomFields()->processConfigPost('chapter_fields', $new_data);
			return $new_data;
		}
/*
		private function processCustomFields($newdata)
		{
			if(empty($newdata))
			{
				return null;
			}
			$new = array();
			foreach($newdata as $fields)
			{
				if(empty($fields['key']) || empty($fields['type']))
				{
					continue;
				}
				$key = $fields['key'];
				unset($fields['key']);
				$new[$key] = $fields;
			}
			return $new;
		}*/
}
class page_chapters_form_ui extends e_admin_form_ui
{
	
	function chapter_name($curVal,$mode,$parm)
	{
	
		$frm = e107::getForm();
	
		if($mode == 'read') 
		{
			$parent 	= $this->getController()->getListModel()->get('chapter_parent');
			$id			= $this->getController()->getListModel()->get('chapter_id');
			$level = 1;
			$linkQ = e_SELF."?searchquery=&filter_options=page_chapter__".$id."&mode=page&action=list";	
			$level_image = $parent ? ' ' : '';
			return ($parent) ?  $level_image."".$curVal."" : $curVal;
		}
			
		if($mode == 'write')
		{
			return $frm->text('chapter_name',$curVal,255,'size=xxlarge');	
		}
			
		if($mode == 'filter')
		{
			return null;
		}
		if($mode == 'batch')
		{
			return null;
		}		
	}
	
	
	function chapter_fields($curVal,$mode,$parm)
	{
		$fieldAmount = (deftrue('e_DEBUG')) ? 20 :10;
/*
		if($mode == 'read')
		{
		}
*/
		if($mode == 'write')
		{
			return e107::getCustomFields()->loadConfig($curVal)->renderConfigForm('chapter_fields');
		}
/*
		if($mode == 'filter')
		{
			return;
		}
		if($mode == 'batch')
		{
			return;
		}
*/
	}
	
		// Override the default Options field. 
	function options($parms, $value, $id, $attributes)
	{
		//$id = $this->getController()->getListModel()->get('page_id');
		//	return "".$curVal."";
		if($attributes['mode'] !== 'read')
		{
			return;
		}
		$parent = $this->getController()->getListModel()->get('chapter_parent');
	//	$id = $this->getController()->getListModel()->get('chapter_id');
		$att = [];
		$att['readParms']['sort'] = 1;
		if(!getperms('J1')) // Page/Menu Delete Perms.
		{
			$att['readParms']['deleteClass'] = e_UC_NOBODY;
		}
		$text = "
 ' : '';
			return ($parent) ?  $level_image."".$curVal."" : $curVal;
		}
			
		if($mode == 'write')
		{
			return $frm->text('chapter_name',$curVal,255,'size=xxlarge');	
		}
			
		if($mode == 'filter')
		{
			return null;
		}
		if($mode == 'batch')
		{
			return null;
		}		
	}
	
	
	function chapter_fields($curVal,$mode,$parm)
	{
		$fieldAmount = (deftrue('e_DEBUG')) ? 20 :10;
/*
		if($mode == 'read')
		{
		}
*/
		if($mode == 'write')
		{
			return e107::getCustomFields()->loadConfig($curVal)->renderConfigForm('chapter_fields');
		}
/*
		if($mode == 'filter')
		{
			return;
		}
		if($mode == 'batch')
		{
			return;
		}
*/
	}
	
		// Override the default Options field. 
	function options($parms, $value, $id, $attributes)
	{
		//$id = $this->getController()->getListModel()->get('page_id');
		//	return "".$curVal."";
		if($attributes['mode'] !== 'read')
		{
			return;
		}
		$parent = $this->getController()->getListModel()->get('chapter_parent');
	//	$id = $this->getController()->getListModel()->get('chapter_id');
		$att = [];
		$att['readParms']['sort'] = 1;
		if(!getperms('J1')) // Page/Menu Delete Perms.
		{
			$att['readParms']['deleteClass'] = e_UC_NOBODY;
		}
		$text = "
";
		$text .= $this->renderValue('options',$value,$att,$id);
			
		if($parent != 0)
		{
			$link = e_SELF."?searchquery=&filter_options=page_chapter__".$id."&mode=page&action=list";
			$text .= "
".defset('ADMIN_PAGES_ICON')."";  //
		}
		$text .= "