OSDN Git Service

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