Deutschland United States United Kingdom
ContentLion - Open Source CMS

Compare Revisions

Ignore whitespace Rev 1 → Rev 2

/trunk/info.php
New file
0,0 → 1,12
<?PHP
$pluginInfo = new PluginInfo();
$pluginInfo->path = "menu";
$pluginInfo->name = "Menu";
$pluginInfo->authorName = "Stefan Wienströer";
$pluginInfo->authorLink = "http://www.contentlion.de";
$pluginInfo->version = "1.0.0";
$pluginInfo->license = "GPL 3";
$pluginInfo->licenseUrl = "http://www.gnu.org/licenses/gpl.html";
$pluginInfo->supportedLanguages = array("de","en");
$this->Add($pluginInfo);
?>
/trunk/languages/en.php
New file
0,0 → 1,3
<?php
$tokens['plugin_description'] = 'Api for a easy access to the menus';
?>
/trunk/languages/de.php
New file
0,0 → 1,3
<?PHP
$tokens['plugin_description'] = 'API zum besseren Zugriff auf die Menüs';
?>
/trunk/package.xml
New file
0,0 → 1,33
<?xml version="1.0" encoding="UTF-8"?>
<contentlion_package>
<info>
<dataname>menu</dataname>
<displayname>Menu</displayname>
<short_description lang="de">Api zum einfchen Zugriff auf die Menüs</short_description>
<revision>1</revision>
</info>
<files download_zip="https://ssl.kundenserver.de/ssl.contentlion.org/download/plugins/menu/1/package.zip">
<folder name="system">
<folder name="plugins">
<folder name="menu">
<folder name="classes">
<file name="entry.php" />
<file name="entryfilter.php" />
<file name="externalentry.php" />
<file name="filter.php" />
<file name="menu.php" />
<file name="pageentry.php" />
<file name="submenuentry.php" />
</folder>
<folder name="language">
<file name="de.php" />
<file name="en.php" />
</folder>
<file name="info.php" />
</folder>
</folder>
</folder>
</files>
<dependencies>
</dependencies>
</contentlion_package>
/trunk/classes/pageentry.php
New file
0,0 → 1,54
<?php
class Plugin_Menu_PageEntry extends Plugin_Menu_Entry
{
private $page = null;
/**
* Type
* @return int
*/
public function type()
{
return 0;
}
 
/**
* destination url
*
* @return string
*/
public function url()
{
$res = null;
if($this->page() != null)
{
$res = $this->page()->GetUrl();
}
return $res;
}
/**
* Linked page
*
* @param Page $new_value optional
* @return Page
*/
public function page(Page $new_value = null)
{
if(isset($new_value))
{
$this->page = $new_value;
$this->href($new_value->id);
}
else if(is_null($this->page))
{
$this->page = new Page();
$this->page->loadPropertiesById($this->href());
}
return $this->page;
}
}
?>
/trunk/classes/submenuentry.php
New file
0,0 → 1,74
<?php
class Plugin_Menu_SubmenuEntry extends Plugin_Menu_Entry
{
private $show_title = false;
private $submenu = null;
 
/**
* Type
* @return int
*/
public function type()
{
return $this->show_title() ? 4 : 3;
}
 
/**
* Url
*
* @return null
*/
public function url()
{
//Submenu
return null;
}
/**
* Show menu title in code?
*
* @param string $new_value optional
* @return string
*/
public function show_title($new_value = null)
{
if(is_bool($new_value))
{
$this->show_title = $new_value;
}
else if(is_numeric($new_value))
{
$this->show_title = $new_value == 1;
}
else if(is_string($new_value))
{
$this->show_title = strtolower(trim($new_value)) == "yes";
}
return $this->show_title;
}
/**
* Submenu
*
* @param Plugin_Menu_Menu $new_value optional
* @return Plugin_Menu_Menu
*/
public function submenu(Plugin_Menu_Menu $new_value = null)
{
if(isset($new_value))
{
$this->submenu = $new_value;
$this->href($new_value->id());
}
else if(is_null($this->submenu))
{
$filter = new Plugin_Menu_Filter();
$filter->ids(array($this->href()));
$this->submenu = $filter->first();
}
return $this->submenu;
}
}
?>
/trunk/classes/entry.php
New file
0,0 → 1,198
<?php
abstract class Plugin_Menu_Entry
{
private $id = null;
private $menu_id = null;
private $title = null;
private $href = null;
private $menu = null;
/**
* Build an menu entry by menu-row
*
* @param stdClass $data data like table menu
* @return Plugin_Menu_Entry
*/
public static function get(stdClass $data)
{
$res = null;
switch(@$data->type)
{
case 0:
$res = new Plugin_Menu_PageEntry();
break;
case 1:
$res = new Plugin_Menu_ExternalEntry();
break;
case 2:
$res = new Plugin_Menu_SubmenuEntry();
break;
case 3:
$res = new Plugin_Menu_SubmenuEntry();
$res->show_title(true);
break;
}
if(isset($res))
{
$res->id(@$data->id);
$res->menu_id(@$data->menuID);
$res->title(@$data->title);
$res->href(@$data->href);
}
return $res;
}
/**
* Id
*
* @param int $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return int
*/
public function id($new_value = null, $allow_reset = false)
{
if(is_numeric($new_value))
{
$this->id = $new_value;
}
else if(is_null($new_value) && $allow_reset)
{
$this->id = null;
}
return $this->id;
}
/**
* ID of the menu
*
* @param int $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return int
*/
public function menu_id($new_value = null, $allow_reset = false)
{
if(is_numeric($new_value))
{
$this->menu_id = $new_value;
$this->menu = null;
}
else if(is_null($new_value) && $allow_reset)
{
$this->menu_id = null;
$this->menu = null;
}
return $this->menu_id;
}
/**
* Title
*
* @param string $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return string
*/
public function title($new_value = null, $allow_reset = false)
{
if(is_string($new_value))
{
$this->title = $new_value;
}
else if(is_null($new_value) && $allow_reset)
{
$this->title = null;
}
return $this->title;
}
/**
* href from database.
* Please use url() for the real url.
*
* @param string $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return string
*/
public function href($new_value = null, $allow_reset = false)
{
if(is_string($new_value) || is_numeric($new_value))
{
$this->href = $new_value;
}
else if(is_null($new_value) && $allow_reset)
{
$this->href = null;
}
return $this->href;
}
/**
* menu
*
* @param Plugin_menu_menu $new_value optional
* @return Plugin_menu_menu
*/
public function menu(Plugin_menu_menu $new_value = null)
{
if(isset($new_value))
{
$this->menu = $new_value;
$this->menu_id = $new_value->id();
}
else if(is_null($this->menu))
{
$filter = new Plugin_Menu_Filter();
$filter->ids(array($this->menu_id()));
$this->menu = $filter->first();
}
return $this->menu;
}
/**
* Save the entry
*/
public function save()
{
$sql = "REPLACE INTO {'dbprefix'}menu "
."SET id = '".DataBase::Current()->EscapeString($this->id())."', "
."menuID = '".DataBase::Current()->EscapeString($this->menu_id())."', "
."title = '".utf8_decode(DataBase::Current()->EscapeString($this->title()))."', "
."href = '".DataBase::Current()->EscapeString($this->href())."', "
."type = '".DataBase::Current()->EscapeString($this->type())."'";
DataBase::Current()->Execute($sql);
}
/**
* Delete the entry
*/
public function delete()
{
$sql = "DELETE FROM {'dbprefix'}menu "
."WHERE id = '".DataBase::Current()->EscapeString($this->id())."' "
."&& menuID = '".DataBase::Current()->EscapeString($this->menu_id())."'";
DataBase::Current()->Execute($sql);
}
 
/**
* Type id from database
*
* @return int
*/
abstract function type();
/**
* Url
* @return string
*/
abstract function url();
}
?>
/trunk/classes/externalentry.php
New file
0,0 → 1,24
<?php
class Plugin_Menu_ExternalEntry extends Plugin_Menu_Entry
{
/**
* Type
*
* @return int
*/
public function type()
{
return 1;
}
 
/**
* Url
*
* @return string
*/
public function url()
{
return str_replace("{VAR:HOST}",Settings::getInstance()->get("host"),$this->href());
}
}
?>
/trunk/classes/menu.php
New file
0,0 → 1,205
<?php
class Plugin_Menu_Menu
{
private $id = null;
private $name = null;
private $dictionary = null;
private $entries = null;
/**
* A main menu object
*
* @param stdClass $data prefill data. Syntax like menu_names table
*/
public function __construct(stdClass $data = null)
{
if(isset($data))
{
$this->id(@$data->id);
$this->name(@$data->name);
$this->dictionary(@$data->dir);
}
}
/**
* ID
*
* @param int $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return int
*/
public function id($new_value = null, $allow_reset = false)
{
if(is_numeric($new_value))
{
$this->id = round($new_value);
}
else if(is_null($new_value) && $allow_reset)
{
$this->id = null;
}
return $this->id;
}
/**
* Name
*
* @param string $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return string
*/
public function name($new_value = null, $allow_reset = false)
{
if(is_string($new_value))
{
$this->name = $new_value;
}
else if(is_null($new_value) && $allow_reset)
{
$this->name = null;
}
return $this->name;
}
/**
* Dictionary where the menu is saved in
*
* @param string $new_value optional
* @param boolean $allow_reset Can $new_value be null?
* @return string
*/
public function dictionary($new_value = null, $allow_reset = false)
{
if(is_string($new_value))
{
$this->dictionary = $new_value;
}
else if(is_null($new_value) && $allow_reset)
{
$this->dictionary = null;
}
return $this->dictionary;
}
/**
* Save the menu
*/
public function save()
{
$sql = "REPLACE INTO {'dbprefix'}menu_names "
."SET name = '".utf8_decode(DataBase::Current()->EscapeString($this->name()))."', "
."dir = '".DataBase::Current()->EscapeString($this->dictionary())."' ";
if($this->id() != null)
{
$sql .= ",id = '".DataBase::Current()->EscapeString($this->id())."'";
}
DataBase::Current()->Execute($sql);
if($this->id() == null)
{
$this->id(DataBase::Current()->InsertID());
}
//Entries laden, falls noch nicht da
$this->entries();
$sql = "DELETE FROM {'dbprefix'}menu "
."WHERE menuID = '".DataBase::Current()->EscapeString($this->id())."'";
DataBase::Current()->Execute($sql);
$i = 1;
foreach($this->entries() as $entry)
{
$entry->id($i);
$entry->menu_id($this->id());
$entry->save();
$i++;
}
EventManager::RaiseEvent("plugin_menu_menu_saved", array("menu" => $this));
Cache::clear("menu");
}
/**
* Menu entries
* @param array(Plugin_Menu_Entry) $new_value
* @return array(Plugin_Menu_Entry)
*/
public function entries(array $new_value = null)
{
if(isset($new_value))
{
$this->entries = array();
foreach($new_value as $item)
{
if(is_a($item,"Plugin_Menu_Entry"))
{
$item->menu_id($this->id());
$this->entries[] = $item;
}
}
}
else if(is_null($this->entries) && $this->id() != null)
{
$this->entries = array();
$filter = new Plugin_Menu_EntryFilter();
$filter->menu_ids(array($this->id()));
foreach($filter as $item)
{
$this->entries[] = $item;
}
}
return $this->entries;
}
/**
* Add an entry to the menu
*
* @param Plugin_Menu_Entry $entry
*/
public function add_entry(Plugin_Menu_Entry $entry)
{
$this->entries();
$this->entries[] = $entry;
$entry->menu_id($this->id());
}
/**
* Delete the menu including the entries
*/
public function delete()
{
$sql = "DELETE FROM {'dbprefix'}menu_names "
."WHERE id = '".Database::Current()->EscapeString($this->id())."'";
database::Current()->Execute($sql);
foreach($this->entries() as $entry)
{
$entry->delete();
}
$this->entries = array();
EventManager::RaiseEvent("plugin_menu_menu_deleted", array("menu" => $this));
Cache::clear("menu");
}
 
public function __sleep() {
return array("id","name","dir");
}
public function __toString()
{
return trim($this->name()." (".$this->id().")");
}
}
?>
/trunk/classes/filter.php
New file
0,0 → 1,209
<?php
class Plugin_Menu_Filter implements Iterator
{
private $ids = null;
private $names = null;
private $dictionaries = null;
private $items = array();
private $index = null;
/**
* Ids
*
* @param array(int) $new_value optional
* @return array(int)
*/
public function ids(array $new_value = null)
{
if(isset($new_value))
{
$this->ids = array();
foreach($new_value as $item)
{
if(is_numeric($item))
{
$this->ids[] = $item;
}
}
}
return $this->ids;
}
/**
* Filter by names
*
* @param array(string) $new_value optional
* @return array(string)
*/
public function names(array $new_value = null)
{
if(isset($new_value))
{
$this->names = array();
foreach($new_value as $item)
{
if(is_string($item))
{
$this->names[] = $item;
}
}
}
return $this->names;
}
/**
* Filter by dictionaries
*
* @param array(string) $new_value
* @return array(string)
*/
public function dictionaries(array $new_value = null)
{
if(isset($new_value))
{
$this->dictionaries = array();
foreach($new_value as $item)
{
if(is_string($item))
{
$this->dictionaries[] = $item;
}
}
}
return $this->dictionaries;
}
 
 
/**
* current item
*
* @return Plugin_Menu_Menu
*/
public function current()
{
return $this->items[$this->index];
}
 
/**
* index
*
* @return int
*/
public function key()
{
return $this->index;
}
 
/**
* Move cursor to the next element
*/
public function next()
{
$this->index++;
}
 
/**
* Start new iteration
*/
public function rewind()
{
$this->items = array();
$where = array();
if($this->ids() != null)
{
if(count($this->ids) == 0)
{
return;
}
$where[] = "id IN ('".implode("','",$this->ids())."')";
}
if($this->names() != null)
{
if(count($this->names) == 0)
{
return;
}
$names_escaped = array();
foreach($this->names() as $name)
{
$names_escaped[] = DataBase::Current()->EscapeString($name);
}
$where[] = "name IN ('".implde("','",$names_escaped)."')";
}
if($this->dictionaries() != null)
{
if(count($this->dictionaries()) == 0)
{
return;
}
$dictionaries_escaped = array();
foreach($this->dictionaries() as $dictionaries)
{
$dictionaries_escaped[] = DataBase::Current()->EscapeString($dictionaries);
}
$where[] = "dir IN ('".implde("','",$dictionaries_escaped)."')";
}
$sql = "SELECT * "
."FROM {'dbprefix'}menu_names ";
if(count($where) > 0)
{
$sql .= "WHERE ".implode(" && ",$where);
}
$rows = DataBase::Current()->ReadRows($sql);
foreach($rows as $row)
{
$this->items[] = new Plugin_Menu_Menu($row);
}
$this->index = null;
}
 
/**
* Is the current item valid?
*
* @return boolean
*/
public function valid()
{
if(is_null($this->index))
{
$this->index = 0;
}
return count($this->items) > $this->index;
}
/**
* The first menu
*
* @return Plugin_Menu_Menu
*/
public function first()
{
$res = null;
$this->rewind();
if(count($this->items) > 0)
{
$res = $this->items[0];
}
return $res;
}
}
?>
/trunk/classes/entryfilter.php
New file
0,0 → 1,252
<?php
class Plugin_Menu_EntryFilter implements Iterator
{
private $ids = null;
private $menu_ids = null;
private $titles = null;
private $hrefs = null;
private $types = null;
private $items = array();
const TYPE_PAGE = 0;
const TYPE_EXTERNAL = 1;
const TYPE_SUBMENU = 2;
const TYPE_SUBMENU_WITH_TITLE = 3;
 
/**
* Ids
*
* @param array(int) $new_value optional
* @return array(int)
*/
public function ids(array $new_value = null)
{
if(isset($new_value))
{
$this->ids = array();
foreach($new_value as $item)
{
if(is_numeric($item))
{
$this->ids[] = $item;
}
}
}
return $this->ids;
}
/**
* menu ids
*
* @param array(int) $new_value optional
* @return array(int)
*/
public function menu_ids(array $new_value = null)
{
if(isset($new_value))
{
$this->menu_ids = array();
foreach($new_value as $item)
{
if(is_numeric($item))
{
$this->menu_ids[] = $item;
}
}
}
return $this->menu_ids;
}
/**
* titles
*
* @param array(string) $new_value optional
* @return array(string)
*/
public function titles(array $new_value = null)
{
if(isset($new_value))
{
$this->titles = array();
foreach($new_value as $item)
{
if(is_string($item))
{
$this->titles[] = $item;
}
}
}
return $this->titles;
}
/**
* hrefs
*
* @param array(string) $new_value optional
* @return array(string)
*/
public function hrefs(array $new_value = null)
{
if(isset($new_value))
{
$this->hrefs = array();
foreach($new_value as $item)
{
if(is_string($item))
{
$this->hrefs[] = $item;
}
}
}
return $this->hrefs;
}
/**
* Allowed types
*
* @param array(int) $new_value
* @return array(int)
*/
public function types(array $new_value = null)
{
if(isset($new_value))
{
$this->types = array();
foreach($new_value as $item)
{
if(is_numeric($item))
{
$this->types[] = $item;
}
}
}
return $this->types;
}
 
/**
* Current item
*
* @return Plugin_Menu_Entry
*/
public function current()
{
return $this->items[$this->index];
}
 
/**
* Key
*
* @return int
*/
public function key()
{
return $this->index;
}
 
/**
* Move to the next element
*/
public function next()
{
$this->index++;
}
 
/**
* Move to first element
*/
public function rewind()
{
$this->items = array();
$where = array();
if($this->ids() != null)
{
if(count($this->ids()) == 0)
return;
$where[] = "id IN ('".implode("'",$this->ids())."')";
}
if($this->menu_ids() != null)
{
if(count($this->menu_ids()) == 0)
return;
$where[] = "menuID IN ('".implode("'",$this->menu_ids())."')";
}
if($this->titles() != null)
{
if(count($this->titles()) == 0)
return;
$titles_escaped = array();
foreach($this->titles() as $title)
{
$titles_escaped[] = DataBase::Current()->EscapeString($title);
}
$where[] = "title IN ('".implode("'",$titles_escaped)."')";
}
if($this->hrefs() != null)
{
if(count($this->hrefs()) == 0)
return;
$hrefs_escaped = array();
foreach($this->hrefs() as $href)
{
$hrefs_escaped[] = DataBase::Current()->EscapeString($href);
}
$where[] = "href IN ('".implode("'",$hrefs_escaped)."')";
}
if($this->types() != null)
{
if(count($this->types()) == 0)
return;
$where[] = "type IN ('".implode("'",$this->types())."')";
}
$sql = "SELECT * "
."FROM {'dbprefix'}menu ";
if(count($where) > 0)
{
$sql .= "WHERE ".implode(" && ",$where);
}
$sql .= " ORDER BY menuID, id";
$rows = DataBase::Current()->ReadRows($sql);
foreach($rows as $row)
{
$this->items[] = Plugin_Menu_Entry::get($row);
}
$this->index = null;
}
 
/**
* Is the current element valid?
* @return type
*/
public function valid()
{
if(is_null($this->index))
{
$this->index = 0;
}
return count($this->items) > $this->index;
}
}
?>