В папке представлений
/components/com_zoo/views создаём папку
additem для нового представления
в ней файл
view.html.php// no direct access
defined ('_JEXEC') or die ('Restricted access');
class ZooViewAdditem extends YView
{
function display ($tpl = null)
{
global $mainframe, $Itemid;
$params =& $mainframe->getParams ();
$this->params = $params;
switch ($this->getLayout ())
{
case 'add':
{
$this->add ($tpl);
break;
}
case 'edit':
default:
{
$this->edit ($tpl);
break;
}
}
}
function add ($tpl = null)
{
$db =& JFactory::getDBO ();
$user =& JFactory::getUser ();
// get request vars
$option = JRequest::getCmd ('option');
$controller = JRequest::getWord ('controller');
// get types
$table =& JTable::getInstance ('type', 'Table');
$types = $table->getAll ();
// set template vars
$this->assignRef ('user', $user);
$this->assignRef ('controller', $controller);
$this->assignRef ('option', $option);
$this->assignRef ('types', $types);
parent::display ($tpl);
}
function edit ($tpl = null)
{
global $mainframe;
$params =& $mainframe->getParams ();
$type_id = $params->get ('type', 0);
$db =& JFactory::getDBO ();
$user =& JFactory::getUser ();
// get request vars
$option = JRequest::getCmd ('option');
$controller = JRequest::getWord ('controller');
$cid = JRequest::getVar ('cid', array (0), '', 'array');
$edit = intval ($cid[0]) > 0;
// get item
$item = new Item (intval ($cid[0]));
// set defaults, if new
if ($item->id == 0)
{
$item->type_id = $type_id;
if (!$item->type_id)
{
$item->type_id = JRequest::getVar ('type_id', 0);
}
$item->publish_down = $db->getNullDate ();
$selected = array ();
}
else
{
$selected = $item->getRelatedCategoryIds ();
}
// categories select
$lists['select_categories'] = JHTML::_('zoo.categorylist', array (), 'categories[]', 'class="inputbox" size="15" multiple="multiple"', 'value', 'text', $selected);
// set template vars
$this->assignRef ('user', $user);
$this->assignRef ('option', $option);
$this->assignRef ('controller', $controller);
$this->assignRef ('lists', $lists);
$this->assignRef ('item', $item);
$this->assign ('type', $item->getType ());
parent::display ($tpl);
}
}
файл
index.html - пустой
папку [/b]tmpl[/b]
в этой папке пять файлов:
add.php
<?php
defined ('_JEXEC') or die ('Restricted access');
JFilterOutput::objectHTMLSafe ($this->item, ENT_QUOTES);
?>
<?php $link = JRoute::_('index.php?option='.$this->option.'&view=additem&layout=edit');?>
<form action="index.php" method="post" name="adminForm" accept-charset="utf-8">
<table class="admintable" width="100%">
<tr valign="top">
<td width="60%">
<fieldset>
<legend>
<?php echo JText::_('Select Item Type'); ?>
</legend>
<ul id="item-type" class="jtree">
<ul>
<?php for ($i = 0, $n = count ($this->types); $i < $n; $i++) : ?>
<li>
<div class="node-open">
<span></span>
<a href="<?php echo $link.'&type_id='.$this->types[$i]->id ?>" id= "types">
<?php echo $this->types[$i]->name; ?>
</a>
</div>
</li>
<?php endfor; ?>
</ul>
</ul>
</fieldset>
</td>
<td width="40%">
</td>
</tr>
</table>
<input type="hidden" name="option" value="<?php echo $this->option; ?>" />
<input type="hidden" name="controller" value="<?php echo $this->controller; ?>" />
<input type="hidden" name="task" value="" />
<?php echo JHTML::_('form.token'); ?>
</form>
add.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Add item of any type">
<message>
<![CDATA[Item Layout]]>
</message>
</layout>
<state>
<name>Add item of any type</name>
<description>Add item of any type</description>
<params>
</params>
</state>
</metadata>
edit.php
<?php
defined ('_JEXEC') or die ('Restricted access');
JFilterOutput::objectHTMLSafe ($this->item, ENT_QUOTES);
jimport ('joomla.html.pane');
$db =& JFactory::getDBO ();
$editor =& JFactory::getEditor ();
$pane =& JPane::getInstance ('sliders');
$row = $this->item;
$format = JText::_('DATE_FORMAT_LC2');
JHTML::_('behavior.tooltip');
JHTML::stylesheet ('admin.css', 'components/com_zoo/templates/building/assets/css/');
?>
<?php /* Если указано в настройках, то показываем заголовок страницы */ ?>
<?php if ($this->params->get ('show_page_title')) : ?>
<div class="componentheading<?php echo $this->params->get ('pageclass_sfx');?>">
<?php echo $this->escape ($this->params->get ('page_title')); ?>
</div>
<?php endif; ?>
<form id="item-edit" action="<?php echo JRoute::_('index.php?option=com_zoo&view=additem&layout=add')?>" method="post" name="adminForm" accept-charset="utf-8" enctype="multipart/form-data">
<fieldset class="adminform">
<legend>
<?php echo JText::_('Details'); ?>
</legend>
<table class="admintable">
<tr>
<td width="110" class="key">
<label for="name">
<?php echo JText::_('Name'); ?>
</label>
</td>
<td>
<input class="inputbox" type="text" name="name" id="name" size="60" value="<?php echo $row->name; ?>" />
</td>
</tr>
<tr>
<td width="110" class="key">
<label for="alias">
<?php echo JText::_('Alias'); ?>
</label>
</td>
<td>
<input class="inputbox" type="text" name="alias" id="alias" size="60" value="<?php echo $this->item->alias; ?>" /><span class="notice"></span>
</td>
</tr>
<tr>
<td width="110" class="key">
<label for="type">
<?php echo JText::_('Type'); ?>
</label>
</td>
<td>
<?php echo $this->type->name; ?>
</td>
</tr>
<tr>
<td width="110" class="key">
<label>
<?php echo JText::_('Published'); ?>
</label>
</td>
<td>
<?php
$text = JText::_('No');
$val = 0;
$options[] = JHTML::_('select.option', $val, $text);
$text = JText::_('Yes');
$val = 1;
$options[] = JHTML::_('select.option', $val, $text);
echo JHTML::_('select.radiolist', $options, "state", null, 'value', 'text', $row->state);
?>
</td>
</tr>
<tr>
<td width="110" class="key">
<label>
<?php echo JText::_('Categories'); ?>
</label>
</td>
<td>
<?php echo $this->lists['select_categories']; ?>
</td>
</tr>
</table>
<?php echo JHTML::_('type.edit', $this->item); ?>
</fieldset>
<?php
$db =& JFactory::getDBO ();
$create_date = null;
$nullDate = $db->getNullDate ();
// used to hide "Reset Hits" when hits = 0
if (!$row->hits)
{
$visibility = 'style="display: none; visibility: hidden;"';
}
else
{
$visibility = '';
}
?>
<table width="100%" style="border: 1px dashed silver; padding: 5px; margin: 6px 0px 10px 0px;">
<?php if ($row->id) : ?>
<tr>
<td>
<strong>
<?php echo JText::_('Item ID'); ?>:
</strong>
</td>
<td>
<?php echo $row->id; ?>
</td>
</tr>
<?php endif ?>
<tr>
<td>
<strong>
<?php echo JText::_('State'); ?>
</strong>
</td>
<td>
<?php echo $row->state > 0 ? JText::_('Published') : ($row->state < 0 ? JText::_('Archived') : JText::_('Draft Unpublished'));?>
</td>
</tr>
<tr>
<td>
<strong>
<?php echo JText::_('Hits'); ?>
</strong>
</td>
<td>
<?php echo $row->hits;?>
<span <?php echo $visibility; ?>>
<input name="reset_hits" type="button" class="button" value="<?php echo JText::_('Reset'); ?>" onclick="submitbutton ('resethits');" />
</span>
</td>
</tr>
<tr>
<td>
<strong>
<?php echo JText::_('Created'); ?>
</strong>
</td>
<td>
<?php
if ($row->created == $nullDate)
{
echo JText::_('New item');
}
else
{
echo JHTML::_('date', $row->created, $format);
}
?>
</td>
</tr>
<tr>
<td>
<strong>
<?php echo JText::_('Modified'); ?>
</strong>
</td>
<td>
<?php
if ($row->modified == $nullDate)
{
echo JText::_('Not modified');
}
else
{
echo JHTML::_('date', $row->modified, JText::_('DATE_FORMAT_LC2'));
}
?>
</td>
</tr>
</table>
<?php
// Create the form
$form = new JParameter ('', JPATH_COMPONENT_SITE.DS.'models'.DS.'item.xml');
// Details Group
$active = (intval ($row->created_by)? intval ($row->created_by) : $this->user->get ('id'));
$form->set ('created_by', $active);
$form->set ('access', $row->access);
$form->set ('created_by_alias', $row->created_by_alias);
$form->set ('created', JHTML::_('date', $row->created, '%Y-%m-%d %H:%M:%S'));
$form->set ('publish_up', JHTML::_('date', $row->publish_up, '%Y-%m-%d %H:%M:%S'));
if (JHTML::_('date', $row->publish_down, '%Y') <= 1969 || $row->publish_down == $db->getNullDate ())
{
$form->set ('publish_down', JText::_('Never'));
}
else
{
$form->set ('publish_down', JHTML::_('date', $row->publish_down, '%Y-%m-%d %H:%M:%S'));
}
// Metadata Group
$form->set ('description', $row->metadesc);
$form->set ('keywords', $row->metakey);
$form->loadINI ($row->metadata);
$title = JText::_('Parameters - Item');
echo $pane->startPane ("content-pane");
echo $pane->startPanel ($title, "detail-page");
echo $form->render ('details');
$title = JText::_('Metadata Information');
echo $pane->endPanel ();
echo $pane->startPanel ($title, "metadata-page");
echo $form->render ('meta', 'metadata');
echo $pane->endPanel ();
echo $pane->endPane ();
?>
<input type="hidden" id="image_name" name="image_name" value="" />
<input type="hidden" id="image_folder" name="image_folder" value="" />
<input type="hidden" name="option" value="<?php echo $this->option; ?>" />
<input type="hidden" name="controller" value="<?php echo $this->controller; ?>" />
<input type="hidden" id="task" name="task" value="save" />
<input type="hidden" id="apply" name="apply" value="0" />
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<input type="hidden" name="cid[]" value="<?php echo $row->id; ?>" />
<input type="hidden" name="type_id" value="<?php echo $row->type_id; ?>" />
<input type="hidden" name="hits" value="<?php echo $row->hits; ?>" />
<?php echo JHTML::_('form.token'); ?>
<input type="submit" value="<?php echo JText::_('Save')?>" />
</form>
edit.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Add item of concrete type">
<message>
<![CDATA[Item Layout]]>
</message>
</layout>
<state>
<name>Add item Layout</name>
<description>Add item Layout</description>
<params addpath="/administrator/components/com_zoo/joomla/elements">
<param name="type" type="zootype" default="" label="Category" description="Set a Category" />
</params>
</state>
</metadata>
index.html - пустой
в папку /administrator/components/com_zoo/joomla/elements добавляем файл
zootype.php // no direct access
defined ('_JEXEC') or die ('Restricted access');
// load config
require_once (JPATH_ADMINISTRATOR.'/components/com_zoo/config.php');
class JElementZooType extends JElement
{
var $_name = 'ZooType';
function fetchElement ($name, $value, &$node, $control_name)
{
$options = array (JHTML::_('select.option', '0:0', '- '.JText::_('Select Type').' -'));
return JHTML::_('zoo.typelist', $options, $control_name.'['.$name.']', null, 'value', 'text', $value);
}
}
в файл /components/com_zoo/controller.php добавляем функции:
function save()
{
// check for request forgeries
JRequest::checkToken() or jexit('Invalid Token');
// init vars
$msg = null;
$db =& JFactory::getDBO();
$config =& JFactory::getConfig();
$user =& JFactory::getUser();
$now =& JFactory::getDate();
$option = JRequest::getCmd('option');
$post = JRequest::get('post');
$categories = JRequest::getVar('categories', null, 'post', 'array');
$details = JRequest::getVar('details', array(), 'post', 'array');
$metadata = JRequest::getVar('meta', array(), 'post', 'array');
$cid = JRequest::getVar('cid', array(0), 'post', 'array');
$id = intval($cid[0]);
$Itemid = JRequest::getVar('Itemid', 0, 'GET', 'int');
$tzoffset = $config->getValue('config.offset');
$post = array_merge($post, $details);
// get item
$item = new Item($id);
// set type, if new
if ($item->id == 0)
{
$item->type_id = JRequest::getInt('type_id', 0);
}
// filter post data
$post = ItemHelper::filterElementData($item, $post);
// bind post data
$item->bind($post);
$item->bindMetadata($metadata);
$item->bindElements($post);
// filter alias
$item->alias = JFilterOutput::stringURLSafe($item->alias == '' ? $item->name : $item->alias);
// set modified
$item->modified = $now->toMySQL();
$item->modified_by = $user->get('id');
$item->created_by = $user->get('id');
// set created date
if ($item->created && strlen(trim($item->created)) <= 10)
{
$item->created .= ' 00:00:00';
}
$date =& JFactory::getDate($item->created, $tzoffset);
$item->created = $date->toMySQL();
// set publish up date
if (strlen(trim($item->publish_up)) <= 10)
{
$item->publish_up .= ' 00:00:00';
}
$date =& JFactory::getDate($item->publish_up, $tzoffset);
$item->publish_up = $date->toMySQL();
// set publish down date
if (trim($item->publish_down) == JText::_('Never') || trim($item->publish_down) == '')
{
$item->publish_down = $db->getNullDate();
}
else
{
if (strlen(trim($item->publish_down)) <= 10)
{
$item->publish_down .= ' 00:00:00';
}
$date =& JFactory::getDate($item->publish_down, $tzoffset);
$item->publish_down = $date->toMySQL();
}
// save item
if ($item->save())
{
// save items category relations
$table =& JTable::getInstance('categoryitem', 'Table');
if ($table->saveCategoryItemRelations($item, $categories))
{
$msg = JText::_('Item Saved');
}
else
{
JError::raiseNotice(0, JText::_('Error Saving Item').' ('.$table->getError().')');
$this->_task = 'apply';
}
}
else
{
JError::raiseNotice(0, JText::_('Error Saving Item').' ('.$item->getError().')');
$this->_task = 'apply';
}
if (JRequest::getInt('apply', 0, 'post', 'int'))
{
$this->_task = 'apply';
}
switch ($this->_task)
{
case 'apply':
{
$link = 'index.php?option=com_zoo&view=additem&layout=edit&cid='.$id.'&Itemid='.$Itemid;
break;
}
case 'save':
default:
{
// URL куда будет перенаправлен пользователь после сохранения
$link = 'index.php';
break;
}
}
// Перенаправляем пользователя
$this->setRedirect($link, $msg);
}
function resethits ()
{
// check for request forgeries
JRequest::checkToken () or jexit ('Invalid Token');
// init vars
$msg = null;
$option = JRequest::getCmd ('option');
$cid = JRequest::getVar ('cid', array (0), 'post', 'array');
$Itemid = JRequest::getVar ('Itemid', 0, 'GET', 'int');
$id = intval ($cid[0]);
// get item
$item = new Item ($id);
// reset hits
if ($item->hits > 0)
{
$item->hits = 0;
// save item
if ($item->save ())
{
$msg = JText::_('Item Hits Reseted');
}
else
{
JError::raiseNotice (0, JText::_('Error Reset Item Hits').' ('.$item->getError ().')');
}
}
$link = 'index.php?option=com_zoo&view=additem&layout=edit&cid='.$item->id.'&Itemid='.$Itemid;
$this->setRedirect ($link, $msg, 'message');
}