ゲスト用には見えなくてユーザー登録すると見えるQ&Aが欲しいとのオーダーにより作成しました。
まずは、SQL。ユーザーグループを指定できればいいのでシンプルに整数で。
CREATE TABLE plzxoo_category (済んだら class CategoryEditFormで編集用のインターフェースを準備
`cid` int(10) auto_increment,
`pid` int(10) NOT NULL default 0,
`name` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`size` mediumint(5) NOT NULL default 0,
`weight` mediumint(5) NOT NULL default 0,
`groupid` tinyint(1) NOT NULL default 0,
PRIMARY KEY (`cid`) ,
KEY (`weight`) ,
KEY (`pid`)
) TYPE=MyISAM;
<?phplanguage/hoge/admin.phpに変数追加して
require_once "exForm/Form.php";
class CategoryEditForm extends exActionFormEx
{
var $cid_;
var $pid_;
var $name_;
var $description_;
var $groupid_;
function fetch(&$master) {
$this->pid_ = intval($_POST['pid']);
// pid 妥当性検査
if($this->pid_) {
$handler=&plzXoo::getHandler('category');
$obj=&$handler->get($this->pid_);
if(!is_object($obj))
$this->addError(_MD_PLZXOO_ERROR_PID_INJURY);
}
$this->name_ = trim($_POST['name']);
if(!$this->name_) {
$this->addError(_MD_PLZXOO_ERROR_NAME_REQUIRED);
}
if(!$this->validateMaxLength($this->name_, 255)) {
$this->addError(_MD_PLZXOO_ERROR_NAME_SIZEOVER);
}
$this->description_ = $_POST['description'];
$this->weight_ = intval( $_POST['weight'] ) ;
$this->groupid_ = intval( $_POST['groupid'] ) ;
}
function load(&$master) {
$this->cid_ = $master->getVar ( 'cid', 'e' );
$this->pid_ = $master->getVar ( 'pid', 'e' );
$this->name_ = $master->getVar ( 'name', 'e' );
$this->weight_ = $master->getVar ( 'weight', 'e' );
$this->groupid_ = $master->getVar ( 'groupid', 'e' );
$this->description_ = $master->getVar ( 'description', 'e' );
}
function update(&$master) {
$master->setVar ( 'pid', $this->pid_ );
$master->setVar ( 'name', $this->name_ );
$master->setVar ( 'weight', $this->weight_ );
$master->setVar ( 'groupid', $this->groupid_ );
$master->setVar ( 'description', $this->description_ );
}
}
?>
define ( '_MD_A_PLZXOO_LANG_GROUPID','groupid' );
管理者画面に項目を追加。
<?php
require_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";
class default_CategoryView_input
{
function &execute (&$controller, &$request, &$user)
{
$editform=&$request->getAttribute('editform');
$form = new XoopsThemeForm(_MD_A_PLZXOO_LANG_EDIT_CATEGORY,'Category','','POST');
$form->addElement(new XoopsFormHidden('cid',$editform->cid_));
$form->addElement(new XoopsFormText(_MD_A_PLZXOO_LANG_NAME,'name',64,255,$editform->name_));
//-------------------------
// 親カテゴリ
//-------------------------
$select =new XoopsFormSelect(_MD_A_PLZXOO_LANG_PARENT_CATEGORY,'pid',$editform->pid_);
$select->addOption(0, _MD_A_PLZXOO_LANG_TOP );
$categories=&$request->getAttribute('categories');
foreach($categories as $category){
$select->addOption($category->getVar('cid'),$category->getVar('name'));
}
$form->addElement($select);
unset($select);
$form->addElement(new XoopsFormDhtmlTextArea(_MD_A_PLZXOO_LANG_DESCRIPTION,'description',$editform->description_,6, 50));
$form->addElement(new XoopsFormText(_MD_A_PLZXOO_LANG_WEIGHT,'weight',10,10,intval($editform->weight_)));
$form->addElement(new XoopsFormText(_MD_A_PLZXOO_LANG_GROUPID,'groupid',10,10,intval($editform->groupid_)));
$tray = new XoopsFormElementTray(_MD_A_PLZXOO_LANG_CONTROL);
$tray->addElement( new XoopsFormButton ( '', 'submit', _MD_A_PLZXOO_LANG_SUBMIT, 'submit' ) );
$tray->addElement( new XoopsFormButton ( '', 'reset', _MD_A_PLZXOO_LANG_RESET, 'reset' ) );
$form->addElement($tray);
$renderer = new mojaLE_Renderer($controller,$request,$user);
$renderer->setTemplate('category_edit.tpl');
$renderer->setAttribute('xoopsform',$form);
return $renderer;
}
}
?>
後は、表でロードした時にWHERE条件を追加 category.class.php
/* class function */
function getChildren( $cid )
{
// hack by bluemoon: Added groupid
global $xoopsUser;
if (is_object( $xoopsUser )){
$gids = array_merge( array(0,3) , $xoopsUser->getGroups());
$usergroups = implode( ",", $gids );
if ( $usergroups ) $addwhere = " AND groupid in ($usergroups)";
}else{
$addwhere = " AND groupid in (0,3)";
}
$db =& Database::getInstance() ;
$sql = "SELECT `cid`,`name`,`groupid` FROM ".$db->prefix('plzxoo_category').
" WHERE `pid`=".intval($cid).$addwhere." ORDER BY `weight`" ;
$result = $db->query( $sql ) ;
$ret = array() ;
while( list( $cid , $name ) = $db->fetchRow( $result ) ) {
$ret[] = array( $cid => $name ) ;
}
return $ret ;
}
これでOK。無事会員専用Q&Aの出来上がり。
0 件のコメント:
コメントを投稿