summaryrefslogtreecommitdiff
path: root/smartyplugins/modifier.displayUrl.php
blob: f6e8dc26aa32e5cd9fc1e5efd11d94b78f5c86bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Bitweaver\Plugins;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     modifier
 * Name:     displayUrl
 * Purpose:  give back the display URL
 * If the modification is preformed on a string then the (new <lib>)->getDisplayUrl method is called, (this defaults to BitPage if not specified)
 * If the modification is preformed on an object then, if <lib> is given, then it is passed as the parameter to (new <lib>)->getDisplayUrl otherwise, the object' getDisplayUrl method is called
 * If the modification is preformed on an array then, if <lib> is given, then it is passed as the parameter to (new <lib>)->getDisplayUrl otherwise the following is attempted:
 *     If the array contains an element display_url it is returned
 *     If the array contains an element content_type_guid then lib becomes the handler class of the content_type_guid and the array is passed as the parameter to (new <lib>)->getDisplayUrl
 *     If the array contains an element handler_class then lib becomes the handler_class and the array is passed as the parameter to (new <lib>)->getDisplayUrl
 * --
 * If all of the above tests fail then LibertyContent::getDisplayUrlFromHash with the argument to the modifier passed as the second argument
 * Example: {'My Page'|displayUrl}, {'admin'|displayUrl:BitUser}, {$gContent|displayUrl:MyObject}
 * -------------------------------------------------------------
 */

function smarty_modifier_displayUrl_findLib(&$lib,$class_only=false) {
  global $gLibertySystem;
	if (!class_exists($lib)) {
		foreach ($gLibertySystem->mContentTypes as $type) {
			if ($type['handler_class']==$lib) {
				smarty_modifier_displayUrl_loadLib($type);
				return true;
			} elseif ((!$class_only) && ($type['content_type_guid']==$lib)) {
				$lib = $type['handler_class'];
				smarty_modifier_displayUrl_loadLib($type);
				return true;
			}
		}
		return false;
	}
	return true;
}

function smarty_modifier_displayUrl_loadLib($type) {
	$path = constant(strtoupper($type['handler_package']).'_PKG_PATH');
	require_once($path.$type['handler_file']);
}

function smarty_modifier_displayUrl($pMixed, $lib='') {
	global  $gLibertySystem;
	if (is_string($pMixed)) {
		if (empty($lib)) $lib ='BitPage';
		if (smarty_modifier_displayUrl_findLib($lib)) {
			$call =[$lib, 'getDisplayUrl'];
			if (is_callable($call)) {
				return call_user_func($call,$pMixed);
			}
			$i = $lib();
			if (method_exists($i,'getDisplayUrl')) {
				return $i->getDisplayUrl($pMixed);
			}
		}
	} elseif (is_object($pMixed)) {
		if (!empty($lib)) {
			if (smarty_modifier_displayUrl_findLib($lib)) {
				$i = $lib();
				return $i->getDisplayUrl($pMixed);
			}
		}
		if (method_exists($pMixed,'getDisplayUrl')) {
			return $pMixed->getDisplayUrl();
		}
	} elseif (is_array($pMixed)) {
		if (!empty($lib)) {
			if (smarty_modifier_displayUrl_findLib($lib)) {
				$i = new $lib();
				return $i->getDisplayUrl($pMixed);
			}
		}
		if (!empty($pMixed['display_url'])) {
			return $pMixed['display_url'];
		}
		if (!empty($pMixed['content_type_guid'])) {
			$type =$gLibertySystem->mContentTypes[$pMixed['content_type_guid']];
			if (!empty($type)) {
				$lib = $type['handler_class'];
				smarty_modifier_displayUrl_loadLib($type);
				$i = new $lib();
				return $i->getDisplayUrl($pMixed);
			}
		}
		if (!empty($pMixed['handler_class'])) {
			$lib= $pMixed['handler_class'];
			if (smarty_modifier_displayUrl_findLib($lib,true)) {
				$i = $lib();
				return $i->getDisplayUrl($pMixed);
			}
		}
	}
	return LibertyContent::getDisplayUrlFromHash( $pMixed );
}