1<?php
2/*
3 * e107 website system
4 *
5 * Copyright (C) 2008-2017 e107 Inc (e107.org)
6 * Released under the terms and conditions of the
7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
8 *
9 * Site navigation administration
10 *
11 */
12
13require_once("../class2.php");
14
15if (!getperms("I"))
16{
17	e107::redirect('admin');
18	exit;
19}
20
21e107::coreLan('links', true);
22
23e107::css('inline', " td .label-warning { margin-left:30px } ");
24
25
26class links_admin extends e_admin_dispatcher
27{
28
29	protected $modes = array(
30		'main'		=> array(
31			'controller' 	=> 'links_admin_ui',
32			'path' 			=> null,
33			'ui' 			=> 'links_admin_form_ui',
34			'uipath' 		=> null
35		)
36	);
37
38	protected $adminMenu = array(
39		'main/list'		=> array('caption'=> LAN_MANAGE, 'perm' => 'I'),
40		'main/create' 	=> array('caption'=> LAN_CREATE, 'perm' => 'I'),
41		'main/prefs' 	=> array('caption'=> LAN_OPTIONS, 'perm' => 'I'),
42		'main/tools'	=> array('caption'=> LINKLAN_4, 'perm' => 'I')
43	);
44
45	protected $adminMenuAliases = array(
46		'main/edit'	=> 'main/list'
47	);
48
49	protected $menuTitle = ADLAN_138;
50
51		protected $adminMenuIcon = 'e-links-24';
52}
53
54class links_admin_ui extends e_admin_ui
55{
56	protected $pluginTitle 	= ADLAN_138;
57	protected $pluginName 	= 'core';
58	protected $table 		= "links";
59	protected $listQry 		= '';
60	protected $pid 			= "link_id";
61	protected $perPage 		= 0;
62	protected $batchDelete 	= true;
63	protected $batchCopy 	= true;
64	protected $listOrder = 'link_category,link_order ASC';
65	protected $sortField	= 'link_order';
66
67    //FIXME TOOD - Filter out 'unassigned' entries by default.
68
69	public $current_parent = 0;
70	public $sublink_data = null;
71
72	protected $fields = array(
73		'checkboxes' 		=> array('title'=> '',				'width' => '3%',		'forced' => true,	'thclass'=>'center first',	'class'=>'center first'),
74		'link_button'		=> array('title'=> LAN_ICON, 		'type'=>'icon',			'width'=>'5%',		'thclass'=>'center',		'class'=>'center',	'readParms'=>array('legacy'=>'{e_IMAGE}icons/'),	'writeParms'=>'glyphs=1'),
75		'link_id'			=> array('title'=> LAN_ID, 			'type'=>'method',		'readParms'=>'',	'noedit'=>TRUE),
76		'link_name'			=> array('title'=> LAN_NAME,		'type'=>'text',			'inline'=>true,		'required'=>false,		'validate'=>false,	'width'=>'auto', 'writeParms'=>array('size'=>'xlarge')), // not required as only an icon may be used.
77		'link_category'		=> array('title'=> LAN_TEMPLATE,	'type'=>'dropdown',		'inline'=>true,		'batch'=>true,			'filter'=>true,		'width'=>'auto', 'writeParms'=>array('size'=>'xlarge')),
78
79		'link_parent'		=> array('title'=> LAN_PARENT,		'type' => 'method',		'data'=>'int',		'width'=>'auto',		'batch'=>true,		'filter'=>true,		'thclass'=>'left first', 'writeParms'=>array('size'=>'xlarge')),
80		'link_url'	   		=> array('title'=> LAN_URL, 		'width'=>'auto', 'type'=>'method', 'inline'=>true, 'required'=>true,'validate' => true, 'writeParms'=>'size=xxlarge'),
81		'link_sefurl' 		=> array('title'=> LAN_SEFURL, 		'type' => 'method', 'inline'=>false, 'width' => 'auto', 'help'=>LCLAN_107),
82		'link_class' 		=> array('title'=> LAN_USERCLASS, 	'type' => 'userclass','inline'=>true, 'writeParms' => 'classlist=public,guest,nobody,member,classes,admin,main', 'batch'=>true, 'filter'=>true, 'width' => 'auto'),
83		'link_description' 	=> array('title'=> LAN_DESCRIPTION,	'type' => 'textarea', 'width' => 'auto'), // 'method'=>'tinymce_plugins',  ?
84		'link_order' 		=> array('title'=> LAN_ORDER, 		'type' => 'number', 'width' => 'auto', 'nolist'=>false, 'inline' => true),
85		'link_open'			=> array('title'=> LCLAN_19, 		'type' => 'dropdown', 'inline'=>true, 'width' => 'auto', 'batch'=>true, 'filter'=>true, 'thclass' => 'left first', 'writeParms'=>array('size'=>'xlarge')),
86		'link_function'		=> array('title'=> LCLAN_105, 		'type' => 'method', 'data'=>'str', 'width' => 'auto', 'thclass' => 'left first'),
87		'link_owner'		=> array('title'=> LCLAN_106,		'type' => 'hidden', 'data'=>'str'),
88		'options' 			=> array('title'=> LAN_OPTIONS, 	'type'	=> null, 'forced'=>TRUE, 'width' => '10%', 'thclass' => 'center last', 'class'=>'center','readParms'=>'sort=1') // quick workaround
89	);
90
91	protected $fieldpref =  array('checkboxes','link_button', 'link_id','link_name','link_sefurl','link_class','link_category','options');
92
93	protected $prefs = array(
94		'linkpage_screentip'	=> array('title'=>LCLAN_78,	'type'=>'boolean', 'help'=>LCLAN_79),
95		'sitelinks_expandsub'	=> array('title'=>LCLAN_80,	'type'=>'boolean', 'help'=>LCLAN_81)
96	);
97
98
99	/**
100	 * Runtime cache of all links array
101	 * @var array
102	 */
103	protected $_link_array	= null;
104
105
106	function afterCreate($newdata,$olddata, $id) //FIXME needs to work after inline editing too.
107	{
108		e107::getCache()->clearAll('content');
109	}
110
111	function afterUpdate($newdata,$olddata, $id) //FIXME needs to work after inline editing too.
112	{
113		e107::getCache()->clearAll('content');
114	}
115
116
117
118	function init()
119	{
120		$this->fields['link_category']['writeParms']['optArray'] = array(
121			1	=> "1 - Main",
122			2	=> "2 - Sidebar",
123			3	=> "3 - Footer",
124			4	=> "4 - Alt",
125			5	=> "5 - Alt",
126			6	=> "6 - Alt", // If more than 6 are required, then something is not right with the themeing method.
127	//		7	=> "7 - Alt",
128	//		8	=> "8 - Alt",
129	//		9	=> "9 - Alt",
130	//		10	=> "10 - Alt"
131	       255 => "(Unassigned)",
132		);
133
134
135		$this->fields['link_open']['writeParms']['optArray'] = array(
136			0 => LCLAN_20, // 0 = same window
137			1 => LCLAN_23, // new window
138			4 => LCLAN_24, // 4 = miniwindow  600x400
139			5 => LINKLAN_1 // 5 = miniwindow  800x600
140		);
141
142
143
144
145
146	}
147
148
149
150	public function handleListLinkParentBatch($selected, $value)
151	{
152		$field = 'link_parent';
153		$ui = $this->getUI();
154		$found = false;
155		foreach ($selected as $k => $id)
156		{
157			// var_dump($ui->_has_parent($value, $id, $this->getLinkArray()));
158			if($ui->_has_parent($value, $id, $this->getLinkArray()))
159			{
160				unset($selected[$k]);
161				$found = true;
162			}
163		}
164		if($found) e107::getMessage()->addWarning(LCLAN_108);
165		if(!$selected) return;
166
167		if(parent::handleListBatch($selected, $field, $value))
168		{
169			$this->_link_array = null; // reset batch/filters
170			return true;
171		}
172		return false;
173	}
174
175	public function ListObserver()
176	{
177		$searchFilter = $this->_parseFilterRequest($this->getRequest()->getQuery('filter_options', ''));
178
179		if($searchFilter && in_array('link_parent', $searchFilter))
180		{
181			$this->getTreeModel()->current_id = intval($searchFilter[1]);
182			$this->current_parent = intval($searchFilter[1]);
183		}
184		parent::ListObserver();
185
186	}
187	public function ListAjaxObserver()
188	{
189		$searchFilter = $this->_parseFilterRequest($this->getRequest()->getQuery('filter_options', ''));
190
191		if($searchFilter && in_array('link_parent', $searchFilter))
192		{
193			$this->getTreeModel()->current_id = intval($searchFilter[1]);
194			$this->current_parent = intval($searchFilter[1]);
195		}
196		parent::ListAjaxObserver();
197	}
198
199	/**
200	 * Form submitted - 'etrigger_generate_sublinks' POST variable caught
201	 *//*
202	public function SublinksGenerateSublinksTrigger()
203	{
204		$this->generateSublinks();
205	}
206
207	public function sublinksObserver()
208	{
209		$this->getTreeModel()->load();
210	}*/
211
212	/**
213	 * Sublinks generator
214	 */
215	public function toolsPage()
216	{
217
218		if(!empty($_POST['etrigger_generate_sublinks']))
219		{
220			$this->generateSublinks($_POST);
221		}
222
223		$sublinks = $this->sublink_data();
224		$ui = $this->getUI();
225		// TODO - use UI create form
226		$sql = e107::getDb();
227		$text = "
228		<form method='post' action='".e_REQUEST_URL."'>
229			<fieldset id='core-links-generator'>
230				<legend class='e-hideme'>".LINKLAN_4."</legend>
231				<table class='table adminform'>
232					<colgroup>
233						<col class='col-label' />
234						<col class='col-control' />
235					</colgroup>
236					<tbody>
237						<tr>
238							<td>".LINKLAN_6.":</td>
239							<td>
240		";
241
242		foreach($sublinks as $key => $type)
243		{
244			$optarrayp[$key] = $type['title'];
245			//$selected = $this->getPosted('sublink_type') == $key ? ' selected="selected"' : '';
246			/*$text .= "
247									<option value='{$key}'{$selected}>".$type['title']."</option>
248					";*/
249		}
250		$text .= $ui->selectbox('sublink_type', $optarrayp, $this->getPosted('sublink_type'), '', true);
251
252		$text .= "
253							</td>
254						</tr>
255						<tr>
256							<td>".LINKLAN_7." (".LAN_OPTIONAL.")</td>
257							<td>
258								";
259		$text .= $ui->link_parent($this->getPosted('link_parent'), 'write');
260		$text .= "
261
262							</td>
263						</tr>
264					</tbody>
265				</table>
266				<div class='buttons-bar center'>
267					".$ui->admin_button('etrigger_generate_sublinks', 'no-value', 'submit', LINKLAN_5)."
268				</div>
269			</fieldset>
270		</form>
271		";
272
273		//$e107->ns->tablerender(LINKLAN_4, $emessage->render().$text);
274	//	$this->addTitle(LINKLAN_4);
275
276		return $text;
277
278	}
279
280
281
282
283
284
285
286
287
288
289
290
291	function sublink_data($name = "")
292	{
293		if(null !== $this->sublink_data) return ($name ? $this->sublink_data[$name] : $this->sublink_data);
294		$sublink_type = array();
295		$sublink_type['news']['title'] = LINKLAN_8; // "News Categories";
296		$sublink_type['news']['table'] = "news_category";
297		$sublink_type['news']['query'] = "category_id !='-2' ORDER BY category_name ASC";
298		$sublink_type['news']['url'] = "news.php?list.#";
299		$sublink_type['news']['fieldid'] = "category_id";
300		$sublink_type['news']['fieldname'] = "category_name";
301		$sublink_type['news']['fieldicon'] = "category_icon";
302		$sublink_type['news']['sef'] = "news/list/category";
303
304		$sublink_type['newsalt'] = $sublink_type['news'];
305		$sublink_type['newsalt']['url'] = "news.php?cat.#";
306		$sublink_type['newsalt']['title'] = LINKLAN_8." (".LAN_LIST.")"; // "News Categories";
307		$sublink_type['newsalt']['sef'] = "news/list/short";
308
309
310		$sublink_type['downloads']['title'] = LINKLAN_9; //"Download Categories";
311		$sublink_type['downloads']['table'] = "download_category";
312		$sublink_type['downloads']['query'] = "download_category_parent ='0' ORDER BY download_category_name ASC";
313		$sublink_type['downloads']['url'] = "download.php?list.#";
314		$sublink_type['downloads']['fieldid'] = "download_category_id";
315		$sublink_type['downloads']['fieldname'] = "download_category_name";
316		$sublink_type['downloads']['fieldicon'] = "download_category_icon";
317
318		// fixed - sql query not needed
319		$plugins = array_keys(e107::getConfig()->get('plug_installed'));
320
321		foreach ($plugins as $plugin)
322		{
323			if(is_readable(e_PLUGIN.$plugin.'/e_linkgen.php'))
324			{
325				require_once (e_PLUGIN.$plugin.'/e_linkgen.php');
326			}
327		}
328		$this->sublink_data = $sublink_type;
329		if($name)
330		{
331			return $sublink_type[$name];
332		}
333
334		return $sublink_type;
335	}
336
337	function generateSublinks($sublink)
338	{
339
340		$mes = e107::getMessage();
341		$subtype = $this->getPosted('sublink_type');//$_POST['sublink_type'];
342		$pid = intval($this->getPosted('link_parent'));
343		$sublink = $this->sublink_data($subtype);
344
345		if(!$pid)
346		{
347		//	$mes->addWarning(LCLAN_109);
348		//	return;
349		}
350		if(!$subtype)
351		{
352			$mes->addWarning(LCLAN_110);
353			return;
354		}
355		if(!$sublink)
356		{
357			$mes->addError(LCLAN_111);
358			return;
359		}
360
361		$sublink = $this->sublink_data($subtype);
362
363
364		$sql = e107::getDb();
365		$sql2 = e107::getDb('sql2');
366
367
368		$sql->select("links", "*", "link_id=".$pid);
369		$par = $sql->fetch();
370
371		//extract($par);
372		// Added option for passing of result array
373		if(vartrue($sublink['result']))
374		{
375			$count = 1;
376			foreach ($sublink['result'] as $row)
377			{
378				$subcat = $row[($sublink['fieldid'])];
379				$name = $row[($sublink['fieldname'])];
380				$subname = $name; // eliminate old embedded hierarchy from names. (e.g. 'submenu.TopName.name')
381
382				if(!empty($sublink['sef']))
383				{
384					$suburl = e107::url($sublink['sef'], $row);
385				}
386				else
387				{
388					$suburl = str_replace("#", $subcat, $sublink['url']);
389				}
390
391				$subicon = ($sublink['fieldicon']) ? $row[($sublink['fieldicon'])] : $par['link_button'];
392				$subdiz = ($sublink['fielddiz']) ? $row[($sublink['fielddiz'])] : $par['link_description'];
393				$subparent = $pid;
394
395				$insert_array = array(
396						'link_name'			=> $subname,
397						'link_url'			=> $suburl,
398						'link_description'	=> $subdiz,
399						'link_button'		=> $subicon,
400						'link_category'		=> $par['link_category'],
401						'link_order'		=> $count,
402						'link_parent'		=> $subparent,
403						'link_open'			=> $par['link_open'],
404						'link_class'		=> $par['link_class'],
405						'link_function'		=> ''
406				);
407				$count++;
408			}
409		}
410		else
411		{
412			$sql->select($sublink['table'], "*", $sublink['query']);
413			$count = 1;
414			while($row = $sql->fetch())
415			{
416				$subcat = $row[($sublink['fieldid'])];
417				$name = $row[($sublink['fieldname'])];
418				$subname = $name; // eliminate old embedded hierarchy from names. (e.g. 'submenu.TopName.name')
419				if(!empty($sublink['sef']))
420				{
421					$suburl = e107::url($sublink['sef'], $row);
422				}
423				else
424				{
425					$suburl = str_replace("#", $subcat, $sublink['url']);
426				}
427				$subicon = ($sublink['fieldicon']) ? $row[($sublink['fieldicon'])] : $par['link_button'];
428				$subdiz = ($sublink['fielddiz']) ? $row[($sublink['fielddiz'])] : $par['link_description'];
429				$subparent = $pid;
430
431				$insert_array = array(
432						'link_name'			=> $subname,
433						'link_url'			=> $suburl,
434						'link_description'	=> $subdiz,
435						'link_button'		=> $subicon,
436						'link_category'		=> vartrue($par['link_category'],1),
437						'link_order'		=> $count,
438						'link_parent'		=> $subparent,
439						'link_open'			=> $par['link_open'],
440						'link_class'		=> intval($par['link_class']),
441						'link_function'		=> ''
442				);
443
444				e107::getMessage()->addDebug(print_a($insert_array,true));
445
446				if($sql2->insert("links",$insert_array))
447				{
448					$message = LAN_CREATED." ({$name})[!br!]";
449					$mes->addSuccess(LAN_CREATED." ({$name})");
450				} else
451				{
452					$message = LAN_CREATED_FAILED." ({$name})[!br!]";
453					$mes->addError(LAN_CREATED_FAILED." ({$name})");
454				}
455				$count++;
456			}
457		}
458
459		if($message) // TODO admin log
460		{
461			// sitelinks_adminlog('01', $message); // 'Sublinks generated'
462		}
463	}
464
465	/**
466	 * Product tree model
467	 * @return links_model_admin_tree
468	 */
469	public function _setTreeModel()
470	{
471		$this->_tree_model = new links_model_admin_tree();
472		return $this;
473	}
474
475	/**
476	 * Link ordered array
477	 * @return array
478	 */
479	public function getLinkArray($current_id = 0)
480	{
481		if(null === $this->_link_array)
482		{
483			if($this->getAction() != 'list')
484			{
485				$this->getTreeModel()->setParam('order', 'ORDER BY '.$this->listOrder)->loadBatch();
486			}
487			/** @var e_tree_modell $tree */
488			$tree = $this->getTreeModel()->getTree();
489			$this->_link_array = array();
490			foreach ($tree as $id => $model)
491			{
492				if($current_id == $id) continue;
493				$this->_link_array[$model->get('link_parent')][$id] = $model->get('link_name');
494			}
495			asort($this->_link_array);
496		}
497
498		return $this->_link_array;
499	}
500}
501
502class links_model_admin_tree extends e_admin_tree_model
503{
504	public $modify = false;
505	public $current_id = 0;
506
507	protected $_db_table = 'links';
508	protected $_link_array	= null;
509	protected $_link_array_modified	= null;
510
511	protected $_field_id = 'link_id';
512
513
514	/**
515	 * Get array of models
516	 * Custom tree order
517	 * @return array
518	 */
519	function getTree($force = false)
520	{
521		return $this->getOrderedTree($this->modify);
522	}
523
524	/**
525	 * Get ordered by their parents models
526	 * @return array
527	 */
528	function getOrderedTree($modified = false)
529	{
530		$var = !$modified ? '_link_array' : '_link_array_modified';
531		if(null === $this->$var)
532		{
533			$tree = $this->get('__tree', array());
534
535			$this->$var = array();
536			$search = array();
537			foreach ($tree as $id => $model)
538			{
539				$search[$model->get('link_parent')][$id] = $model;
540			}
541			asort($search);
542			$this->_tree_order($this->current_id, $search, $this->$var, 0, $modified);
543		}
544		//$this->buildTreeIndex();
545		return $this->$var;
546	}
547
548	/**
549	 * Reorder current tree
550	 * @param $parent_id
551	 * @param $search
552	 * @param $src
553	 * @param $level
554	 * @return void
555	 */
556	function _tree_order($parent_id, $search, &$src, $level = 0, $modified = false)
557	{
558		if(!isset($search[$parent_id]))
559		{
560			return;
561		}
562
563		$level_image = $level ? '<img src="'.e_IMAGE_ABS.'generic/branchbottom.gif" class="icon" alt="" style="margin-left: '.($level * 20).'px" />&nbsp;' : '';
564		foreach ($search[$parent_id] as $model)
565		{
566			$id = $model->get('link_id');
567			$src[$id] = $model;
568			if($modified)
569			{
570				$model->set('link_name', $this->bcClean($model->get('link_name')))
571					->set('link_indent', $level_image);
572			}
573			$this->_tree_order($id, $search, $src, $level + 1, $modified);
574		}
575	}
576
577
578	function bcClean($link_name)
579	{
580		if(substr($link_name, 0,8) == 'submenu.') // BC Fix.
581		{
582			list($tmp,$tmp2,$link) = explode('.', $link_name, 3);
583		}
584		else
585		{
586			$link = $link_name;
587		}
588
589		return $link;
590	}
591
592}
593
594
595class links_admin_form_ui extends e_admin_form_ui
596{
597	protected $current_parent = null;
598
599	private $linkFunctions;
600
601	function init()
602	{
603
604		$tp = e107::getParser();
605		$tmp = e107::getAddonConfig('e_sitelink','sitelink');
606
607		foreach($tmp as $cat=> $array)
608		{
609			$func = array();
610			foreach($array as $val)
611			{
612				$newkey = $cat.'::'.$val['function'];
613				if(vartrue($val['parm']))
614				{
615					$newkey .= "(".$val['parm'].")";
616				}
617				$func[$newkey] = $tp->toHTML($val['name'],'','TITLE');
618			}
619			$this->linkFunctions[$cat] = $func;
620		}
621
622		$sitetheme = e107::getPref('sitetheme');
623
624		if(!file_exists(e_THEME.$sitetheme.'/theme_shortcodes.php'))
625		{
626			return null;
627		}
628
629		require_once(e_THEME.$sitetheme.'/theme_shortcodes.php');
630		$methods = get_class_methods('theme_shortcodes');
631
632		asort($methods);
633
634		$cat = defset('LINKLAN_10',"Theme Shortcodes");
635
636		foreach($methods as $func)
637		{
638			if(strpos($func,'sc_') !== 0)
639			{
640				continue;
641			}
642
643			$newkey = 'theme::'.$func;
644
645			$this->linkFunctions[$cat][$newkey] = str_replace('sc_','',$func);
646		}
647
648
649	//	var_dump($methods );
650
651
652	}
653
654	function link_parent($value, $mode)
655	{
656		switch($mode)
657		{
658			case 'read':
659				$current = $this->getController()->current_parent;
660				if($current) // show only one parent
661				{
662					if(null === $this->current_parent)
663					{
664						if(e107::getDb()->db_Select('links', 'link_name', 'link_id='.$current))
665						{
666							$tmp = e107::getDb()->db_Fetch();
667							$this->current_parent = $tmp['link_name'];
668						}
669					}
670				}
671				$cats	= $this->getController()->getLinkArray();
672				$ret	= array();
673				$this->_parents($value, $cats, $ret);
674				if($this->current_parent) array_unshift($ret, $this->current_parent);
675				return ($ret ? implode('&nbsp;&raquo;&nbsp;', $ret) : '-');
676			break;
677
678			case 'write':
679				$catid	= $this->getController()->getId();
680				$cats	= $this->getController()->getLinkArray($catid);
681				$ret	= array();
682				$this->_parent_select_array(0, $cats, $ret);
683				return $this->selectbox('link_parent', $ret, $value, array('size'=>'xlarge','default' => LAN_SELECT."..."));
684			break;
685
686			case 'batch':
687			case 'filter':
688				$cats	= $this->getController()->getLinkArray();
689
690				$ret[0]	= $mode == 'batch' ? 'REMOVE PARENT' : 'Main Only';
691				$this->_parent_select_array(0, $cats, $ret);
692				return $ret;
693			break;
694		}
695	}
696
697	function link_function($curVal,$mode)
698	{
699		if($mode == 'read')
700		{
701			return $curVal; //  $this->linkFunctions[$curVal];
702		}
703
704		if($mode == 'write')
705		{
706			return $this->selectbox('link_function',$this->linkFunctions,$curVal,array('size'=>'xlarge','default'=> "(".LAN_OPTIONAL.")"));
707		}
708
709		else
710		{
711			return $this->linkFunctions;
712		}
713	}
714
715	function link_id($curVal,$mode)
716	{
717		if($mode == 'read')
718		{
719			$linkUrl = $this->getController()->getListModel()->get('link_url');
720
721
722
723			$url = $this->link_url($linkUrl,'link_id');
724
725
726
727			return "<a href='".$url."' rel='external'>".$curVal."</a>"; //  $this->linkFunctions[$curVal];
728		}
729	}
730
731
732	function link_sefurl($curVal,$mode)
733	{
734		if($mode == 'read')
735		{
736			$plugin = $this->getController()->getModel()->get('link_owner');
737			return $curVal; //  $this->linkFunctions[$curVal];
738		}
739
740		if($mode == 'write')
741		{
742			$plugin = $this->getController()->getModel()->get('link_owner');
743			$obj    = e107::getAddon($plugin,'e_url');
744			$config = e107::callMethod($obj,'config');
745			$opts   = array();
746
747			if(empty($config))
748			{
749				return $this->hidden('link_sefurl','')."<span class='label label-warning'>".LAN_NOT_AVAILABLE."</span>";
750			}
751
752			foreach($config as $k=>$v)
753			{
754				if($k == 'index' || (strpos($v['regex'],'(') === false)) // only provide urls without dynamic elements.
755				{
756					$opts[] = $k;
757				}
758			}
759
760			sort($opts);
761
762			return $this->select('link_sefurl', $opts, $curVal, array('useValues'=>true,'defaultValue'=>'','default'=>'('.LAN_DISABLED.')'));
763		}
764
765	}
766
767	function link_url($curVal,$mode)
768	{
769		if($mode == 'read' || $mode == 'link_id') // read = display mode, link_id = actual absolute URL
770		{
771			$owner = $this->getController()->getListModel()->get('link_owner');
772			$sef =  $this->getController()->getListModel()->get('link_sefurl');
773
774			if($curVal[0] !== '{' && substr($curVal,0,4) != 'http' && $mode == 'link_id')
775			{
776				$curVal = '{e_BASE}'.$curVal;
777			}
778
779			if(!empty($owner) && !empty($sef))
780			{
781				$opt = ($mode == 'read') ? array('mode'=>'raw') : array();
782				$curVal = e107::url($owner,$sef, null, $opt);
783			}
784			else
785			{
786				$opt = ($mode == 'read') ? 'rel' : 'abs';
787				$curVal = e107::getParser()->replaceConstants($curVal,$opt);
788			}
789
790			e107::getDebug()->log($curVal);
791
792			return $curVal; //  $this->linkFunctions[$curVal];
793		}
794
795		if($mode == 'write')
796		{
797			$owner = $this->getController()->getModel()->get('link_owner');
798			$sef =  $this->getController()->getModel()->get('link_sefurl');
799
800			if(!empty($owner) && !empty($sef))
801			{
802
803				$text = str_replace(e_HTTP,'',e107::url($owner,$sef)); // dynamically created.
804				$text .= $this->hidden('link_url',$curVal);
805				$text .= " <span class='label label-warning'>".LAN_AUTO_GENERATED."</span>";
806
807				return $text;
808
809			}
810
811			return $this->text('link_url', $curVal, 255,  array('size'=>'xxlarge'));
812		}
813
814		if($mode == 'inline')
815		{
816			$sef =  $this->getController()->getListModel()->get('link_sefurl');
817
818			if(empty($sef))
819			{
820				return array('inlineType'=>'text');
821			}
822
823			return false;
824		}
825
826	}
827
828	/**
829	 *
830	 * @param integer $category_id
831	 * @param array $search
832	 * @param array $src
833	 * @param boolean $titles
834	 * @return array
835	 */
836	function _parents($link_id, $search, &$src, $titles = true)
837	{
838		foreach ($search as $parent => $cat)
839		{
840			if($cat && array_key_exists($link_id, $cat))
841			{
842				array_unshift($src, ($titles ? $cat[$link_id] : $link_id));
843				if($parent > 0)
844				{
845					$this->_parents($parent, $search, $src, $titles);
846				}
847			}
848		}
849	}
850
851	function _parent_select_array($parent_id, $search, &$src, $strpad = '&nbsp;&nbsp;&nbsp;', $level = 0)
852	{
853		if(!isset($search[$parent_id]))
854		{
855			return;
856		}
857
858		foreach ($search[$parent_id] as $id => $title)
859		{
860			$src[$id] = str_repeat($strpad, $level).($level != 0 ? '-&nbsp;' : '').$title;
861			$this->_parent_select_array($id, $search, $src, $strpad, $level + 1);
862		}
863	}
864
865	function _has_parent($link_id, $parent_id, $cats)
866	{
867		$path = array();
868		$this->_parents($link_id, $cats, $path, false);
869		return in_array($parent_id, $path);
870	}
871
872	/**
873	 * New core feature - triggered before values are rendered
874	 */
875	function renderValueTrigger(&$field, &$value, &$params, $id)
876	{
877		if($field !== 'link_name') return;
878		$tree = $this->getController()->getTreeModel();
879		// notify we need modified tree
880		$tree->modify = true;
881
882		//retrieve array of data models
883		$data = $tree->getTree();
884		// retrieve the propper model by id
885		$model = varset($data[$id]);
886
887		if(!$model) return;
888
889		// Add indent as 'pre' parameter
890		$params['pre'] = $model->get('link_indent');
891	}
892
893	/**
894	 * Override Create list view
895	 *
896	 * @return string
897	 */
898	public function getList($ajax = false, $view='default')
899	{
900		$tp = e107::getParser();
901		$controller = $this->getController();
902
903		$request = $controller->getRequest();
904		$id = $this->getElementId();
905		$tree = $options = array();
906		$tree[$id] = clone $controller->getTreeModel();
907		$tree[$id]->modify = true;
908
909		// if going through confirm screen - no JS confirm
910		$controller->setFieldAttr('options', 'noConfirm', $controller->deleteConfirmScreen);
911
912		$options[$id] = array(
913			'id' => $this->getElementId(), // unique string used for building element ids, REQUIRED
914			'pid' => $controller->getPrimaryName(), // primary field name, REQUIRED
915			//'url' => e_SELF, default
916			//'query' => $request->buildQueryString(array(), true, 'ajax_used'), - ajax_used is now removed from QUERY_STRING - class2
917			'head_query' => $request->buildQueryString('field=[FIELD]&asc=[ASC]&from=[FROM]', false), // without field, asc and from vars, REQUIRED
918			'np_query' => $request->buildQueryString(array(), false, 'from'), // without from var, REQUIRED for next/prev functionality
919			'legend' => $controller->getPluginTitle(), // hidden by default
920			'form_pre' => !$ajax ? $this->renderFilter($tp->post_toForm(array($controller->getQuery('searchquery'), $controller->getQuery('filter_options'))), $controller->getMode().'/'.$controller->getAction()) : '', // needs to be visible when a search returns nothing
921			'form_post' => '', // markup to be added after closing form element
922			'fields' => $controller->getFields(), // see e_admin_ui::$fields
923			'fieldpref' => $controller->getFieldPref(), // see e_admin_ui::$fieldpref
924			'table_pre' => '', // markup to be added before opening table element
925			'table_post' => !$tree[$id]->isEmpty() ? $this->renderBatch(array('delete'=>$controller->getBatchDelete(),'copy'=>$controller->getBatchCopy())) : '',
926			'fieldset_pre' => '', // markup to be added before opening fieldset element
927			'fieldset_post' => '', // markup to be added after closing fieldset element
928			'perPage' => $controller->getPerPage(), // if 0 - no next/prev navigation
929			'from' => $controller->getQuery('from', 0), // current page, default 0
930			'field' => $controller->getQuery('field'), //current order field name, default - primary field
931			'asc' => $controller->getQuery('asc', 'desc'), //current 'order by' rule, default 'asc'
932		);
933		//$tree[$id]->modify = false;
934		return $this->renderListForm($options, $tree, $ajax);
935	}
936}
937
938new links_admin();
939
940
941require_once(e_ADMIN."auth.php");
942
943e107::getAdminUI()->runPage();
944// TODO Link Preview. (similar to userclass preview)
945/*
946echo "<h2>Preview (To-Do)</h2>";
947echo $tp->parseTemplate("{SITELINKS_ALT}");
948*/
949require_once(e_ADMIN."footer.php");
950exit;
951
952
953
954
955
956
957
958?>
959