-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpi.page2.php
103 lines (88 loc) · 2.02 KB
/
pi.page2.php
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
$plugin_info = array(
'pi_name' => 'Page2',
'pi_version' =>'1.0.0',
'pi_author' =>'Mark Croxton',
'pi_author_url' => 'http://www.hallmark-design.co.uk/',
'pi_description' => 'Get an entry id from a page / structure uri, or vice-versa',
'pi_usage' => Page2::usage()
);
class Page2 {
public $return_data = '';
protected $site_id;
protected $site_pages;
/**
* Constructor
*
* @access public
* @return void
*/
public function __construct()
{
$this->EE =& get_instance();
$this->site_id = $this->_get_site_id();
// get the site page array
$this->site_pages = $this->EE->config->item('site_pages');
$this->site_pages = $this->site_pages[$this->site_id];
}
/**
* Tag: {exp:page2:id uri="/{segment_1}/{segment_2}/"}
*
* @access public
* @return integer
*/
public function id()
{
// the variable we want to find
$uri = $this->EE->TMPL->fetch_param('uri', NULL);
if ( ! is_null($uri))
{
// make sure we have leading and trailing slashes
$uri = '/' . trim($uri, '/') . '/';
// lookup the entry id
if ($entry_id = array_search($uri, $this->site_pages['uris']))
{
return $entry_id;
}
}
}
/**
* Tag: {exp:page2:uri entry_id="143"}
*
* @access public
* @return string
*/
public function uri()
{
// the variable we want to find
$entry_id = $this->EE->TMPL->fetch_param('entry_id', NULL);
if ( ! is_null($entry_id))
{
// lookup the uri
if ( isset($this->site_pages['uris'][$entry_id]) )
{
return $this->site_pages['uris'][$entry_id];
}
}
}
private function _get_site_id()
{
$site_id = is_numeric($this->EE->config->item('site_id')) ? $this->EE->config->item('site_id') : 1;
return $site_id;
}
// usage instructions
public static function usage()
{
ob_start();
?>
-------------------
HOW TO USE
-------------------
{exp:page2:id uri="/{segment_1}/{segment_2}/"}
{exp:page2:uri entry_id="/{segment_1}/{segment_2}/"}
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}