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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class Navigation
* @brief Handle the navigation within a module and could create a html navigation bar
*
* This class stores every url that you add to the object in a stack. From
* there it's possible to return the last called url or a previous url. This
* can be used to allow a navigation within a module. It's also possible
* to create a html navigation bar. Therefore you should add a url and a link text
* to the object everytime you submit a url.
* @par Example 1
* @code // start the navigation in a module (the object $gNavigation is created in common.php)
* $gNavigation->addStartUrl('http://www.example.com/index.php', 'Example-Module');
*
* // add a new url from another page within the same module
* $gNavigation->addUrl('http://www.example.com/addentry.php', 'Add Entry');
*
* // optional you can now create the html navigation bar
* $gNavigation->getHtml();
*
* // if you want to remove the last entry from the stack
* $gNavigation->deleteLastUrl(); @endcode
* @par Example 2
* @code // show a navigation bar in your html code
* ... <br /><?php echo $gNavigation->getHtmlNavigationBar('id-my-navigation'); ?><br /> ... @endcode
*/
class Navigation
{
private $urlStack;
/**
* Constructor will initialize the local parameters
*/
public function __construct()
{
$this->urlStack = array();
}
/**
* Initialize the stack and adds a new url to the navigation stack.
* If a html navigation bar should be created later than you should fill the text and maybe the icon.
* @param string $url The url that should be added to the navigation stack.
* @param string $text A text that should be shown in the html navigation stack and
* would be linked with the $url.
* @param string $icon A url to the icon that should be shown in the html navigation stack
* together with the text and would be linked with the $url.
* @return void
*/
public function addStartUrl($url, $text = null, $icon = null)
{
$this->clear();
$this->addUrl($url, $text, $icon);
}
/**
* Add a new url to the navigation stack. If a html navigation bar should be created later
* than you should fill the text and maybe the icon. Before the url will be added to the stack
* the method checks if the current url was already added to the url.
* @param string $url The url that should be added to the navigation stack.
* @param string $text A text that should be shown in the html navigation stack and
* would be linked with the $url.
* @param string $icon A url to the icon that should be shown in the html navigation stack
* together with the text and would be linked with the $url.
* @return bool Returns true if the url got added and false if not.
*/
public function addUrl($url, $text = null, $icon = null)
{
$count = count($this->urlStack);
if($count === 0 || $url !== $this->urlStack[$count - 1]['url'])
{
if($count > 1 && $url === $this->urlStack[$count - 2]['url'])
{
// if the last but one url is equal to the current url then only remove the last url
array_pop($this->urlStack);
}
else
{
// if the current url will not be the last or the last but one then add the current url to stack
$this->urlStack[] = array('url' => $url, 'text' => $text, 'icon' => $icon);
return true;
}
}
return false;
}
/**
* Initialize the url stack and set the internal counter to 0
* @return void
*/
public function clear()
{
$this->urlStack = array();
}
/**
* Number of urls that a currently in the stack
* @return int
*/
public function count()
{
return count($this->urlStack);
}
/**
* Removes the last url from the stack.
* @return string[] Returns the removed element
*/
public function deleteLastUrl()
{
return array_pop($this->urlStack);
}
/**
* Returns html code that contain a link back to the previous url.
* @param string $id Optional you could set an id for the back link
* @return string Returns html code of the navigation back link.
*/
public function getHtmlBackButton($id = 'adm-navigation-back')
{
global $gL10n;
$html = '';
// now get the "new" last url from the stack. This should be the last page
$url = $this->getPreviousUrl();
// if no page was found then show the default homepage
if($url !== '')
{
$html = '
<a class="btn" href="'.$url.'"><img src="'. THEME_PATH. '/icons/back.png"
alt="'.$gL10n->get('SYS_BACK').'" />'.$gL10n->get('SYS_BACK').'</a>';
}
// if entries where found then add div element
if($html !== '')
{
$html = '<div id="'.$id.'" class="admNavigation admNavigationBack">'.$html.'</div>';
}
return $html;
}
/**
* Returns html code that contain links to all previous added urls from the stack.
* The output will look like: @n FirstPage > SecondPage > ThirdPage ...@n
* The last page of this list is always the current page.
* @param string $id Optional you could set an id for the navigation bar
* @return string Returns html code of the navigation bar.
*/
public function getHtmlNavigationBar($id = 'adm-navigation-bar')
{
$html = '';
foreach ($this->urlStack as $url)
{
if(strlen($url['text']) > 0)
{
$html .= '<a href="'.$url['url'].'">'.$url['text'].'</a>';
}
}
// if entries where found then add div element
if($html !== '')
{
$html = '<div id="'.$id.'" class="admNavigation admNavigationBar">'.$html.'</div>';
}
return $html;
}
/**
* Get the previous url from the stack. This is not the last url that was added to the stack!
* @return string|null Returns the previous added url. If only one url is added it returns this one. If no url is added returns null
*/
public function getPreviousUrl()
{
$count = count($this->urlStack);
if($count === 0)
{
return null;
}
// Only one url, take this one
$entry = min(1, $count - 2);
return $this->urlStack[$entry]['url'];
}
/**
* Get the last added url from the stack.
* @return string|null Returns the last added url. If the stack is empty returns null
*/
public function getUrl()
{
$count = count($this->urlStack);
if($count > 0)
{
return $this->urlStack[$count - 1]['url'];
}
return null;
}
}