<?php /**\r
* Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
- * Copyright (C) 2002-2004 The Nucleus Group\r
+ * Copyright (C) 2002-2005 The Nucleus Group\r
*\r
* This program is free software; you can redistribute it and/or\r
* modify it under the terms of the GNU General Public License\r
*\r
* for more information on plugins and how to write your own, see the\r
* plugins.html file that is included with the Nucleus documenation\r
- */\r
+ *\r
+ * $Id: PLUGIN.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $\r
+ * $NucleusJP$\r
+ */\r
class NucleusPlugin {\r
\r
// these functions _have_ to be redefined in your plugin\r
* @param $feature\r
* Name of the feature. See plugin documentation for more info\r
* 'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names\r
+ * 'HelpPage' -> if the plugin provides a helppage\r
*/\r
function supportsFeature($feature) {\r
return 0;\r
}\r
\r
+ /**\r
+ * Report a list of plugin that is required to function\r
+ * \r
+ * @returns an array of names of plugin, an empty array indicates no dependency\r
+ */\r
+ function getPluginDep() { return array(); }\r
+\r
// these helper functions should not be redefined in your plugin\r
\r
/**\r
function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);\r
}\r
+ function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {\r
+ return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);\r
+ }\r
\r
/**\r
* Removes the option from the database\r
function deleteCategoryOption($name) {\r
return $this->_deleteOption('category', $name);\r
}\r
+ function deleteItemOption($name) {\r
+ return $this->_deleteOption('item', $name);\r
+ }\r
\r
/**\r
* Sets the value of an option to something new\r
function setCategoryOption($catid, $name, $value) {\r
return $this->_setOption('category', $catid, $name, $value);\r
}\r
+ function setItemOption($itemid, $name, $value) {\r
+ return $this->_setOption('item', $itemid, $name, $value);\r
+ }\r
\r
/**\r
* Retrieves the current value for an option\r
function getCategoryOption($catid, $name) {\r
return $this->_getOption('category', $catid, $name);\r
}\r
+ function getItemOption($itemid, $name) {\r
+ return $this->_getOption('item', $itemid, $name);\r
+ }\r
\r
/**\r
* Retrieves an associative array with the option value for each\r
function getAllCategoryOptions($name) {\r
return $this->_getAllOptions('category', $name);\r
}\r
+ function getAllItemOptions($name) {\r
+ return $this->_getAllOptions('item', $name);\r
+ }\r
+ \r
+ /**\r
+ * Retrieves an indexed array with the top (or bottom) of an option\r
+ * (delegates to _getOptionTop())\r
+ */\r
+ function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {\r
+ return $this->_getOptionTop('blog', $name, $amount, $sort);\r
+ }\r
+ function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {\r
+ return $this->_getOptionTop('member', $name, $amount, $sort);\r
+ }\r
+ function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {\r
+ return $this->_getOptionTop('category', $name, $amount, $sort);\r
+ }\r
+ function getItemOptionTop($name, $amount = 10, $sort = 'desc') {\r
+ return $this->_getOptionTop('item', $name, $amount, $sort);\r
+ }\r
+ \r
+ /**\r
+ * Retrieves an array of the top (or bottom) of an option from a plugin.\r
+ * @author TeRanEX\r
+ * @param string $context the context for the option: item, blog, member,...\r
+ * @param string $name the name of the option\r
+ * @param int $amount how many rows must be returned\r
+ * @param string $sort desc or asc\r
+ * @return array array with both values and contextid's\r
+ * @access private\r
+ */\r
+ function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {\r
+ if (($sort != 'desc') && ($sort != 'asc')) {\r
+ $sort= 'desc';\r
+ }\r
+\r
+ $oid = $this->_getOID($context, $name);\r
+\r
+ // retrieve the data and return\r
+ $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;\r
+ $query = mysql_query($q);\r
+\r
+ $o = mysql_fetch_array($query);\r
+\r
+ if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {\r
+ $orderby = 'CAST(ovalue AS SIGNED)';\r
+ } else {\r
+ $orderby = 'ovalue';\r
+ }\r
+ $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.$amount;\r
+ $query = mysql_query($q);\r
+ \r
+ // create the array\r
+ $i = 0;\r
+ $top = array();\r
+ while($row = mysql_fetch_array($query)) {\r
+ $top[$i++] = $row;\r
+ }\r
+ \r
+ // return the array (duh!)\r
+ return $top;\r
+ }\r
\r
/**\r
* Returns the plugin ID\r
case 'category':\r
if (!$manager->existsCategory($contextid)) return 0;\r
break;\r
+ case 'item':\r
+ if (!$manager->existsItem($contextid, true, true)) return 0;\r
+ break;\r
case 'global':\r
if ($contextid != 0) return 0;\r
break;\r
}\r
\r
/**\r
+ * splits the option's typeextra field (at ;'s) to split the meta collection\r
+ * @param string $typeExtra the value of the typeExtra field of an option\r
+ * @return array array of the meta-key/value-pairs\r
+ * @author TeRanEX\r
+ * @static\r
+ */\r
+ function getOptionMeta($typeExtra) {\r
+ $tmpMeta = explode(';', $typeExtra);\r
+ $meta = array();\r
+ for ($i = 0; $i < count($tmpMeta); $i++) {\r
+ if (($i == 0) && (!strstr($tmpMeta[0], '='))) {\r
+ // we have the select-list\r
+ $meta['select'] = $tmpMeta[0];\r
+ } else {\r
+ $tmp = explode('=', $tmpMeta[$i]);\r
+ $meta[$tmp[0]] = $tmp[1];\r
+ }\r
+ }\r
+ return $meta;\r
+ }\r
+\r
+ /**\r
+ * filters the selectlists out of the meta collection\r
+ * @param string $typeExtra the value of the typeExtra field of an option\r
+ * @return string the selectlist\r
+ * @author TeRanEX\r
+ */\r
+ function getOptionSelectValues($typeExtra) {\r
+ $meta = NucleusPlugin::getOptionMeta($typeExtra);\r
+ //the select list must always be the first part\r
+ return $meta['select'];\r
+ }\r
+ \r
+ /**\r
+ * checks if the eventlist in the database is up-to-date\r
+ * @return bool if it is up-to-date it return true, else false\r
+ * @author TeRanEX\r
+ */\r
+ function subscribtionListIsUptodate() {\r
+ $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());\r
+ $ev = array();\r
+ while($a = mysql_fetch_array($res)) {\r
+ array_push($ev, $a['event']);\r
+ }\r
+ if (count($ev) != count($this->getEventList())) {\r
+ return false;\r
+ }\r
+ $d = array_diff($ev, $this->getEventList());\r
+ if (count($d) > 0) {\r
+ // there are differences so the db is not up-to-date\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+ \r
+ /**\r
* @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))\r
* (taken from request using requestVar())\r
- *\r
- * (static method)\r
+ * @param $newContextid: integer (accepts a contextid when it is for a new \r
+ * contextid there was no id available at the moment of writing the\r
+ * formcontrols into the page (by ex: itemOptions for new item)\r
+ * @static \r
*/\r
- function _applyPluginOptions(&$aOptions) {\r
+ function _applyPluginOptions(&$aOptions, $newContextid = 0) {\r
+ global $manager;\r
if (!is_array($aOptions)) return;\r
\r
foreach ($aOptions as $oid => $values) {\r
\r
// get option type info\r
- $query = 'SELECT otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);\r
+ $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);\r
$res = sql_query($query);\r
if ($o = mysql_fetch_object($res))\r
{\r
foreach ($values as $contextid => $value) {\r
+ // retreive any metadata\r
+ $meta = NucleusPlugin::getOptionMeta($o->oextra);\r
+ \r
+ // if the option is readonly or hidden it may not be saved\r
+ if (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden')) {\r
+ \r
$value = undoMagic($value); // value comes from request\r
-\r
+ \r
switch($o->otype) {\r
case 'yesno':\r
if (($value != 'yes') && ($value != 'no')) $value = 'no';\r
default:\r
break;\r
}\r
-\r
+ \r
+ // check the validity of numerical options\r
+ if (($meta['datatype'] == 'numerical') && (!is_numeric($value))) { \r
+ //the option must be numeric, but the it isn't\r
+ //use the default for this option\r
+ $value = $o->odef;\r
+ }\r
+ \r
+ // decide wether we are using the contextid of newContextid\r
+ if ($newContextid != 0) {\r
+ $contextid = $newContextid;\r
+ }\r
+ \r
+ //trigger event PrePluginOptionsUpdate to give the plugin the\r
+ //possibility to change/validate the new value for the option\r
+ $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));\r
+ \r
+ // delete the old value for the option\r
sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));\r
sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");\r
-\r
+ }\r
}\r
}\r
}\r
}\r
\r
}\r
-?>
\ No newline at end of file
+?>\r