1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8//this script may only be included - so its better to die if called directly. 9if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) { 10 header('location: index.php'); 11 exit; 12} 13 14/** 15 * SerializedList manages lists of objects in prefs 16 * 17 * Should be extended in your own class to use 18 * TODO refactor toolbars and plugin aliasses to use this 19 */ 20abstract class SerializedList 21{ 22 protected $name = ''; 23 protected $data; 24 protected $prefPrefix; 25 26 /** 27 * Constructor 28 * poss add the prefPrefix and data init as params 29 * also getting a named item should be a separate step? 30 * 31 * @param string $name 32 */ 33 public function __construct($name) 34 { 35 global $prefs; 36 37 $this->initPrefPrefix(); 38 39 $this->name = strtolower(TikiLib::remove_non_word_characters_and_accents($name)); 40 if (! empty($this->name) && ! empty($prefs[$this->getPrefName()])) { 41 $this->loadPref(); 42 } else { 43 $this->initData(); 44 } 45 } 46 47 abstract public function initPrefPrefix(); // to be declared to set $this->prefPrefix = 'your_pref_prefix_' 48 abstract public function initData(); // func to set $this->data as you need it 49 /** 50 * @param $params 51 * @return mixed 52 */ 53 abstract public function setData($params); // func to set the date 54 55 public function getData() 56 { 57 return $this->data; 58 } 59 60 /** 61 * @return string 62 */ 63 public function getName() 64 { 65 return $this->name; 66 } 67 68 /** 69 * @return string 70 */ 71 public function getPrefName() 72 { 73 return $this->prefPrefix . $this->name; 74 } 75 76 /** 77 * @return string 78 */ 79 public function getListName() 80 { 81 return $this->prefPrefix . 'list'; 82 } 83 84 /** 85 * @return array|mixed 86 */ 87 public function getPrefList() 88 { 89 global $prefs; 90 91 if (isset($prefs[$this->getListName()])) { 92 $custom = @unserialize($prefs[$this->getListName()]); 93 sort($custom); 94 } else { 95 $custom = []; 96 } 97 98 return $custom; 99 } 100 101 /** 102 * @return mixed 103 */ 104 public function loadPref() 105 { 106 global $prefs, $tikilib; 107 108 $this->data = unserialize($prefs[$this->getPrefName()]); 109 return $this->data; 110 } 111 112 public function savePref() 113 { 114 global $prefs, $tikilib; 115 116 $list = $this->getPrefList(); 117 118 $tikilib->set_preference($this->getPrefName(), serialize($this->data)); 119 120 if (! in_array($this->name, $list)) { 121 $list[] = $this->name; 122 $tikilib->set_preference($this->getListName(), serialize($list)); 123 } 124 } 125 126 public function deletePref() 127 { 128 global $prefs, $tikilib; 129 130 $prefName = $this->getPrefName(); 131 if (isset($prefs[$prefName])) { 132 $tikilib->delete_preference($prefName); 133 } 134 $list = $this->getPrefList(); 135 136 if (in_array($this->name, $list)) { 137 $list = array_diff($list, [$this->name]); 138 $tikilib->set_preference($this->getListName(), serialize($list)); 139 } 140 } 141} 142