OSDN Git Service

fix autosaveDraft
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / PLUGIN.php
1 <?php
2         /*
3          * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4          * Copyright (C) 2002-2009 The Nucleus Group
5          *
6          * This program is free software; you can redistribute it and/or
7          * modify it under the terms of the GNU General Public License
8          * as published by the Free Software Foundation; either version 2
9          * of the License, or (at your option) any later version.
10          * (see nucleus/documentation/index.html#license for more info)
11          */
12         /**
13          * This is an (abstract) class of which all Nucleus Plugins must inherit
14          *
15          * for more information on plugins and how to write your own, see the
16          * plugins.html file that is included with the Nucleus documenation
17          *
18          * @license http://nucleuscms.org/license.txt GNU General Public License
19          * @copyright Copyright (C) 2002-2009 The Nucleus Group
20          * @version $Id$
21          * $NucleusJP: PLUGIN.php,v 1.12.2.3 2007/12/03 02:22:42 kmorimatsu Exp $
22          */
23         class NucleusPlugin {\r
24 \r
25                 // these functions _have_ to be redefined in your plugin\r
26 \r
27                 function getName() { return 'Undefined'; }\r
28                 function getAuthor()  { return 'Undefined'; }\r
29                 function getURL()  { return 'Undefined'; }\r
30                 function getVersion() { return '0.0'; }\r
31                 function getDescription() { return 'Undefined';}\r
32 \r
33                 // these function _may_ be redefined in your plugin\r
34 \r
35                 function getMinNucleusVersion() { return 150; }\r
36                 function getMinNucleusPatchLevel() { return 0; }\r
37                 function getEventList() { return array(); }\r
38                 function getTableList() { return array(); }\r
39                 function hasAdminArea() { return 0; }\r
40 \r
41                 function install() {}\r
42                 function unInstall() {}\r
43 \r
44                 function init() {}\r
45 \r
46                 function doSkinVar($skinType) {}\r
47                 function doTemplateVar(&$item) {\r
48                         $args = func_get_args();\r
49                         array_shift($args);\r
50                         array_unshift($args, 'template');\r
51                         call_user_func_array(array(&$this,'doSkinVar'),$args);\r
52                 }\r
53                 function doTemplateCommentsVar(&$item, &$comment) {\r
54                         $args = func_get_args();\r
55                         array_shift($args);\r
56                         array_shift($args);\r
57                         array_unshift($args, 'template');\r
58                         call_user_func_array(array(&$this,'doSkinVar'),$args);\r
59                 }\r
60                 function doAction($type) { return _ERROR_PLUGIN_NOSUCHACTION; }\r
61                 function doIf($key,$value) { return false; }\r
62                 function doItemVar () {}\r
63 \r
64                 /**\r
65                  * Checks if a plugin supports a certain feature.\r
66                  *\r
67                  * @returns 1 if the feature is reported, 0 if not\r
68                  * @param $feature\r
69                  *              Name of the feature. See plugin documentation for more info\r
70                  *                      'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names\r
71                  *                      'HelpPage' -> if the plugin provides a helppage\r
72                  */\r
73                 function supportsFeature($feature) {\r
74                         return 0;\r
75                 }\r
76 \r
77                 /**\r
78                  * Report a list of plugin that is required to function\r
79                  *\r
80                  * @returns an array of names of plugin, an empty array indicates no dependency\r
81                  */\r
82                 function getPluginDep() { return array(); }\r
83 \r
84                 // these helper functions should not be redefined in your plugin\r
85 \r
86                 /**\r
87                   * Creates a new option for this plugin\r
88                   *\r
89                   * @param name\r
90                   *             A string uniquely identifying your option. (max. length is 20 characters)\r
91                   * @param description\r
92                   *             A description that will show up in the nucleus admin area (max. length: 255 characters)\r
93                   * @param type\r
94                   *             Either 'text', 'yesno' or 'password'\r
95                   *             This info is used when showing 'edit plugin options' screens\r
96                   * @param value\r
97                   *             Initial value for the option (max. value length is 128 characters)\r
98                   */\r
99                 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
100                         return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);\r
101                 }\r
102                 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
103                         return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);\r
104                 }\r
105                 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
106                         return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);\r
107                 }\r
108                 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
109                         return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);\r
110                 }\r
111                 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
112                         return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);\r
113                 }\r
114 \r
115                 /**\r
116                   * Removes the option from the database\r
117                   *\r
118                   * Note: Options get erased automatically on plugin uninstall\r
119                   */\r
120                 function deleteOption($name) {\r
121                         return $this->_deleteOption('global', $name);\r
122                 }\r
123                 function deleteBlogOption($name) {\r
124                         return $this->_deleteOption('blog', $name);\r
125                 }\r
126                 function deleteMemberOption($name) {\r
127                         return $this->_deleteOption('member', $name);\r
128                 }\r
129                 function deleteCategoryOption($name) {\r
130                         return $this->_deleteOption('category', $name);\r
131                 }\r
132                 function deleteItemOption($name) {\r
133                         return $this->_deleteOption('item', $name);\r
134                 }\r
135 \r
136                 /**\r
137                   * Sets the value of an option to something new\r
138                   */\r
139                 function setOption($name, $value) {\r
140                         return $this->_setOption('global', 0, $name, $value);\r
141                 }\r
142                 function setBlogOption($blogid, $name, $value) {\r
143                         return $this->_setOption('blog', $blogid, $name, $value);\r
144                 }\r
145                 function setMemberOption($memberid, $name, $value) {\r
146                         return $this->_setOption('member', $memberid, $name, $value);\r
147                 }\r
148                 function setCategoryOption($catid, $name, $value) {\r
149                         return $this->_setOption('category', $catid, $name, $value);\r
150                 }\r
151                 function setItemOption($itemid, $name, $value) {\r
152                         return $this->_setOption('item', $itemid, $name, $value);\r
153                 }\r
154 \r
155                 /**\r
156                   * Retrieves the current value for an option\r
157                   */\r
158                 function getOption($name)\r
159                 {\r
160                         // only request the options the very first time. On subsequent requests\r
161                         // the static collection is used to save SQL queries.\r
162                         if ($this->plugin_options == 0)\r
163                         {\r
164                                 $this->plugin_options = array();\r
165                                 $query = sql_query(\r
166                                          'SELECT d.oname as name, o.ovalue as value '.\r
167                                          'FROM '.\r
168                                          sql_table('plugin_option').' o, '.\r
169                                          sql_table('plugin_option_desc').' d '.\r
170                                          'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'\r
171                                 );\r
172                                 while ($row = mysql_fetch_object($query))\r
173                                         $this->plugin_options[strtolower($row->name)] = $row->value;\r
174                   }\r
175                   if (isset($this->plugin_options[strtolower($name)]))\r
176                                 return $this->plugin_options[strtolower($name)];\r
177                   else\r
178                                 return $this->_getOption('global', 0, $name);\r
179                 }\r
180 \r
181                 function getBlogOption($blogid, $name) {\r
182                         return $this->_getOption('blog', $blogid, $name);\r
183                 }\r
184                 function getMemberOption($memberid, $name) {\r
185                         return $this->_getOption('member', $memberid, $name);\r
186                 }\r
187                 function getCategoryOption($catid, $name) {\r
188                         return $this->_getOption('category', $catid, $name);\r
189                 }\r
190                 function getItemOption($itemid, $name) {\r
191                         return $this->_getOption('item', $itemid, $name);\r
192                 }\r
193 \r
194                 /**\r
195                  * Retrieves an associative array with the option value for each\r
196                  * context id\r
197                  */\r
198                 function getAllBlogOptions($name) {\r
199                         return $this->_getAllOptions('blog', $name);\r
200                 }\r
201                 function getAllMemberOptions($name) {\r
202                         return $this->_getAllOptions('member', $name);\r
203                 }\r
204                 function getAllCategoryOptions($name) {\r
205                         return $this->_getAllOptions('category', $name);\r
206                 }\r
207                 function getAllItemOptions($name) {\r
208                         return $this->_getAllOptions('item', $name);\r
209                 }\r
210 \r
211                 /**\r
212                  * Retrieves an indexed array with the top (or bottom) of an option\r
213                  * (delegates to _getOptionTop())\r
214                  */\r
215                 function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {\r
216                         return $this->_getOptionTop('blog', $name, $amount, $sort);\r
217                 }\r
218                 function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {\r
219                         return $this->_getOptionTop('member', $name, $amount, $sort);\r
220                 }\r
221                 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {\r
222                         return $this->_getOptionTop('category', $name, $amount, $sort);\r
223                 }\r
224                 function getItemOptionTop($name, $amount = 10, $sort = 'desc') {\r
225                         return $this->_getOptionTop('item', $name, $amount, $sort);\r
226                 }\r
227 \r
228                 /**\r
229                   * Returns the plugin ID\r
230                   * \r
231                   * public                                \r
232                   */\r
233                 function getID() {\r
234                         return $this->plugid;\r
235                 }\r
236 \r
237                 /**\r
238                   * Returns the URL of the admin area for this plugin (in case there's\r
239                   * no such area, the returned information is invalid)\r
240                   * \r
241                   * public                                \r
242                   */\r
243                 function getAdminURL() {\r
244                         global $CONF;\r
245                         return $CONF['PluginURL'] . $this->getShortName() . '/';\r
246                 }\r
247 \r
248                 /**\r
249                   * Returns the directory where the admin directory is located and\r
250                   * where the plugin can maintain his extra files\r
251                   * \r
252                   * public                                \r
253                   */\r
254                 function getDirectory() {\r
255                         global $DIR_PLUGINS;\r
256                         return $DIR_PLUGINS . $this->getShortName() . '/';\r
257                 }\r
258 \r
259                 /**\r
260                   * Derives the short name for the plugin from the classname (all \r
261                   * lowercase)\r
262                   * \r
263                   * public                                \r
264                   */\r
265                 function getShortName() {\r
266                         return str_replace('np_','',strtolower(get_class($this)));\r
267                 }\r
268 \r
269                 /**\r
270                  *      Clears the option value cache which saves the option values during\r
271                  *      the plugin execution. This function is usefull if the options has \r
272                  *      changed during the plugin execution (especially in association with\r
273                  *      the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)\r
274                  *      \r
275                  *  public                               \r
276                  **/                            \r
277                 function clearOptionValueCache(){\r
278                         $this->_aOptionValues = array();\r
279                         $this->plugin_options = 0;\r
280                 }\r
281 \r
282                 // internal functions of the class starts here\r
283 \r
284                 var $_aOptionValues;    // oid_contextid => value\r
285                 var $_aOptionToInfo;    // context_name => array('oid' => ..., 'default' => ...)\r
286                 var $plugin_options;    // see getOption()\r
287                 var $plugid;                    // plugin id\r
288 \r
289 \r
290                 /**\r
291                  * Class constructor: Initializes some internal data\r
292                  */                                             \r
293                 function NucleusPlugin() {\r
294                         $this->_aOptionValues = array();        // oid_contextid => value\r
295                         $this->_aOptionToInfo = array();        // context_name => array('oid' => ..., 'default' => ...)\r
296                         $this->plugin_options = 0;\r
297                 }\r
298 \r
299                 /**\r
300                  * Retrieves an array of the top (or bottom) of an option from a plugin.\r
301                  * @author TeRanEX\r
302                  * @param  string $context the context for the option: item, blog, member,...\r
303                  * @param  string $name    the name of the option\r
304                  * @param  int    $amount  how many rows must be returned\r
305                  * @param  string $sort    desc or asc\r
306                  * @return array           array with both values and contextid's\r
307                  * @access private\r
308                  */\r
309                 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {\r
310                         if (($sort != 'desc') && ($sort != 'asc')) {\r
311                                 $sort= 'desc';\r
312                         }\r
313 \r
314                         $oid = $this->_getOID($context, $name);\r
315 \r
316                         // retrieve the data and return\r
317                         $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;\r
318                         $query = sql_query($q);\r
319 \r
320                         $o = mysql_fetch_array($query);\r
321 \r
322                         if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {\r
323                                 $orderby = 'CAST(ovalue AS SIGNED)';\r
324                         } else {\r
325                                 $orderby = 'ovalue';\r
326                         }\r
327                         $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);\r
328                         $query = sql_query($q);\r
329 \r
330                         // create the array\r
331                         $i = 0;\r
332                         $top = array();\r
333                         while($row = mysql_fetch_array($query)) {\r
334                                 $top[$i++] = $row;\r
335                         }\r
336 \r
337                         // return the array (duh!)\r
338                         return $top;\r
339                 }\r
340 \r
341                 /**\r
342                  * Creates an option in the database table plugin_option_desc\r
343                  *               \r
344                  * private\r
345                  */                                             \r
346                 function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {\r
347                         // create in plugin_option_desc\r
348                         $query = 'INSERT INTO ' . sql_table('plugin_option_desc')\r
349                                    .' (opid, oname, ocontext, odesc, otype, odef, oextra)'\r
350                                    .' VALUES ('.intval($this->plugid)\r
351                                                          .', \''.addslashes($name).'\''\r
352                                                          .', \''.addslashes($context).'\''\r
353                                                          .', \''.addslashes($desc).'\''\r
354                                                          .', \''.addslashes($type).'\''\r
355                                                          .', \''.addslashes($defValue).'\''\r
356                                                          .', \''.addslashes($typeExtras).'\')';\r
357                         sql_query($query);\r
358                         $oid = mysql_insert_id();\r
359 \r
360                         $key = $context . '_' . $name;\r
361                         $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);\r
362                         return 1;\r
363                 }\r
364 \r
365 \r
366                 /**\r
367                  * Deletes an option from the database tables\r
368                  * plugin_option and plugin_option_desc \r
369                  *\r
370                  * private               \r
371                  */                                             \r
372                 function _deleteOption($context, $name) {\r
373                         $oid = $this->_getOID($context, $name);\r
374                         if (!$oid) return 0; // no such option\r
375 \r
376                         // delete all things from plugin_option\r
377                         sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);\r
378 \r
379                         // delete entry from plugin_option_desc\r
380                         sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);\r
381 \r
382                         // clear from cache\r
383                         unset($this->_aOptionToInfo[$context . '_' . $name]);\r
384                         $this->_aOptionValues = array();\r
385                         return 1;\r
386                 }\r
387 \r
388                 /**\r
389                  * Update an option in the database table plugin_option\r
390                  *               \r
391                  * returns: 1 on success, 0 on failure\r
392                  * private\r
393                  */\r
394                 function _setOption($context, $contextid, $name, $value) {\r
395                         global $manager;\r
396 \r
397                         $oid = $this->_getOID($context, $name);\r
398                         if (!$oid) return 0;\r
399 \r
400                         // check if context id exists\r
401                         switch ($context) {\r
402                                 case 'member':\r
403                                         if (!MEMBER::existsID($contextid)) return 0;\r
404                                         break;\r
405                                 case 'blog':\r
406                                         if (!$manager->existsBlogID($contextid)) return 0;\r
407                                         break;\r
408                                 case 'category':\r
409                                         if (!$manager->existsCategory($contextid)) return 0;\r
410                                         break;\r
411                                 case 'item':\r
412                                         if (!$manager->existsItem($contextid, true, true)) return 0;\r
413                                         break;\r
414                                 case 'global':\r
415                                         if ($contextid != 0) return 0;\r
416                                         break;\r
417                         }\r
418 \r
419 \r
420                         // update plugin_option\r
421                         sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));\r
422                         sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.addslashes($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');\r
423 \r
424                         // update cache\r
425                         $this->_aOptionValues[$oid . '_' . $contextid] = $value;\r
426 \r
427                         return 1;\r
428                 }\r
429 \r
430                 /**\r
431                  * Get an option from Cache or database\r
432                  *       - if not in the option Cache read it from the database\r
433                  *   - if not in the database write default values into the database\r
434                  *                \r
435                  * private               \r
436                  */                                             \r
437                 function _getOption($context, $contextid, $name) {\r
438                         $oid = $this->_getOID($context, $name);\r
439                         if (!$oid) return '';\r
440 \r
441 \r
442                         $key = $oid . '_' . $contextid;\r
443 \r
444                         if (isset($this->_aOptionValues[$key]))\r
445                                 return $this->_aOptionValues[$key];\r
446 \r
447                         // get from DB\r
448                         $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));\r
449 \r
450                         if (!$res || (mysql_num_rows($res) == 0)) {\r
451                                 $defVal = $this->_getDefVal($context, $name);\r
452                                 $this->_aOptionValues[$key] = $defVal;\r
453 \r
454                                 // fill DB with default value\r
455                                 $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'\r
456                                            .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';\r
457                                 sql_query($query);\r
458                         }\r
459                         else {\r
460                                 $o = mysql_fetch_object($res);\r
461                                 $this->_aOptionValues[$key] = $o->ovalue;\r
462                         }\r
463 \r
464                         return $this->_aOptionValues[$key];\r
465                 }\r
466 \r
467                 /**\r
468                  * Returns assoc array with all values for a given option \r
469                  * (one option per possible context id)\r
470                  * \r
471                  * private                               \r
472                  */\r
473                 function _getAllOptions($context, $name) {\r
474                         $oid = $this->_getOID($context, $name);\r
475                         if (!$oid) return array();\r
476                         $defVal = $this->_getDefVal($context, $name);\r
477 \r
478                         $aOptions = array();\r
479                         switch ($context) {\r
480                                 case 'blog':\r
481                                         $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));\r
482                                         break;\r
483                                 case 'category':\r
484                                         $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));\r
485                                         break;\r
486                                 case 'member':\r
487                                         $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));\r
488                                         break;\r
489                                 case 'item':\r
490                                         $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));\r
491                                         break;\r
492                         }\r
493                         if ($r) {\r
494                                 while ($o = mysql_fetch_object($r))\r
495                                         $aOptions[$o->contextid] = $defVal;\r
496                         }\r
497 \r
498                         $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);\r
499                         while ($o = mysql_fetch_object($res))\r
500                                 $aOptions[$o->ocontextid] = $o->ovalue;\r
501 \r
502                         return $aOptions;\r
503                 }\r
504 \r
505                 /**\r
506                  * Gets the 'option identifier' that corresponds to a given option name.\r
507                  * When this method is called for the first time, all the OIDs for the plugin\r
508                  * are loaded into memory, to avoid re-doing the same query all over.\r
509                  */\r
510                 function _getOID($context, $name) {\r
511                         $key = $context . '_' . $name;\r
512                         $info = @$this->_aOptionToInfo[$key];\r
513                         if (is_array($info)) return $info['oid'];\r
514 \r
515                         // load all OIDs for this plugin from the database\r
516                         $this->_aOptionToInfo = array();\r
517                         $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);\r
518                         $res = sql_query($query);\r
519                         while ($o = mysql_fetch_object($res)) {\r
520                                 $k = $o->ocontext . '_' . $o->oname;\r
521                                 $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);\r
522                         }\r
523                         mysql_free_result($res);\r
524 \r
525                         return @$this->_aOptionToInfo[$key]['oid'];\r
526                 }\r
527                 function _getDefVal($context, $name) {\r
528                         $key = $context . '_' . $name;\r
529                         $info = $this->_aOptionToInfo[$key];\r
530                         if (is_array($info)) return $info['default'];\r
531                 }\r
532 \r
533 \r
534                 /**\r
535                  * Deletes all option values for a given context and contextid\r
536                  * (used when e.g. a blog, member or category is deleted)\r
537                  *\r
538                  * (static method)\r
539                  */\r
540                 function _deleteOptionValues($context, $contextid) {\r
541                         // delete all associated plugin options\r
542                         $aOIDs = array();\r
543                                 // find ids\r
544                         $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.addslashes($context).'\'';\r
545                         $res = sql_query($query);\r
546                         while ($o = mysql_fetch_object($res))\r
547                                 array_push($aOIDs, $o->oid);\r
548                         mysql_free_result($res);\r
549                                 // delete those options. go go go\r
550                         if (count($aOIDs) > 0) {\r
551                                 $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);\r
552                                 sql_query($query);\r
553                         }\r
554                 }\r
555 \r
556                 /**\r
557                  * splits the option's typeextra field (at ;'s) to split the meta collection\r
558                  * @param string $typeExtra the value of the typeExtra field of an option\r
559                  * @return array array of the meta-key/value-pairs\r
560                  * @author TeRanEX\r
561                  * @static\r
562                  */\r
563                 function getOptionMeta($typeExtra) {\r
564                         $tmpMeta = explode(';', $typeExtra);\r
565                         $meta = array();\r
566                         for ($i = 0; $i < count($tmpMeta); $i++) {\r
567                                 if (($i == 0) && (!strstr($tmpMeta[0], '='))) {\r
568                                         // we have the select-list\r
569                                         $meta['select'] = $tmpMeta[0];\r
570                                 } else {\r
571                                         $tmp = explode('=', $tmpMeta[$i]);\r
572                                         $meta[$tmp[0]] = $tmp[1];\r
573                                 }\r
574                         }\r
575                         return $meta;\r
576                 }\r
577 \r
578                 /**\r
579                  * filters the selectlists out of the meta collection\r
580                  * @param string $typeExtra the value of the typeExtra field of an option\r
581                  * @return string the selectlist\r
582                  * @author TeRanEX\r
583                  */\r
584                 function getOptionSelectValues($typeExtra) {\r
585                         $meta = NucleusPlugin::getOptionMeta($typeExtra);\r
586                         //the select list must always be the first part\r
587                         return $meta['select'];\r
588                 }\r
589 \r
590                 /**\r
591                  * checks if the eventlist in the database is up-to-date\r
592                  * @return bool if it is up-to-date it return true, else false\r
593                  * @author TeRanEX\r
594                  */\r
595                 function subscribtionListIsUptodate() {\r
596                         $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());\r
597                         $ev = array();\r
598                         while($a = mysql_fetch_array($res)) {\r
599                                 array_push($ev, $a['event']);\r
600                         }\r
601                         if (count($ev) != count($this->getEventList())) {\r
602                                 return false;\r
603                         }\r
604                         $d = array_diff($ev, $this->getEventList());\r
605                         if (count($d) > 0) {\r
606                                 // there are differences so the db is not up-to-date\r
607                                 return false;\r
608                         }\r
609                         return true;\r
610                 }\r
611 \r
612                 /**\r
613                  * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))\r
614                  *        (taken from request using requestVar())\r
615                  * @param $newContextid: integer (accepts a contextid when it is for a new\r
616                  *        contextid there was no id available at the moment of writing the\r
617                  *        formcontrols into the page (by ex: itemOptions for new item)\r
618                  * @static\r
619                  */\r
620                 function _applyPluginOptions(&$aOptions, $newContextid = 0) {\r
621                         global $manager;\r
622                         if (!is_array($aOptions)) return;\r
623 \r
624                         foreach ($aOptions as $oid => $values) {\r
625 \r
626                                 // get option type info\r
627                                 $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);\r
628                                 $res = sql_query($query);\r
629                                 if ($o = mysql_fetch_object($res))\r
630                                 {\r
631                                         foreach ($values as $key => $value) {\r
632                                                 // avoid overriding the key used by foreach statement\r
633                                                 $contextid=$key;\r
634 \r
635                                                 // retreive any metadata\r
636                                                 $meta = NucleusPlugin::getOptionMeta($o->oextra);\r
637 \r
638                                                 // if the option is readonly or hidden it may not be saved\r
639                                                 if ((@$meta['access'] != 'readonly') && (@$meta['access'] != 'hidden')) {\r
640 \r
641                                                         $value = undoMagic($value);     // value comes from request\r
642 \r
643                                                         switch($o->otype) {\r
644                                                                 case 'yesno':\r
645                                                                         if (($value != 'yes') && ($value != 'no')) $value = 'no';\r
646                                                                         break;\r
647                                                                 default:\r
648                                                                         break;\r
649                                                         }\r
650 \r
651                                                         // check the validity of numerical options\r
652                                                         if ((@$meta['datatype'] == 'numerical') && (!is_numeric($value))) {\r
653                                                                 //the option must be numeric, but the it isn't\r
654                                                                 //use the default for this option\r
655                                                                 $value = $o->odef;\r
656                                                         }\r
657 \r
658                                                         // decide wether we are using the contextid of newContextid\r
659                                                         if ($newContextid != 0) {\r
660                                                                 $contextid = $newContextid;\r
661                                                         }\r
662 \r
663                                                         //trigger event PrePluginOptionsUpdate to give the plugin the\r
664                                                         //possibility to change/validate the new value for the option\r
665                                                         $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));\r
666 \r
667                                                         // delete the old value for the option\r
668                                                         sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));\r
669                                                         sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");\r
670                                                 }\r
671                                         }\r
672                                 }\r
673                                 // clear option value cache if the plugin object is already loaded\r
674                                 if (is_object($o)) {\r
675                                         $plugin=& $manager->pidLoaded($o->opid);\r
676                                         if ($plugin) $plugin->clearOptionValueCache();\r
677                                 }\r
678                         }\r
679                 }\r
680         }\r
681 ?>