OSDN Git Service

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