Да.
<?php
/**
* @package Joomla.Administrator
* @subpackage com_comfilms
*
* @copyright Copyright (C) 2015, Dmitry. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
defined('_JEXEC') or die;
/**
* Banner model.
*
* @package Joomla.Administrator
* @subpackage com_comfilms
* @since 1.6
*/
class ComfilmsModelFilm extends JModelAdmin {
/**
* @var string The prefix to use with controller messages.
* @since 1.6
*/
protected $text_prefix = 'COM_COMFILMS_FILM';
/**
* Method to perform batch operations on an item or a set of items.
*
* @param array $commands An array of commands to perform.
* @param array $pks An array of item ids.
* @param array $contexts An array of item contexts.
*
* @return boolean Returns true on success, false on failure.
*
* @since 2.5
*/
public function batch($commands, $pks, $contexts){
// Sanitize user ids.
$pks = array_unique($pks);
JArrayHelper::toInteger($pks);
// Remove any values of zero.
if(array_search(0, $pks, true)){
unset($pks[array_search(0, $pks, true)]);
}
if(empty($pks)){
$this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
return false;
}
$done = false;
if(!empty($commands['category_id'])){
$cmd = JArrayHelper::getValue($commands, 'move_copy', 'c');
if($cmd == 'c'){
$result = $this->batchCopy($commands['category_id'], $pks, $contexts);
if(is_array($result)){
$pks = $result;
}else{
return false;
}
}elseif($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)){
return false;
}
$done = true;
}
if(!$done){
$this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
return false;
}
// Clear the cache
$this->cleanCache();
return true;
}
/**
* Batch copy items to a new category or current.
*
* @param integer $value The new category.
* @param array $pks An array of row IDs.
* @param array $contexts An array of item contexts.
*
* @return mixed An array of new IDs on success, boolean false on failure.
*
* @since 2.5
*/
protected function batchCopy($value, $pks, $contexts){
$categoryId = (int) $value;
$table = $this->getTable();
$i = 0;
// Check that the category exists
if ($categoryId){
$categoryTable = JTable::getInstance('Category');
if (!$categoryTable->load($categoryId)){
if ($error = $categoryTable->getError()){
// Fatal error
$this->setError($error);
return false;
}else{
$this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
return false;
}
}
}
if (empty($categoryId)){
$this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
return false;
}
// Check that the user has create permission for the component
$user = JFactory::getUser();
if (!$user->authorise('core.create', 'com_comfilms.category.' . $categoryId)){
$this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
return false;
}
// Parent exists so we let's proceed
while (!empty($pks)){
// Pop the first ID off the stack
$pk = array_shift($pks);
$table->reset();
// Check that the row actually exists
if (!$table->load($pk)){
if ($error = $table->getError()){
// Fatal error
$this->setError($error);
return false;
}else{
// Not fatal error
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
continue;
}
}
// Alter the title & alias
$data = $this->generateNewTitle($categoryId, $table->alias, $table->name);
$table->name = $data['0'];
$table->alias = $data['1'];
// Reset the ID because we are making a copy
$table->id = 0;
// TODO: Deal with ordering?
//$table->ordering = 1;
// Check the row.
if (!$table->check()){
$this->setError($table->getError());
return false;
}
// Store the row.
if (!$table->store()){
$this->setError($table->getError());
return false;
}
// Get the new item ID
$newId = $table->get('id');
// Add the new ID to the array
$newIds[$i] = $newId;
$i++;
}
// Clean the cache
$this->cleanCache();
return $newIds;
}
/**
* Method to test whether a record can be deleted.
*
* @param object $record A record object.
*
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
*
* @since 1.6
*/
protected function canDelete($record){
if (!empty($record->id)){
if ($record->published != -2){
return;
}
return parent::canDelete($record);
}
}
/**
* Method to test whether a record can have its state changed.
*
* @param object $record A record object.
*
* @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
*
* @since 1.6
*/
protected function canEditState($record){
$user = JFactory::getUser();
// Check against the category.
if (!empty($record->category_id)){
return $user->authorise('core.edit.state', 'com_comfilms.category.' . (int) $record->category_id);
}else{// Default to component settings if category not known.
return parent::canEditState($record);
}
}
/**
* Returns a JTable object, always creating it.
*
* @param string $type The table type to instantiate. [optional]
* @param string $prefix A prefix for the table class name. [optional]
* @param array $config Configuration array for model. [optional]
*
* @return JTable A database object
*
* @since 1.6
*/
public function getTable($type = 'Film', $prefix = 'ComfilmsTable', $config = array()){
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to get the record form.
*
* @param array $data Data for the form. [optional]
* @param boolean $loadData True if the form is to load its own data (default case), false if not. [optional]
*
* @return mixed A JForm object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true){
// Get the form.
$form = $this->loadForm('com_comfilms.film', 'film', array('control' => 'jform', 'load_data' => $loadData));
if(empty($form)){
return false;
}
// Determine correct permissions to check.
if ($this->getState('film.id')){
// Existing record. Can only edit in selected categories.
$form->setFieldAttribute('catid', 'action', 'core.edit');
}else{
// New record. Can only create in selected categories.
$form->setFieldAttribute('catid', 'action', 'core.create');
}
// Modify the form based on access controls.
if (!$this->canEditState((object) $data)){
// Disable fields for display.
$form->setFieldAttribute('ordering', 'disabled', 'true');
$form->setFieldAttribute('publish_up', 'disabled', 'true');
$form->setFieldAttribute('publish_down', 'disabled', 'true');
$form->setFieldAttribute('state', 'disabled', 'true');
// Disable fields while saving.
// The controller has already verified this is a record you can edit.
$form->setFieldAttribute('ordering', 'filter', 'unset');
$form->setFieldAttribute('publish_up', 'filter', 'unset');
$form->setFieldAttribute('publish_down', 'filter', 'unset');
$form->setFieldAttribute('state', 'filter', 'unset');
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData(){
// Check the session for previously entered form data.
$app = JFactory::getApplication();
$data = $app->getUserState('comfilms.edit.film.data', array());
if(empty($data)){
$data = $this->getItem();
// Prime some default values.
if($this->getState('film.id') == 0){
$data->set('catid', $app->input->getInt('catid', $app->getUserState('com_comfilms.films.filter.category_id')));
}
// bands
if($data->taguss){
$data->taguss = explode(',', $data->taguss);
}
// city
if($data->city){
$data->city = explode(',', $data->city);
}
}
return $data;
}
public function save( $data ){
$data['city'] = json_encode( $data['city'] );
parent::save( $data );
}
/*protected function loadFormData(){
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_MYCOMPONENT.edit.MYVIEW.data', array());
if (empty($data)){
$data = $this->getItem();
};
// bands
if($data->taguss){
$data->taguss = explode(',', $data->taguss);
}
return $data;
}
*/
/**
* A protected method to get a set of ordering conditions.
*
* @param JTable $table A record object.
*
* @return array An array of conditions to add to add to ordering queries.
*
* @since 1.6
*/
protected function getReorderConditions($table){
$condition = array();
$condition[] = 'catid = '. (int) $table->catid;
$condition[] = 'state >= 0';
return $condition;
}
/**
* @since 3.0
*/
protected function prepareTable($table){
$date = JFactory::getDate();
$user = JFactory::getUser();
if (empty($table->id)){
// Set the values
$table->created = $date->toSql();
// Set ordering to the last item if not set
if (empty($table->ordering)){
$db = JFactory::getDbo();
$db->setQuery('SELECT MAX(ordering) FROM #__comfilms_films');
$max = $db->loadResult();
$table->ordering = $max + 1;
}
}else{
// Set the values
$table->modified = $date->toSql();
$table->modified_by = $user->get('id');
}
}
}