-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.class.php
331 lines (293 loc) · 8.04 KB
/
link.class.php
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* Link class definition
*
* @author Mark Nielsen
* @version $Id$
* @package pagemenu
**/
/**
* Base link class
*/
abstract class mod_pagemenu_link {
/**
* Link type
*
* @var string
**/
public $type;
/**
* Link record object
*
* @var object
**/
public $link;
/**
* Link config options
*
* @var object
**/
public $config;
/**
* Editing flag
*
* @var boolean
**/
protected $editing = false;
/**
* YUI support flag
*
* @var boolean
**/
protected $yui = false;
/**
* Is the link active
*
* @var boolean
**/
public $active = false;
/**
* Constructor
*
* @param mixed $link Link record object or link record ID
* @param array $data Link data records
* @return void
**/
public function __construct($link = NULL, $data = NULL) {
global $CFG;
// Get the last word in the classname
$this->type = get_class($this);
$this->type = explode('_', $this->type);
$this->type = end($this->type);
if (is_int($link)) {
if (!$this->link = get_record('pagemenu_links', 'id', $link)) {
error('Failed to get link');
}
} else if (is_object($link)) {
$this->link = $link;
} else {
$this->link = new stdClass;
$this->link->id = 0;
$this->link->pagemenuid = 0;
$this->link->previd = 0;
$this->link->nextid = 0;
$this->link->type = $this->type;
}
$this->config = $this->get_config($data);
}
/**
* Link Factory
*
* Creates link objects and makes
* sure all the necessary files are
* included
*
* @param string $type Link type (AKA class name)
* @param mixed $link (Optional) Include Link ID or Link Record Object - will be passed to constructor
* @param object $config (Optional) The links data records
* @param type $name description
**/
public static function factory($type, $link = NULL, $data = NULL) {
global $CFG;
$classname = "mod_pagemenu_link_$type";
$classfile = "$CFG->dirroot/mod/pagemenu/links/$type.class.php";
// Get the class file if needed
if (!class_exists($type) and file_exists($classfile)) {
require_once($classfile);
}
// Make sure the class name is defined
if (class_exists($classname)) {
// Woot! Make it :)
return new $classname($link, $data);
}
throw new Exception("pagemenu_link factory error for type: $type");
}
/**
* Returns the display name of the link
*
* @return string
**/
public function get_name() {
return get_string($this->type, 'pagemenu');
}
/**
* Add an element to the
* edit_form to add a link
*
* @param object $mform The Moodle Form Class
* @return void
**/
abstract public function edit_form_add(&$mform);
/**
* Save form data from creating
* a new link
*
* @param object $data Form data (cleaned)
* @return mixed
**/
public function save($data) {
$names = $this->get_data_names();
$allset = true;
foreach ($names as $name) {
if (empty($data->$name)) {
$allset = false;
break;
}
}
if ($allset) {
if (!empty($data->linkid)) {
$linkid = $data->linkid;
} else {
$linkid = $this->add_new_link($data->a);
}
foreach ($names as $name) {
$this->save_data($linkid, $name, $data->$name);
}
}
}
/**
* Create a new link
*
* @param int $pagemenuid Instance ID
* @return int
**/
public function add_new_link($pagemenuid) {
$link = new stdClass;
$link->type = $this->type;
$link->previd = 0;
$link->nextid = 0;
$link->pagemenuid = $pagemenuid;
$link = pagemenu_append_link($link);
return $link->id;
}
/**
* Get the names of the link data items
* This allows for the auto processing of
* simple data items.
*
* @return array
**/
public function get_data_names() {
return array();
}
/**
* Save a piece of link data
*
* @param int $linkid ID of the link that the data belongs to
* @param string $name Name of the data
* @param mixed $value Value of the data
* @param boolean $unique Is the name/value combination unique?
* @return int
**/
public function save_data($linkid, $name, $value, $unique = false) {
$return = false;
$data = new stdClass;
$data->linkid = $linkid;
$data->name = $name;
$data->value = $value;
if ($unique) {
$fieldname = 'value';
$fieldvalue = $data->value;
} else {
$fieldname = $fieldvalue = '';
}
if ($id = get_field('pagemenu_link_data', 'id', 'linkid', $linkid, 'name', $name, $fieldname, $fieldvalue)) {
$data->id = $id;
if (update_record('pagemenu_link_data', $data)) {
$return = $id;
}
} else {
$return = insert_record('pagemenu_link_data', $data);
}
return $return;
}
/**
* Gets all of the of the data associated
* with the link. This method is designed
* to work well with passing link data recorded
* objects.
*
* @param array $data An array of pagemenu_link_data records belonging to this link
* @return object
**/
protected function get_config($data) {
$config = new stdClass;
if (!empty($this->link->id)) {
if ($data !== NULL or $data = get_records('pagemenu_link_data', 'linkid', $this->link->id)) {
foreach ($data as $datum) {
$config->{$datum->name} = $datum->value;
}
}
}
return $config;
}
/**
* Create a menu item that will be used to contruct the menu HTML
*
* @param boolean $editing Editing is turned on
* @param boolean $yui Print with YUI Menu support
* @return object
**/
abstract public function get_menuitem($editing = false, $yui = false);
/**
* Returns a blank menu item
*
* @return object
**/
protected function get_blank_menuitem() {
$menuitem = new stdClass;
$menuitem->title = '';
$menuitem->url = '';
$menuitem->class = 'link_'.get_class($this);
$menuitem->pre = '';
$menuitem->post = '';
$menuitem->active = false;
$menuitem->childtree = false;
return $menuitem;
}
/**
* The link can create its own edit actions.
* Handle them using this method.
*
* @return mixed
**/
public function handle_action() {
// Nothing
}
/**
* Mostly an internal method to see if the
* current link is active
*
* @param string $url URL to test - see if it is the current page
* @return boolean
**/
protected function is_active($url = NULL) {
if ($url === NULL) {
return false;
} else if (strpos(qualified_me(), $url) === false) {
return false;
} else {
$this->active = true;
return true;
}
}
/**
* Whether or not this link type is enabled
*
* @return boolean
**/
public function is_enabled() {
return true;
}
/**
* Restore link data - return boolean!
*
* @param array $data An array of pagemenu_link_data record objects
* @param object $restore Restore object
* @return boolean
**/
public static function restore_data($link, $restore) {
return true;
}
}
?>