OSDN Git Service

FIX: NucleusPluginクラスが発生する警告への対策
[nucleus-jp/nucleus-next.git] / 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: PLUGIN.php 1630 2012-01-28 12:16:14Z sakamocchi $
21  */
22 class NucleusPlugin
23 {
24         // these functions _have_ to be redefined in your plugin
25         function getName()
26         {
27                 return 'Undefined';
28         }
29         
30         function getAuthor()
31         {
32                 return 'Undefined';
33         }
34         
35         function getURL()
36         {
37                 return 'Undefined';
38         }
39         
40         function getVersion()
41         {
42                 return '0.0';
43         }
44         
45         function getDescription()
46         {
47                 return 'Undefined';
48         }
49         
50         // these function _may_ be redefined in your plugin
51         
52         function getMinNucleusVersion()
53         {
54                 return 150;
55         }
56         
57         function getMinNucleusPatchLevel()
58         {
59                 return 0;
60         }
61         
62         function getEventList()
63         {
64                 return array();
65         }
66         
67         function getTableList()
68         {
69                 return array();
70         }
71         
72         function hasAdminArea()
73         {
74                 return 0;
75         }
76         
77         function install()
78         {
79         }
80         
81         function unInstall()
82         {
83         }
84         
85         function init()
86         {
87         }
88         
89         function doSkinVar($skinType)
90         {
91         }
92         
93         function doTemplateVar(&$item)
94         {
95                 $args = func_get_args();
96                 array_shift($args);
97                 array_unshift($args, 'template');
98                 call_user_func_array(array(&$this,'doSkinVar'),$args);
99         }
100         
101         function doTemplateCommentsVar(&$item, &$comment)
102         {
103                 $args = func_get_args();
104                 array_shift($args);
105                 array_shift($args);
106                 array_unshift($args, 'template');
107                 call_user_func_array(array(&$this,'doSkinVar'),$args);
108         }
109         
110         function doAction($type)
111         {
112                 return _ERROR_PLUGIN_NOSUCHACTION;
113         }
114         
115         function doIf($key,$value)
116         {
117                 return false;
118         }
119         
120         function doItemVar (&$item)
121         {
122         }
123         
124         /**
125          * Checks if a plugin supports a certain feature.
126          *
127          * @returns 1 if the feature is reported, 0 if not
128          * @param $feature
129          *              Name of the feature. See plugin documentation for more info
130          *                      'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
131          *                      'HelpPage' -> if the plugin provides a helppage
132          *                      'SqlApi' -> if the plugin uses the complete sql_* api (must also require nucleuscms 3.5)
133          */
134         function supportsFeature($feature)
135         {
136                 return 0;
137         }
138         
139         /**
140          * Report a list of plugin that is required to function
141          *
142          * @returns an array of names of plugin, an empty array indicates no dependency
143          */
144         function getPluginDep()
145         {
146                 return array();
147         }
148         
149         // these helper functions should not be redefined in your plugin
150         
151         /**
152          * Creates a new option for this plugin
153          *
154          * @param name
155          *              A string uniquely identifying your option. (max. length is 20 characters)
156          * @param description
157          *              A description that will show up in the nucleus admin area (max. length: 255 characters)
158          * @param type
159          *              Either 'text', 'yesno' or 'password'
160          *              This info is used when showing 'edit plugin options' screens
161          * @param value
162          *              Initial value for the option (max. value length is 128 characters)
163          */
164         function createOption($name, $desc, $type, $defValue = '', $typeExtras = '')
165         {
166                 return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
167         }
168         
169         function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '')
170         {
171                 return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
172         }
173         
174         function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '')
175         {
176                 return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
177         }
178         
179         function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '')
180         {
181                 return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
182         }
183         
184         function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '')
185         {
186                 return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
187         }
188         
189         /**
190          * Removes the option from the database
191          *
192          * Note: Options get erased automatically on plugin uninstall
193          */
194         function deleteOption($name)
195         {
196                 return $this->_deleteOption('global', $name);
197         }
198         
199         function deleteBlogOption($name)
200         {
201                 return $this->_deleteOption('blog', $name);
202         }
203         
204         function deleteMemberOption($name)
205         {
206                 return $this->_deleteOption('member', $name);
207         }
208         
209         function deleteCategoryOption($name)
210         {
211                 return $this->_deleteOption('category', $name);
212         }
213         
214         function deleteItemOption($name)
215         {
216                 return $this->_deleteOption('item', $name);
217         }
218         
219         /**
220          * Sets the value of an option to something new
221          */
222         function setOption($name, $value)
223         {
224                 return $this->_setOption('global', 0, $name, $value);
225         }
226         
227         function setBlogOption($blogid, $name, $value)
228         {
229                 return $this->_setOption('blog', $blogid, $name, $value);
230         }
231         
232         function setMemberOption($memberid, $name, $value)
233         {
234                 return $this->_setOption('member', $memberid, $name, $value);
235         }
236         
237         function setCategoryOption($catid, $name, $value)
238         {
239                 return $this->_setOption('category', $catid, $name, $value);
240         }
241         
242         function setItemOption($itemid, $name, $value) {
243                 return $this->_setOption('item', $itemid, $name, $value);
244         }
245         
246         /**
247          * Retrieves the current value for an option
248          */
249         function getOption($name)
250         {
251                 // only request the options the very first time. On subsequent requests
252                 // the static collection is used to save SQL queries.
253                 if ( $this->plugin_options == 0 )
254                 {
255                         $this->plugin_options = array();
256                         $query = sql_query(
257                                  'SELECT d.oname as name, o.ovalue as value '.
258                                  'FROM '.
259                                  sql_table('plugin_option').' o, '.
260                                  sql_table('plugin_option_desc').' d '.
261                                  'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
262                         );
263                         while ( $row = sql_fetch_object($query) )
264                         {
265                                 $this->plugin_options[strtolower($row->name)] = $row->value;
266                         }
267                 }
268                 if ( isset($this->plugin_options[strtolower($name)]) )
269                 {
270                         return $this->plugin_options[strtolower($name)];
271                 }
272                 else
273                 {
274                         return $this->_getOption('global', 0, $name);
275                 }
276         }
277         
278         function getBlogOption($blogid, $name)
279         {
280                 return $this->_getOption('blog', $blogid, $name);
281         }
282         
283         function getMemberOption($memberid, $name)
284         {
285                 return $this->_getOption('member', $memberid, $name);
286         }
287         
288         function getCategoryOption($catid, $name)
289         {
290                 return $this->_getOption('category', $catid, $name);
291         }
292         
293         function getItemOption($itemid, $name)
294         {
295                 return $this->_getOption('item', $itemid, $name);
296         }
297         
298         /**
299          * Retrieves an associative array with the option value for each
300          * context id
301          */
302         function getAllBlogOptions($name)
303         {
304                 return $this->_getAllOptions('blog', $name);
305         }
306         
307         function getAllMemberOptions($name)
308         {
309                 return $this->_getAllOptions('member', $name);
310         }
311         
312         function getAllCategoryOptions($name)
313         {
314                 return $this->_getAllOptions('category', $name);
315         }
316         
317         function getAllItemOptions($name)
318         {
319                 return $this->_getAllOptions('item', $name);
320         }
321         
322         /**
323          * Retrieves an indexed array with the top (or bottom) of an option
324          * (delegates to _getOptionTop())
325          */
326         function getBlogOptionTop($name, $amount = 10, $sort = 'desc')
327         {
328                 return $this->_getOptionTop('blog', $name, $amount, $sort);
329         }
330         
331         function getMemberOptionTop($name, $amount = 10, $sort = 'desc')
332         {
333                 return $this->_getOptionTop('member', $name, $amount, $sort);
334         }
335         
336         function getCategoryOptionTop($name, $amount = 10, $sort = 'desc')
337         {
338                 return $this->_getOptionTop('category', $name, $amount, $sort);
339         }
340         
341         function getItemOptionTop($name, $amount = 10, $sort = 'desc')
342         {
343                 return $this->_getOptionTop('item', $name, $amount, $sort);
344         }
345         
346         /**
347          * Returns the plugin ID
348          *
349          * public
350          */
351         function getID()
352         {
353                 return $this->plugid;
354         }
355         
356         /**
357          * Returns the URL of the admin area for this plugin (in case there's
358          * no such area, the returned information is invalid)
359          *
360          * public
361          */
362         function getAdminURL()
363         {
364                 global $CONF;
365                 return $CONF['PluginURL'] . $this->getShortName() . '/';
366         }
367         
368         /**
369          * Returns the directory where the admin directory is located and
370          * where the plugin can maintain his extra files
371          *
372          * public
373          */
374         function getDirectory()
375         {
376                 global $DIR_PLUGINS;
377                 return $DIR_PLUGINS . $this->getShortName() . '/';
378         }
379         
380         /**
381          * Derives the short name for the plugin from the classname (all
382          * lowercase)
383          *
384          * public
385          */
386         function getShortName()
387         {
388                 return str_replace('np_','',strtolower(get_class($this)));
389         }
390         
391         /**
392          *      Clears the option value cache which saves the option values during
393          *      the plugin execution. This function is usefull if the options has
394          *      changed during the plugin execution (especially in association with
395          *      the PrePluginOptionsUpdate and the PostPluginOptionsUpdate events)
396          *      
397          *  public
398          **/
399         function clearOptionValueCache()
400         {
401                 $this->_aOptionValues = array();
402                 $this->plugin_options = 0;
403         }
404         
405         // internal functions of the class starts here
406         
407         var $_aOptionValues;    // oid_contextid => value
408         var $_aOptionToInfo;    // context_name => array('oid' => ..., 'default' => ...)
409         var $plugin_options;    // see getOption()
410         var $plugid;                    // plugin id
411         
412         /**
413          * Class constructor: Initializes some internal data
414          */
415         function NucleusPlugin()
416         {
417                 $this->_aOptionValues = array();        // oid_contextid => value
418                 $this->_aOptionToInfo = array();        // context_name => array('oid' => ..., 'default' => ...)
419                 $this->plugin_options = 0;
420         }
421         
422         /**
423          * Retrieves an array of the top (or bottom) of an option from a plugin.
424          * @author TeRanEX
425          * @param  string $context the context for the option: item, blog, member,...
426          * @param  string $name    the name of the option
427          * @param  int    $amount  how many rows must be returned
428          * @param  string $sort    desc or asc
429          * @return array           array with both values and contextid's
430          * @access private
431          */
432         function _getOptionTop($context, $name, $amount = 10, $sort = 'desc')
433         {
434                 if ( ($sort != 'desc') && ($sort != 'asc') )
435                 {
436                         $sort= 'desc';
437                 }
438                 
439                 $oid = $this->_getOID($context, $name);
440                 
441                 // retrieve the data and return
442                 $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
443                 $query = sql_query($q);
444                 
445                 $o = sql_fetch_array($query);
446                 
447                 if ( ($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' ) )
448                 {
449                         $orderby = 'CAST(ovalue AS SIGNED)';
450                 }
451                 else
452                 {
453                         $orderby = 'ovalue';
454                 }
455                 $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.intval($amount);
456                 $query = sql_query($q);
457                 
458                 // create the array
459                 $i = 0;
460                 $top = array();
461                 while( $row = sql_fetch_array($query) )
462                 {
463                         $top[$i++] = $row;
464                 }
465                 
466                 // return the array (duh!)
467                 return $top;
468         }
469         
470         /**
471          * Creates an option in the database table plugin_option_desc
472          *      
473          * private
474          */
475         function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '')
476         {
477                 // create in plugin_option_desc
478                 $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
479                                 .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
480                                 .' VALUES ('.intval($this->plugid)
481                                         .', \''.sql_real_escape_string($name).'\''
482                                         .', \''.sql_real_escape_string($context).'\''
483                                         .', \''.sql_real_escape_string($desc).'\''
484                                         .', \''.sql_real_escape_string($type).'\''
485                                         .', \''.sql_real_escape_string($defValue).'\''
486                                         .', \''.sql_real_escape_string($typeExtras).'\')';
487                 sql_query($query);
488                 $oid = sql_insert_id();
489                 
490                 $key = $context . '_' . $name;
491                 $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
492                 return 1;
493         }
494         
495         /**
496          * Deletes an option from the database tables
497          * plugin_option and plugin_option_desc
498          *
499          * private
500          */
501         function _deleteOption($context, $name)
502         {
503                 $oid = $this->_getOID($context, $name);
504                 if ( !$oid )
505                 {
506                         return 0; // no such option
507                 }
508                 
509                 // delete all things from plugin_option
510                 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
511                 
512                 // delete entry from plugin_option_desc
513                 sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
514                 
515                 // clear from cache
516                 unset($this->_aOptionToInfo[$context . '_' . $name]);
517                 $this->_aOptionValues = array();
518                 return 1;
519         }
520         
521         /**
522          * Update an option in the database table plugin_option
523          *              
524          * returns: 1 on success, 0 on failure
525          * private
526          */
527         function _setOption($context, $contextid, $name, $value)
528         {
529                 global $manager;
530                 
531                 $oid = $this->_getOID($context, $name);
532                 if ( !$oid )
533                 {
534                         return 0;
535                 }
536                 
537                 // check if context id exists
538                 switch ( $context )
539                 {
540                         case 'member':
541                                 if ( !MEMBER::existsID($contextid) )
542                                 {
543                                         return 0;
544                                 }
545                                 break;
546                         case 'blog':
547                                 if ( !$manager->existsBlogID($contextid) )
548                                 {
549                                         return 0;
550                                 }
551                                 break;
552                         case 'category':
553                                 if ( !$manager->existsCategory($contextid) )
554                                 {
555                                         return 0;
556                                 }
557                                 break;
558                         case 'item':
559                                 if ( !$manager->existsItem($contextid, true, true) )
560                                 {
561                                         return 0;
562                                 }
563                                 break;
564                         case 'global':
565                                 if ( $contextid != 0 )
566                                 {
567                                         return 0;
568                                 }
569                                 break;
570                 }
571                 
572                 // update plugin_option
573                 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
574                 sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.sql_real_escape_string($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
575                 
576                 // update cache
577                 $this->_aOptionValues[$oid . '_' . $contextid] = $value;
578                 if ( $context == 'global' )
579                 {
580                         $this->plugin_options[strtolower($name)] = $value;
581                 }
582
583                 return 1;
584         }
585         
586         /**
587          * Get an option from Cache or database
588          *       - if not in the option Cache read it from the database
589          *   - if not in the database write default values into the database
590          *              
591          * private              
592          */                                             
593         function _getOption($context, $contextid, $name)
594         {
595                 $oid = $this->_getOID($context, $name);
596                 if ( !$oid )
597                 {
598                         return '';
599                 }
600                 
601                 $key = $oid . '_' . $contextid;
602                 
603                 if ( isset($this->_aOptionValues[$key]) )
604                 {
605                         return $this->_aOptionValues[$key];
606                 }
607                 
608                 // get from DB
609                 $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
610                 
611                 if ( !$res || (sql_num_rows($res) == 0) )
612                 {
613                         $defVal = $this->_getDefVal($context, $name);
614                         $this->_aOptionValues[$key] = $defVal;
615                         
616                         // fill DB with default value
617                         $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
618                                         .' VALUES ('.intval($oid).', '.intval($contextid).', \''.sql_real_escape_string($defVal).'\')';
619                         sql_query($query);
620                 }
621                 else
622                 {
623                         $o = sql_fetch_object($res);
624                         $this->_aOptionValues[$key] = $o->ovalue;
625                 }
626                 
627                 return $this->_aOptionValues[$key];
628         }
629         
630         /**
631          * Returns assoc array with all values for a given option
632          * (one option per possible context id)
633          *
634          * private                              
635          */
636         function _getAllOptions($context, $name)
637         {
638                 $oid = $this->_getOID($context, $name);
639                 if ( !$oid )
640                 {
641                         return array();
642                 }
643                 $defVal = $this->_getDefVal($context, $name);
644                 
645                 $aOptions = array();
646                 switch ( $context )
647                 {
648                         case 'blog':
649                                 $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
650                                 break;
651                         case 'category':
652                                 $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
653                                 break;
654                         case 'member':
655                                 $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
656                                 break;
657                         case 'item':
658                                 $r = sql_query('SELECT inumber as contextid FROM ' . sql_table('item'));
659                                 break;
660                 }
661                 if ( $r )
662                 {
663                         while ( $o = sql_fetch_object($r) )
664                         {
665                                 $aOptions[$o->contextid] = $defVal;
666                         }
667                 }
668                 
669                 $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
670                 while ( $o = sql_fetch_object($res) )
671                 {
672                         $aOptions[$o->ocontextid] = $o->ovalue;
673                 }
674
675                 return $aOptions;
676         }
677         
678         /**
679          * NucleusPlugin::_getOID
680          * 
681          * Gets the 'option identifier' that corresponds to a given option name.
682          * When this method is called for the first time, all the OIDs for the plugin
683          * are loaded into memory, to avoid re-doing the same query all over.
684          * 
685          * @param       string  $context        option context
686          * @param       string  $name           plugin name
687          * @return              integer option id
688          */
689         function _getOID($context, $name)
690         {
691                 $key = $context . '_' . $name;
692                 
693                 if ( array_key_exists($key, $this->_aOptionToInfo)
694                  && array_key_exists('oid', $this->_aOptionToInfo[$key]) )
695                 {
696                         return $this->_aOptionToInfo[$key]['oid'];
697                 }
698                 
699                 // load all OIDs for this plugin from the database
700                 $this->_aOptionToInfo = array();
701                 $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
702                 $res = sql_query($query);
703                 while ( $o = sql_fetch_object($res) )
704                 {
705                         $k = $o->ocontext . '_' . $o->oname;
706                         $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
707                 }
708                 sql_free_result($res);
709                 
710                 return $this->_aOptionToInfo[$key]['oid'];
711         }
712         function _getDefVal($context, $name)
713         {
714                 $key = $context . '_' . $name;
715                 
716                 if ( array_key_exists($key, $this->_aOptionToInfo)
717                  && array_key_exists('default', $this->_aOptionToInfo[$key]) )
718                 {
719                         return $this->_aOptionToInfo[$key]['default'];
720                 }
721                 return;
722         }
723         
724         /**
725          * Deletes all option values for a given context and contextid
726          * (used when e.g. a blog, member or category is deleted)
727          *
728          * (static method)
729          */
730         function _deleteOptionValues($context, $contextid)
731         {
732                 // delete all associated plugin options
733                 $aOIDs = array();
734                 // find ids
735                 $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.sql_real_escape_string($context).'\'';
736                 $res = sql_query($query);
737                 while ( $o = sql_fetch_object($res) )
738                 {
739                         array_push($aOIDs, $o->oid);
740                 }
741                 sql_free_result($res);
742                 // delete those options. go go go
743                 if ( count($aOIDs) > 0 )
744                 {
745                         $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
746                         sql_query($query);
747                 }
748         }
749         
750         /**
751          * NucleusPlugin::getOptionMeta()
752          * splits the option's typeextra field (at ;'s) to split the meta collection
753          * 
754          * @static
755          * @param string $typeExtra the value of the typeExtra field of an option
756          * @return array array of the meta-key/value-pairs
757          */
758         function getOptionMeta($typeExtra)
759         {
760                 $meta = array();
761                 
762                 /* 1. if $typeExtra includes delimiter ';', split it to tokens */
763                 $tokens = i18n::explode(';', $typeExtra);
764                 
765                 /*
766                  * 2. if each of tokens includes "=", it consists of key => value
767                  *    else it's 'select' option
768                  */
769                 foreach ( $tokens as $token )
770                 {
771                         $matches = array();
772                         if ( preg_match("#^([^=]+)?=([^=]+)?$#", $token, $matches) )
773                         {
774                                 $meta[$matches[1]] = $matches[2];
775                         }
776                         else
777                         {
778                                 $meta['select'] = $token;
779                         }
780                 }
781                 return $meta;
782         }
783         
784         /**
785          * NucleusPlugin::getOptionSelectValues()
786          * filters the selectlists out of the meta collection
787          * 
788          * @static
789          * @param string $typeExtra the value of the typeExtra field of an option
790          * @return string the selectlist
791          */
792         function getOptionSelectValues($typeExtra)
793         {
794                 $meta = NucleusPlugin::getOptionMeta($typeExtra);
795                 
796                 if ( array_key_exists('select', $meta) )
797                 {
798                         return $meta['select'];
799                 }
800                 return;
801         }
802         
803         /**
804          * checks if the eventlist in the database is up-to-date
805          * @return bool if it is up-to-date it return true, else false
806          * @author TeRanEX
807          */
808         function subscribtionListIsUptodate()
809         {
810                 $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
811                 $ev = array();
812                 while( $a = sql_fetch_array($res) )
813                 {
814                         array_push($ev, $a['event']);
815                 }
816                 if ( count($ev) != count($this->getEventList()) )
817                 {
818                         return false;
819                 }
820                 $d = array_diff($ev, $this->getEventList());
821                 if ( count($d) > 0 )
822                 {
823                         // there are differences so the db is not up-to-date
824                         return false;
825                 }
826                 return true;
827         }
828         
829         /**
830          * NucleusPlugin::_applyPluginOptions()
831          * Update its entry in database table
832          * 
833          * @static
834          * @param       $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
835          *                       (taken from request using requestVar())
836          * @param       $newContextid: integer (accepts a contextid when it is for a new
837          *                       contextid there was no id available at the moment of writing the
838          *                        formcontrols into the page (by ex: itemOptions for new item)
839          * @return void
840          */
841         function _applyPluginOptions(&$aOptions, $newContextid = 0)
842         {
843                 global $manager;
844                 if ( !is_array($aOptions) )
845                 {
846                         return;
847                 }
848                 
849                 foreach ( $aOptions as $oid => $values )
850                 {
851                         // get option type info
852                         $query = "SELECT opid, oname, ocontext, otype, oextra, odef FROM %s WHERE oid=%d";
853                         $query = sprintf($query, sql_table('plugin_option_desc'), (integer) $oid);
854                         $result = sql_query($query);
855                         if ( $info = sql_fetch_object($result) )
856                         {
857                                 foreach ( $values as $key => $value )
858                                 {
859                                         // avoid overriding the key used by foreach statement
860                                         $contextid=$key;
861                                         
862                                         // retreive any metadata
863                                         $meta = NucleusPlugin::getOptionMeta($info->oextra);
864                                         
865                                         // if the option is readonly or hidden it may not be saved
866                                         if ( array_key_exists('access', $meta)
867                                          && in_array($meta['access'], array('readonly', 'hidden')) )
868                                         {
869                                                 return;
870                                         }
871                                         
872                                         // value comes from request
873                                         $value = undoMagic($value);
874                                         
875                                         /* validation the value according to its type */
876                                         switch ( $info->otype )
877                                         {
878                                                 case 'yesno':
879                                                         if ( ($value != 'yes') && ($value != 'no') )
880                                                         {
881                                                                 $value = 'no';
882                                                         }
883                                                         break;
884                                                 case 'text':
885                                                 case 'select':
886                                                         if ( array_key_exists('datatype', $meta)
887                                                          && ($meta['datatype'] == 'numerical') && ($value != (integer) $value) )
888                                                         {
889                                                                 $value = (integer) $info->odef;
890                                                         }
891                                                         break;
892                                                 case 'password':
893                                                 case 'textarea':
894                                                 default:
895                                                         break;
896                                         }
897                                         
898                                         // decide wether we are using the contextid of newContextid
899                                         if ( $newContextid != 0 )
900                                         {
901                                                 $contextid = $newContextid;
902                                         }
903                                         
904                                         /*
905                                          * trigger event PrePluginOptionsUpdate to give the plugin the
906                                          * possibility to change/validate the new value for the option
907                                          */
908                                         $data = array(
909                                                 'context'               => $info->ocontext,
910                                                 'plugid'                => $info->opid,
911                                                 'optionname'    => $info->oname,
912                                                 'contextid'     => $contextid,
913                                                 'value'         => &$value);
914                                         $manager->notify('PrePluginOptionsUpdate', $data);
915                                         
916                                         // delete and insert its fields of table in database
917                                         $query = "DELETE FROM %s WHERE oid=%d AND ocontextid=%d;";
918                                         $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid);
919                                         sql_query($query);
920                                         $query = "INSERT INTO %s (oid, ocontextid, ovalue) VALUES (%d, %d, '%s');";
921                                         $query = sprintf($query, sql_table('plugin_option'), (integer) $oid, (integer) $contextid, sql_real_escape_string($value));
922                                         sql_query($query);
923                                 }
924                         }
925                         // clear option value cache if the plugin object is already loaded
926                         if ( is_object($info) )
927                         {
928                                 $plugin=& $manager->pidLoaded($info->opid);
929                                 if ( $plugin )
930                                 {
931                                         $plugin->clearOptionValueCache();
932                                 }
933                         }
934                 }
935                 return;
936         }
937 }