serializedlist.php 2.88 KB
Newer Older
1
<?php
2
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3
//
4 5 6 7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

//this script may only be included - so its better to die if called directly.
9 10 11
if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
	header('location: index.php');
	exit;
12 13 14 15
}

/**
 * SerializedList manages lists of objects in prefs
16
 *
17 18 19 20 21 22 23 24
 * Should be extended in your own class to use
 * TODO refactor toolbars and plugin aliasses to use this
 */
abstract class SerializedList
{
	protected $name = '';
	protected $data;
	protected $prefPrefix;
25

26 27 28 29
	/**
	 * Constructor
	 * poss add the prefPrefix and data init as params
	 * also getting a named item should be a separate step?
30
	 *
31 32
	 * @param string $name
	 */
33 34
	public function __construct($name)
	{
35
		global $prefs;
36

37
		$this->initPrefPrefix();
38 39

		$this->name = strtolower(TikiLib::remove_non_word_characters_and_accents($name));
rjsmelo's avatar
rjsmelo committed
40
		if (! empty($this->name) && ! empty($prefs[$this->getPrefName()])) {
41 42 43 44 45
			$this->loadPref();
		} else {
			$this->initData();
		}
	}
46

47 48
	abstract public function initPrefPrefix();	// to be declared to set $this->prefPrefix = 'your_pref_prefix_'
	abstract public function initData();		// func to set $this->data as you need it
rjsmelo's avatar
rjsmelo committed
49 50 51 52 53
	/**
	 * @param $params
	 * @return mixed
	 */
	abstract public function setData($params);	// func to set the date
54 55 56

	public function getData()
	{
57 58
		return $this->data;
	}
59

rjsmelo's avatar
rjsmelo committed
60 61 62 63
	/**
	 * @return string
	 */
	public function getName()
64
	{
65 66
		return $this->name;
	}
67

rjsmelo's avatar
rjsmelo committed
68 69 70 71
	/**
	 * @return string
	 */
	public function getPrefName()
72
	{
73 74
		return $this->prefPrefix . $this->name;
	}
75

rjsmelo's avatar
rjsmelo committed
76 77 78 79
	/**
	 * @return string
	 */
	public function getListName()
80
	{
81 82
		return $this->prefPrefix . 'list';
	}
83

rjsmelo's avatar
rjsmelo committed
84 85 86 87
	/**
	 * @return array|mixed
	 */
	public function getPrefList()
88
	{
89
		global $prefs;
90

rjsmelo's avatar
rjsmelo committed
91
		if (isset($prefs[$this->getListName()])) {
92 93 94
			$custom = @unserialize($prefs[$this->getListName()]);
			sort($custom);
		} else {
rjsmelo's avatar
rjsmelo committed
95
			$custom = [];
96 97 98 99
		}

		return $custom;
	}
100

rjsmelo's avatar
rjsmelo committed
101 102 103 104
	/**
	 * @return mixed
	 */
	public function loadPref()
105
	{
106
		global $prefs, $tikilib;
107

jonnybradley's avatar
jonnybradley committed
108
		$this->data = unserialize($prefs[$this->getPrefName()]);
109 110
		return $this->data;
	}
111 112 113

	public function savePref()
	{
114
		global $prefs, $tikilib;
115

116
		$list = $this->getPrefList();
117 118 119

		$tikilib->set_preference($this->getPrefName(), serialize($this->data));

rjsmelo's avatar
rjsmelo committed
120
		if (! in_array($this->name, $list)) {
121
			$list[] = $this->name;
122
			$tikilib->set_preference($this->getListName(), serialize($list));
123 124 125
		}
	}

126 127
	public function deletePref()
	{
128 129 130
		global $prefs, $tikilib;

		$prefName = $this->getPrefName();
rjsmelo's avatar
rjsmelo committed
131
		if (isset($prefs[$prefName])) {
132
			$tikilib->delete_preference($prefName);
133 134
		}
		$list = $this->getPrefList();
135

rjsmelo's avatar
rjsmelo committed
136 137
		if (in_array($this->name, $list)) {
			$list = array_diff($list, [$this->name]);
138
			$tikilib->set_preference($this->getListName(), serialize($list));
139 140 141
		}
	}
}