plugins:plugincatecories:np_plugincatecories_code

NP_PluginCategories.phpのソースコード

<?php
/* The licence of this script is GPL */
class NP_PluginCategories extends NucleusPlugin { 
	function getName() { return 'NP_PluginCategories'; }
	function getMinNucleusVersion() { return 220; }
	function getAuthor()  { return 'Katsumi'; }
	function getVersion() { return '0.1.6'; }
	function getURL() {return 'http://japan.nucleuscms.org/bb/viewtopic.php?t=2995';}
	function getDescription() { return $this->getName().' plugin'; } 
	function supportsFeature($what) { return (int)($what=='SqlTablePrefix'); }
	function getEventList() { return array('QuickMenu','PrePluginOptionsEdit'); }
	function getTableList() { return $this->sql_query('list'); }
	function install() {
		$this->sql_query('create','(
				id int(11) not null auto_increment,
				pid int(11) not null default 0,
				cname varchar(63) not null default "",
				PRIMARY KEY (id)
			)');
		$this->createOption('quickmenu','Quick menu?','yesno','yes');
		$this->createOption('deltable','Delete table when uninstalling?','yesno','no');
		$this->createOption('categories','Categories:','textarea','');
 	}
	function unInstall() {
		if ($this->getOption('deltable')=='yes') $this->sql_query('drop');
	}
	function event_QuickMenu(&$data) {
		global $member,$CONF;
		if($this->getOption('quickmenu')=='no' || !$member->isAdmin()) return;
		array_push($data['options'],array('title' => 'Plugin Categories',
						'url' => $CONF['NucleusURL'].'index.php?action=pluginoptions&np_plugincategories_select=&plugid='.(int)$this->getId(),
						'tooltip' => 'NP_PluginCategories'));
	}
	var $sqldata=array();
	var $categories=array();
	function _init() {
		if ( count($this->sqldata) || count($this->categories) ) return;
		// Cache the data from MySQL table
		$res=$this->sql_query('SELECT * FROM');
		while($row=mysql_fetch_assoc($res)) $this->sqldata[$row['pid']]=$row['cname'];
		// Cache the data of category names
		$cats=str_replace("\r","\n", $this->getOption('categories') );
		foreach(explode("\n",$cats) as $value){
			if ($value) $this->categories[]=$value;
		}
	}
	function event_PrePluginOptionsEdit(&$data){
		if ($data['context']!='global') return;
		$this->_init();
		if ($data['plugid']==$this->getID() && isset($_GET['np_plugincategories_select'])){
			// Show the pluginlist of selected category
			// Reset the form.
			echo "</form>\n";
			// Hide the "<h2>Plugin option of NP_PluginCategories</h2>"
			echo '<style type="text/css">/*<![CDATA[*/#content h2 { display: none; }/*]]>*/</style>';
			echo '<h2 style="display:block;">' , _PLUGS_TITLE_MANAGE , ' ', help('plugins'), '</h2>';
			echo '<h3>' , _PLUGS_TITLE_INSTALLED , '</h3>';
			$this->showCategory();
			// Again output the form without showing it.
			echo '<form style="display:none;">';
			return;
		}
		if (is_array($_POST['np_plugincategories'])) {
			// Change the category
			foreach($_POST['np_plugincategories'] as $pid=>$category) $this->_updateCategory($pid,$category);
		}
		echo "<table><tr><th>Plugin Category</th><th></th></tr><tr><td>\n";
		if ($data['plugid']==$this->getID()) {
			// Show the select tags for all installed plugins.
			$query =  'SELECT * FROM '.sql_table('plugin').' ORDER BY porder ASC';
			$res=sql_query($query);
			$right=0;
			while($row=mysql_fetch_assoc($res)){
				echo '<select name="np_plugincategories['.(int)$row['pid'].']">';
				echo $this->_createOptions($row['pid']);
				echo '</select>'.htmlspecialchars($row['pfile']);
				if ($right) echo "</td></tr><tr><td>\n";
				else echo "</td><td>\n";
				$right=1-$right;
			}
			$this->showLink("without option link \n");
			if (!$right) echo "</td><td>\n";
		} else {
			// Show the select tag for this plugin.
			echo '<select name="np_plugincategories['.(int)$data['plugid'].']">';
			echo $this->_createOptions($data['plugid']);
			echo "</select>\n";
			echo "</td><td>\n";
			$this->showLink();
		}
		echo "</td></tr></table>\n";
	}
	function _updateCategory($pid,$category){
		// Delete, first
		$this->sql_query('DELETE FROM','WHERE pid='.(int)$pid);
		// Insert if the categoryname exists
		if ($category && in_array($category,$this->categories)) {
			$this->sql_query('INSERT INTO ',
				' SET cname="'.addslashes($category).'",'.
				' pid='.(int)$pid);
		}
		// Change the cached data
		$this->sqldata[(int)$pid]=$category;
	}
	function _createOptions($id){
		$options='';
		$selected=false;
		foreach($this->categories as $cat){
			$cat=htmlspecialchars($cat);
			if ($this->sqldata[(int)$id]==$cat) {
				$selected=true;
				$options.= '<option value="'.$cat.'" selected>';
			} else $options.= '<option value="'.$cat.'">';
			$options.= $cat.'</option>';
		}
		if ($selected) $options.= '<option value="">others</option>';
		else $options.= '<option value="" selected>others</option>';
		return $options;
	}
	function showLink($cat="\n"){
		// Show the links for category selection.
		echo '<div>';
		$categories=array();
		foreach($this->categories as $value) $categories[$value]=$value;
		$categories['']='others';
		foreach($categories as $key=>$value) {
			if ($key==$cat) echo '[<b style="color:red;"><big>'.htmlspecialchars($value).'</big></b>] ';
			else {
				echo '[<a href="'.htmlspecialchars('?action=pluginoptions&plugid='.
					(int)$this->getId().
					'&np_plugincategories_select='.urlencode($key)).'">'.
					htmlspecialchars($value).'</a>] ';
			}
		}
		if ($cat!="without option link \n") echo '&lt;<a href="'.htmlspecialchars('?action=pluginoptions&plugid='.
			(int)$this->getId()).'">Options&nbsp;for&nbsp;NP_PluginCategories</a>&gt;';
		echo "</div>\n";
	}
	function showCategory(){
		// Show the plugin list of a category.
		$cat=$_GET['np_plugincategories_select'];
		$this->showLink($cat);
		$plugins=array();
		$res=sql_query('SELECT pid FROM '.sql_table('plugin'));
		while($row=mysql_fetch_array($res)){
			if ($cat==(string)@$this->sqldata[$row[0]]) $plugins[]=(int)$row[0];
		}
		if (!count($plugins)) {
			echo 'No plugin belongs to this category';
			return;
		}
		$plugins=implode(',',$plugins);
		$query = 'SELECT * FROM '.sql_table('plugin').
			' WHERE pid in ('.$plugins.')'.
			' ORDER BY porder ASC';
		$template['content'] = 'pluginlist';
		$template['tabindex'] = 10;
		showlist($query, 'table', $template);
	}
	// SQL table stuff
	function sql_query($mode='name',$p1=''){
		$tablename[0]=sql_table(strtolower('plugin_'.substr(get_class($this),3)));
		switch($mode){
		case 'create': return sql_query('CREATE TABLE IF NOT EXISTS '.$tablename[0].' '.$p1);
		case 'drop':   return sql_query('DROP TABLE IF EXISTS '.$tablename[0]);
		case 'list':   return $tablename;
		case 'name':   return $tablename[0];
		default:       return sql_query($mode.' '.$tablename[0].' '.$p1);
		}
	}
	function getOption($name){ return $this->_getOption('global', 0, $name); }//required for Nucleus 3.24
}
?>
 
plugins/plugincatecories/np_plugincatecories_code.txt · 最終更新: 2011/03/23 21:28 (外部編集)