1 <?php
2
3 namespace Icybee\ManageBlock;
4
5 6 7 8 9
10 class Options
11 {
12 public $start = 1;
13 public $limit = 10;
14 public $order_by = null;
15 public $order_direction = null;
16 public $search = null;
17 public $filters = array();
18
19 private $name;
20
21 public function __construct($name)
22 {
23 $this->name = $name;
24 }
25
26 public function __set($property, $value)
27 {
28 if ($property == 'order')
29 {
30 throw new \InvalidArgumentException("The <q>order</q> property is deprecated. Please use <q>order_by</q> and <q>order_direction</q>.");
31 }
32
33 $this->$property = $value;
34 }
35
36 37 38
39 public function reset()
40 {
41 $this->start = 1;
42 $this->limit = 10;
43 $this->order_by = null;
44 $this->order_direction = null;
45 $this->search = null;
46 $this->filters = array();
47
48 return $this;
49 }
50
51 52 53 54 55
56 public function to_array()
57 {
58 $array = get_object_vars($this);
59
60 unset($array['name']);
61
62 return $array;
63 }
64
65 66 67 68 69 70 71
72 public function retrieve()
73 {
74 global $core;
75
76 $this->reset();
77
78 $name = $this->name;
79 $serialized = $core->user->metas["block.manager.{$this->name}:{$core->site_id}"];
80
81 if ($serialized)
82 {
83 $options = json_decode($serialized, true);
84
85 foreach ($options as $option => $value)
86 {
87 $this->$option = $value;
88 }
89 }
90
91 return $this;
92 }
93
94 95 96 97 98 99
100 public function store()
101 {
102 global $core;
103
104 $serialized = json_encode($this->to_array());
105
106 $core->user->metas["block.manager.{$this->name}:{$core->site_id}"] = $serialized;
107
108 return $this;
109 }
110
111 112 113 114 115 116 117
118 public function update(array $modifiers)
119 {
120 if (isset($modifiers['limit']))
121 {
122 $this->limit = max(filter_var($modifiers['limit'], FILTER_VALIDATE_INT), 10);
123 }
124
125 if (isset($modifiers['start']))
126 {
127 $start = $modifiers['start'];
128
129 if ($start === 'next')
130 {
131 $this->start += $this->limit;
132 }
133 else if ($start === 'previous')
134 {
135 $this->start -= $this->limit;
136 }
137 else
138 {
139 $this->start = max(filter_var($start, FILTER_VALIDATE_INT), 1);
140 }
141 }
142
143 if (isset($modifiers['q']))
144 {
145 $this->search = $modifiers['q'];
146 $this->start = 1;
147 }
148
149 if (isset($modifiers['order']))
150 {
151 list($order_by, $order_direction) = explode(':', $modifiers['order']) + array(1 => null);
152
153 $order_direction = (strtolower($order_direction) == 'desc' ? -1 : 1);
154
155 if ($order_by != $this->order_by || $order_direction != $this->order_direction)
156 {
157 $this->start = 1;
158 }
159
160 $this->order_by = $order_by;
161 $this->order_direction = $order_direction;
162 }
163
164 if (isset($modifiers['filters']))
165 {
166 $filters = $modifiers['filters'];
167
168 if ($this->filters != $filters)
169 {
170 $this->filters = $filters;
171 $this->start = 1;
172 }
173 }
174
175 return $this;
176 }
177
178 179 180 181 182 183 184 185
186 public function is_filtering($column_id=null)
187 {
188 if ($column_id === null)
189 {
190 return !empty($this->filters);
191 }
192
193 return isset($this->filters[$column_id]);
194 }
195 }