Current Path :
/
home
/
develito
/
public_html
/
components
/
com_sobipro
/
lib
/
ctrl
/
adm
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/develito/public_html/components/com_sobipro/lib/ctrl/adm/field.php
<?php /** * @package: SobiPro Library * * @author * Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH * Email: sobi[at]sigsiu.net * Url: https://www.Sigsiu.NET * * @copyright Copyright (C) 2006 - 2020 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved. * @license GNU/LGPL Version 3 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 * as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3. * See https://www.gnu.org/licenses/lgpl.html and https://www.sigsiu.net/licenses. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * @created 10-Jan-2009 by Radek Suski * @modified 15 October 2020 by Sigrid Suski */ defined( 'SOBIPRO' ) || exit( 'Restricted access' ); use Sobi\Input\Input; SPLoader::loadController( 'controller' ); SPLoader::loadController( 'field' ); /** * Class SPFieldAdmCtrl */ final class SPFieldAdmCtrl extends SPFieldCtrl { /** @var string */ protected $_type = 'field'; /*** @var string */ protected $_fieldType = 'field'; /*** @var array */ private $attr = []; /** @var bool */ protected $_category = false; /** * Editing/adding a field. * * @throws SPException * @throws \Sobi\Error\Exception */ protected function edit() { $fid = Input::Int( 'fid' ); $this->checkTranslation(); if ( !$fid ) { return $this->add(); /* if adding new field - call #add */ } if ( $this->isCheckedOut( $fid ) ) { SPFactory::message()->error( Sobi::Txt( 'FM.IS_CHECKED_OUT' ), false ); } else { $this->checkOut( $fid ); /* check it out */ } /* load base data */ $f = $this->loadField( $fid ); /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); $field->extend( $f ); $groups = $this->getFieldGroup( $f->fieldType, $f->tGroup ); $type = $f->fType . ' ( ' . $f->tGroup . ' / ' . $f->fieldType . ' )'; $this->_fieldType = $f->fieldType; /* get input filters */ $registry = SPFactory::registry(); $registry->loadDBSection( 'fields_filter' ); $helpTask = 'field.' . $field->get( 'fieldType' ); $registry->set( 'help_task', $helpTask ); $filters = $registry->get( 'fields_filter' ); $f = [ 0 => Sobi::Txt( 'FM.NO_FILTER' ) ]; if ( count( $filters ) ) { foreach ( $filters as $filter => $data ) { $f[ $filter ] = Sobi::Txt( $data[ 'value' ] ); } } /* get view class */ /** @var SPView $view */ $view = SPFactory::View( 'field', true ); $view->addHidden( Input::Int( 'fid' ), 'fid' ); $view->addHidden( Input::Sid(), 'sid' ); $view->addHidden( $this->_category, 'category-field' ); if ( $this->_category ) { $view->addHidden( -1, 'field.adminField' ); $view->addHidden( $this->_fieldType, 'field.fieldType' ); } $view->assign( $groups, 'types' ); $view->assign( $type, 'type' ); $view->assign( $f, 'filters' ); $view->assign( $field, 'field' ); $view->assign( $this->_category, 'category-field' ); $view->assign( $this->_task, 'task' ); $languages = $view->languages(); $view->assign( $languages, 'languages-list' ); $field->onFieldEdit( $view ); $this->loadTemplate( $field, $view ); /** Legacy code for 1.0 fields */ // if ( !( $this->loadTemplate( $field, $view ) ) ) { // $view->assign( $helpTask, '_compatibility' ); // if ( SPLoader::translatePath( 'field.edit.' . $field->get( 'fieldType' ), 'adm', true, 'ini' ) ) { // $view->loadConfig( 'field.edit.' . $field->get( 'fieldType' ) ); // } // $view->setTemplate( 'field.edit' ); // if ( SPLoader::translatePath( 'field.edit.' . $field->get( 'fieldType' ), 'adm' ) ) { // $view->setTemplate( 'field.edit.' . $field->get( 'fieldType' ) ); // } // SPFactory::header()->addCssFile( 'adm.legacy' ); // } $view->display(); } /** * @param $field * @param $view * * @return bool * @throws SPException */ protected function loadTemplate( $field, $view ) { $nid = '/' . Sobi::Section( 'nid' ) . '/'; $disableOverrides = null; if ( is_array( Sobi::My( 'groups' ) ) ) { $disableOverrides = array_intersect( Sobi::My( 'groups' ), Sobi::Cfg( 'templates.disable-overrides', [] ) ); } if ( SPLoader::translatePath( 'field.' . $field->get( 'fieldType' ), 'adm', true, 'xml' ) ) { /** Case we have also override */ /** section override */ if ( !( $disableOverrides ) && SPLoader::translatePath( 'field.' . $nid . $field->get( 'fieldType' ), 'adm', true, 'xml' ) ) { $view->loadDefinition( 'field.' . $nid . $field->get( 'fieldType' ) ); } /** std override */ else { if ( SPLoader::translatePath( 'field.' . $field->get( 'fieldType' ) . '_override', 'adm', true, 'xml' ) ) { $view->loadDefinition( 'field.' . $field->get( 'fieldType' ) . '_override' ); } else { $view->loadDefinition( 'field.' . $field->get( 'fieldType' ) ); } } if ( SPLoader::translatePath( 'field.templates.' . $field->get( 'fieldType' ) . '_override', 'adm' ) ) { $view->setTemplate( 'field.templates.' . $field->get( 'fieldType' ) . '_override' ); } else { if ( SPLoader::translatePath( 'field.templates.' . $nid . $field->get( 'fieldType' ), 'adm' ) ) { $view->setTemplate( 'field.templates.' . $nid . $field->get( 'fieldType' ) ); } else { $view->setTemplate( 'default' ); } } return true; } return false; } /** * @param $fType * @param null $group * * @return mixed * @throws SPException * @throws \Sobi\Error\Exception */ private function getFieldGroup( $fType, $group = null ) { if ( !( $group ) ) { $group = SPFactory::db() ->select( 'tGroup', 'spdb_field_types', [ 'tid' => $fType ] ) ->loadResult(); } /* get cognate field types */ if ( $group != 'special' ) { try { $fTypes = SPFactory::db() ->select( '*', 'spdb_field_types', [ 'tGroup' => $group ], 'fPos' ) ->loadObjectList(); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_GET_FIELD_TYPES_DB_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); } if ( count( $fTypes ) ) { $pre = 'FIELD.TYPE_OPTG_'; foreach ( $fTypes as $type ) { $groups[ str_replace( $pre, null, Sobi::Txt( $pre . $type->tGroup ) ) ][ $type->tid ] = $type->fType; } } } else { $name = SPFactory::db() ->select( 'fType', 'spdb_field_types', [ 'tid' => $fType ] ) ->loadResult(); $groups[ Sobi::Txt( 'FIELD.TYPE_OPTG_SPECIAL' ) ][ $fType ] = $name; } return $groups; } /** * @param $fType * @param null $group * * @return mixed|string * @throws SPException * @throws \Sobi\Error\Exception */ protected function getPlainFieldType( $fType, $group = null ) { if ( !( $group ) ) { $group = SPFactory::db() ->select( 'tGroup', 'spdb_field_types', [ 'tid' => $fType ] ) ->loadResult(); } /* get cognate field types */ if ( $group != 'special' ) { try { $fTypes = SPFactory::db() ->select( '*', 'spdb_field_types', [ 'tGroup' => $group ], 'fPos' ) ->loadObjectList(); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_GET_FIELD_TYPES_DB_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); } if ( count( $fTypes ) ) { $pre = 'FIELD.TYPE_OPTG_'; foreach ( $fTypes as $type ) { if ( $type->tid == $fType ) { $type = $type->fType . ' ( ' . $type->tGroup . ' / ' . $fType . ' )'; break; } } } } else { $name = SPFactory::db() ->select( 'fType', 'spdb_field_types', [ 'tid' => $fType ] ) ->loadResult(); $type = $name . ' ( ' . $group . ' / ' . $fType . ' )'; } return $type; } /** * @param $fid * * @return mixed * @throws SPException */ private function loadField( $fid ) { /* @var SPdb $db */ $db =& SPFactory::db(); try { $db->select( '*', $db->join( [ [ 'table' => 'spdb_field', 'as' => 'sField', 'key' => 'fieldType' ], [ 'table' => 'spdb_field_types', 'as' => 'sType', 'key' => 'tid' ] ] ), [ 'fid' => $fid ] ); $f = $db->loadObject(); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); } if ( $f->adminField == -1 ) { $this->_category = true; } return $f; } /** * Just when adding new field - first step. * * @throws SPException * @throws \Sobi\Error\Exception */ private function add() { if ( $this->_fieldType ) { $groups = $this->getFieldGroup( $this->_fieldType ); $type = $this->getPlainFieldType( $this->_fieldType ); /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); $field->loadType( $this->_fieldType ); } else { $groups = $this->getFieldTypes(); /* create dummy field with initial values */ $field = [ 'name' => '', 'nid' => '', 'notice' => '', 'description' => '', 'adminField' => 0, 'enabled' => 1, 'fee' => 0, 'isFree' => 1, 'withLabel' => 1, 'version' => 1, 'editable' => 1, 'required' => 0, 'priority' => 5, 'showIn' => 'details', 'editLimit' => '', 'inSearch' => 0, 'cssClass' => '', 'fieldType' => $this->_fieldType, ]; } /* get view class */ /** @var SPView $view */ $view = SPFactory::View( 'field', true ); $task = 'add'; $view->addHidden( Input::Sid(), 'sid' ); $view->addHidden( 0, 'fid' ); $view->addHidden( $this->_category, 'category-field' ); if ( $this->_category ) { $view->addHidden( -1, 'field.adminField' ); $view->addHidden( $this->_fieldType, 'field.fieldType' ); } $view->assign( $groups, 'types' ); $view->assign( $type, 'type' ); $view->assign( $field, 'field' ); $view->assign( $this->_category, 'category-field' ); $view->assign( $task, 'task' ); if ( $this->_fieldType ) { $field->onFieldEdit( $view ); } $registry = SPFactory::registry(); $registry->loadDBSection( 'fields_filter' ); $helpTask = 'field.' . $field->get( 'fieldType' ); $registry->set( 'help_task', $helpTask ); $filters = $registry->get( 'fields_filter' ); $f = [ 0 => Sobi::Txt( 'FM.NO_FILTER' ) ]; if ( count( $filters ) ) { foreach ( $filters as $filter => $data ) { $f[ $filter ] = Sobi::Txt( $data[ 'value' ] ); } } $view->assign( $f, 'filters' ); if ( $this->loadTemplate( $field, $view ) ) { $view->display(); } /** legacy */ // elseif ( SPLoader::translatePath( 'field.edit.' . $this->_fieldType, 'adm' ) ) { // $view->assign( $helpTask, '_compatibility' ); // if ( SPLoader::translatePath( 'field.edit.' . $this->_fieldType, 'adm', true, 'ini' ) ) { // $view->loadConfig( 'field.edit.' . $this->_fieldType ); // } // $view->setTemplate( 'field.edit' ); // if ( SPLoader::translatePath( 'field.edit.' . $this->_fieldType, 'adm' ) ) { // $view->setTemplate( 'field.edit.' . $this->_fieldType ); // } // SPFactory::header()->addCSSCode( '#toolbar-box { display: block }' ); // $view->display(); // } else { Sobi::Error( $this->name(), SPLang::e( 'NO_FIELD_DEF' ), SPC::WARNING, 500, __LINE__, __FILE__ ); } } /** * @param false $category * * @return array|array[] * @throws SPException * @throws \Sobi\Error\Exception */ protected function getFieldTypes( $category = false ) { static $fTypes = null; if ( !( $fTypes ) ) { /* get all existing field types */ try { $fTypes = SPFactory::db() ->select( '*', 'spdb_field_types', null, 'fPos' ) ->loadObjectList(); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); } $groups = []; } if ( count( $fTypes ) ) { $pre = 'FIELD.TYPE_OPTG_'; $groups = [ Sobi::Txt( $pre . 'free_single_simple_data' ) => [], Sobi::Txt( $pre . 'predefined_multi_data_single_choice' ) => [], Sobi::Txt( $pre . 'predefined_multi_data_multi_choice' ) => [], Sobi::Txt( $pre . 'special' ) => [], ]; foreach ( $fTypes as $type ) { if ( $category ) { try { $class = SPLoader::loadClass( 'opt.fields.' . $type->tid ); if ( !( property_exists( $class, 'CAT_FIELD' ) ) ) { continue; } } catch ( SPException $x ) { continue; } } $groups[ str_replace( $pre, null, Sobi::Txt( $pre . $type->tGroup ) ) ][ $type->tid ] = $type->fType; } foreach ( $groups as &$group ) { asort( $group ); } return $groups; } return $groups; } /** * @TODO should be moved to the model ???? * Adding new field * Saves base data and redirects to the edit function when the field type has been chosen. * * @return mixed * @throws SPException */ public function saveNew() { /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); $this->getRequest(); return $field->saveNew( $this->attr ); } /** * Gets data from request. */ private function getRequest() { foreach ( $_REQUEST as $k => $v ) { if ( strstr( $k, 'field_' ) ) { $value = Input::Raw( $k ); //$value = SPRequest::raw( $k ); $this->attr[ str_replace( 'field_', null, $k ) ] = $value; } } } /** * @param int $id * * @return array|void * @throws SPException * @throws \Sobi\Error\Exception */ public function delete( $id = 0 ) { $fields = []; $m = []; if ( $id ) { $fields[] = $id; } else { if ( Input::Int( 'fid', 'request', 0 ) ) { $fields[] = Input::Int( 'fid', 'request', 0 ); } else { $fields = Input::Arr( 'p_fid', 'request', [] ); } } if ( count( $fields ) ) { foreach ( $fields as $id ) { /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); $field->extend( $this->loadField( $id ) ); $msg = $field->delete(); SPFactory::message()->setMessage( $msg, false, SPC::SUCCESS_MSG ); $m[] = $msg; } } else { $msg = SPLang::e( 'FMN.STATE_CHANGE_NO_ID' ); SPFactory::message()->setMessage( $msg, false, SPC::ERROR_MSG ); return; } return $m; } /** * @param $fid */ public function checkOut( $fid ) { } /** * @param $fid * * @return bool */ public function isCheckedOut( $fid ) { return false; } /** * @param $field * * @throws SPException */ protected function validateRequest( $field ) { $type = Input::Cmd( 'field_fieldType' ); $definition = SPLoader::path( 'field.' . $type, 'adm', true, 'xml' ); if ( $definition ) { $xdef = new DOMXPath( SPFactory::LoadXML( $definition ) ); $required = $xdef->query( '//field[@required="true"]' ); if ( $required->length ) { for ( $i = 0; $i < $required->length; $i++ ) { $node = $required->item( $i ); $name = $node->attributes->getNamedItem( 'name' )->nodeValue; if ( !( Input::Raw( str_replace( '.', '_', $name ) ) ) ) { $this->response( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $field->get( 'fid' ), 'sid' => Input::Sid() ] ), Sobi::Txt( 'PLEASE_FILL_IN_ALL_REQUIRED_FIELDS' ), false, 'error', [ 'required' => $name ] ); } } } } } /** * @param int $section * @param int $id * * @return array|void * @throws SPException * @throws \Sobi\Error\Exception */ protected function copyToSection( $section = 0, $id = 0 ) { $fields = []; $m = []; if ( $id ) { $fields[] = $id; } else { if ( Input::Int( 'fid', 'request', 0 ) ) { $fields[] = Input::Int( 'fid', 'request', 0 ); } else { $fields = Input::Arr( 'p_fid', 'request', [] ); } } if ( count( $fields ) ) { foreach ( $fields as $fid ) { /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); $attr = $this->loadField( $fid ); if ( $attr->adminField == -1 ) { Input::Set( 'category-field', 1 ); } foreach ( $attr as $k => $v ) { $this->attr[ $k ] = $v; } $field->extend( $this->attr ); // make the model specific $params = $field->get( 'params' ); // field specific properties foreach ( $params as $k => $v ) { $this->attr[ $k ] = $v; } if ( $section != 0 ) { // same section $field->set( 'section', $section ); } $nid = $field->get( 'nid' ); if ( !( $nid ) || !( strstr( $nid, 'field_' ) ) ) { $nid = strtolower( str_replace( '-', '_', SPLang::nid( 'field_' . Input::String( 'field_name' ) ) ) ); Input::Set( 'field_nid', $nid ); } try { $this->attr[ 'name' ] = $field->get( 'name' ); $this->attr[ 'suffix' ] = $field->get( 'suffix' ); $this->attr[ 'description' ] = $field->get( 'description' ); $this->attr[ 'section' ] = $section; if ( is_array( $field->get( 'options' ) ) ) { $this->attr [ 'options' ] = $field->get( 'options' ); } $fid = $field->saveNew( $this->attr ); if ( $section == 0 ) { $msg = Sobi::Txt( 'FM.FIELD_COPIED', [ 'field' => $field->get( 'name' ), 'fid' => $fid ] ); } else { $sectionName = SPLang::translateObject( $section, 'name', 'section' ); $sectionName = SPLang::clean( $sectionName[ $section ][ 'value' ] ); $msg = Sobi::Txt( 'FM.FIELD_COPIEDTO', [ 'field' => $field->get( 'name' ), 'fid' => $fid, 'section' => $sectionName ] ); } } catch ( SPException $x ) { $this->response( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $fid, 'sid' => Input::Sid() ] ), $x->getMessage(), false, SPC::ERROR_MSG ); } SPFactory::message() ->setMessage( $msg, false, SPC::SUCCESS_MSG ); $m[] = $msg; } } else { $msg = SPLang::e( 'FMN.STATE_CHANGE_NO_ID' ); SPFactory::message() ->setMessage( $msg, false, SPC::ERROR_MSG ); return; } return $m; } /** * Saves the existing field. * * @param bool $clone * @param bool $apply * * @throws SPException * @throws \Sobi\Error\Exception */ protected function save( $clone = false, $apply = false ) { $sets = []; if ( !( SPFactory::mainframe()->checkToken() ) ) { Sobi::Error( 'Token', SPLang::e( 'UNAUTHORIZED_ACCESS_TASK', Input::Task() ), SPC::ERROR, 403, __LINE__, __FILE__ ); } $fid = Input::Int( 'fid' ); /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); if ( $fid ) { $f = $this->loadField( $fid ); $field->extend( $f ); } else { $field->loadType( Input::Cmd( 'field_fieldType' ) ); } $nid = Input::Cmd( 'field_nid' ); if ( !( $nid ) || !( strstr( $nid, 'field_' ) ) ) { /** give me my spaces back!!! */ $nid = strtolower( str_replace( '-', '_', SPLang::nid( 'field_' . Input::String( 'field_name' ) ) ) ); Input::Set( 'field_nid', $nid ); } $this->getRequest(); $this->validateRequest( $field ); if ( $clone || !( $fid ) ) { try { $fid = $field->saveNew( $this->attr ); $field->save( $this->attr ); // save the changes!! } catch ( SPException $x ) { $this->response( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $fid, 'sid' => Input::Sid() ] ), $x->getMessage(), false, SPC::ERROR_MSG ); } } else { try { $field->save( $this->attr ); } catch ( SPException $x ) { $this->response( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $fid, 'sid' => Input::Sid() ] ), $x->getMessage(), false, SPC::ERROR_MSG ); } } $alias = $field->get( 'nid' ); $fieldSets = $field->get( 'sets' ); if ( is_array( $fieldSets ) && count( $fieldSets ) ) { $sets = array_merge( $fieldSets, $sets ); } $sets[ 'fid' ] = $field->get( 'fid' ); $sets[ 'field.nid' ] = $alias; /* in case we are changing the sort by field */ if ( Sobi::Cfg( 'list.entries_ordering' ) == $alias && $field->get( 'nid' ) != $alias ) { SPFactory::config()->saveCfg( 'list.entries_ordering', $field->get( 'nid' ) ); } SPFactory::cache()->cleanSection(); if ( $this->_task == 'apply' || $clone ) { if ( $clone ) { $msg = Sobi::Txt( 'FM.FIELD_CLONED' ); $this->response( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $fid, 'sid' => Input::Sid() ] ), $msg ); } else { $msg = Sobi::Txt( 'MSG.ALL_CHANGES_SAVED' ); $this->response( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $fid, 'sid' => Input::Sid() ] ), $msg, false, 'success', [ 'sets' => $sets ] ); } } else { $this->response( Sobi::Back(), Sobi::Txt( 'MSG.ALL_CHANGES_SAVED' ) ); } } /** * Lists all fields in this section. * * @throws SPException * @throws \Sobi\Error\Exception */ private function listFields() { /* @var SPdb $db */ $ord = $this->parseFieldsOrdering( 'forder', 'position.asc' ); SPLoader::loadClass( 'html.input' ); Sobi::ReturnPoint(); /* create menu */ $sid = Sobi::Reg( 'current_section' ); /** @var SPAdmSiteMenu $menu */ $menu = SPFactory::Instance( 'views.adm.menu', 'field.list', $sid ); $cfg = SPLoader::loadIniFile( 'etc.adm.section_menu' ); Sobi::Trigger( 'Create', 'AdmMenu', [ &$cfg ] ); if ( count( $cfg ) ) { foreach ( $cfg as $section => $keys ) { $menu->addSection( $section, $keys ); } } Sobi::Trigger( 'AfterCreate', 'AdmMenu', [ &$menu ] ); /* create new SigsiuTree */ $tree = SPLoader::loadClass( 'mlo.tree' ); $tree = new $tree( Sobi::GetUserState( 'categories.order', 'corder', 'position.asc' ) ); /* set link */ $tree->setHref( Sobi::Url( [ 'sid' => '{sid}' ] ) ); $tree->setId( 'menuTree' ); /* set the task to expand the tree */ $tree->setTask( 'category.expand' ); $tree->init( $sid ); /* add the tree into the menu */ $menu->addCustom( 'AMN.ENT_CAT', $tree->getTree() ); try { $results = SPFactory::db() ->select( '*', 'spdb_field', [ 'section' => $sid ], $ord ) ->loadObjectList(); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 0, __LINE__, __FILE__ ); } $fields = []; $categoryFields = []; if ( count( $results ) ) { foreach ( $results as $result ) { /** @var SPField $field */ $field = SPFactory::Model( 'field', true ); $field->extend( $result ); if ( $field->get( 'adminField' ) == -1 ) { $categoryFields[] = $field; } else { $fields[] = $field; } } } $fieldTypes = $this->getFieldTypes(); $subMenu = []; foreach ( $fieldTypes as $type => $group ) { asort( $group ); $subMenu[] = [ 'label' => $type, 'element' => 'nav-header', ]; foreach ( $group as $t => $l ) { $subMenu[] = [ 'type' => null, 'task' => 'field.add.' . $t, 'label' => $l, 'icon' => '', 'element' => 'button', ]; } } $categoryFieldsTypes = $this->getFieldTypes( true ); $cateSubMenu = []; foreach ( $categoryFieldsTypes as $type => $group ) { asort( $group ); $cateSubMenu[] = [ 'label' => $type, 'element' => 'nav-header', ]; foreach ( $group as $t => $l ) { $cateSubMenu[] = [ 'type' => null, 'task' => 'field.add.' . $t . '.category', 'label' => $l, 'icon' => '', 'element' => 'button', ]; } } $sectionName = Sobi::Section( true ); $fieldsOrder = Sobi::GetUserState( 'fields.order', 'forder', 'position.asc' ); SPFactory::View( 'field', true ) ->addHidden( $sid, 'sid' ) ->assign( $fields, 'fields' ) ->assign( $categoryFields, 'category-fields' ) ->assign( $cateSubMenu, 'categoryFieldTypes' ) ->assign( $subMenu, 'fieldTypes' ) ->assign( $sectionName, 'section' ) ->assign( $menu, 'menu' ) ->assign( $fieldsOrder, 'ordering' ) ->assign( $this->_task, 'task' ) ->determineTemplate( 'field', 'list' ) ->display(); } /** * @param $col * @param $def * * @return array|mixed|string * @throws SPException */ protected function parseFieldsOrdering( $col, $def ) { $order = Sobi::GetUserState( 'fields.order', $col, Sobi::Cfg( 'admin.fields-order', $def ) ); $ord = $order; $dir = 'asc'; /** legacy - why the hell I called it order?! */ $ord = str_replace( 'order', 'position', $ord ); if ( strstr( $ord, '.' ) ) { $ord = explode( '.', $ord ); if ( count( $ord ) == 3 ) { $dir = $ord[ 1 ] . '.' . $ord[ 2 ]; } else { $dir = $ord[ 1 ]; } $ord = $ord[ 0 ]; } $ord = ( $ord == 'state' ) ? 'enabled' : $ord; // $ord = ( $ord == 'position' ) ? 'position' : $ord; if ( $ord == 'name' ) { /* @var SPdb $db */ $db = SPFactory::db(); $fields = $db ->select( 'fid', 'spdb_language', [ 'oType' => 'field', 'sKey' => 'name', 'language' => Sobi::Lang() ], 'sValue.' . $dir ) ->loadResultArray(); if ( !count( $fields ) && Sobi::Lang() != Sobi::DefLang() ) { $fields = $db ->select( 'id', 'spdb_language', [ 'oType' => 'field', 'sKey' => 'name', 'language' => Sobi::DefLang() ], 'sValue.' . $dir ) ->loadResultArray(); } if ( count( $fields ) ) { $fields = implode( ',', $fields ); $ord = "field( fid, {$fields} )"; } else { $ord = 'fid.' . $dir; } } else { $ord = $ord . '.' . $dir; } Sobi::SetUserState( 'fields.order', $order ); return $ord; } /** * @throws SPException * @throws \Sobi\Error\Exception */ private function reorder() { $this->_reorder( Input::Arr( 'fid', 'request', [] ) ); $this->_reorder( Input::Arr( 'cfid', 'request', [] ) ); SPFactory::cache()->cleanSection(); $this->response( Sobi::Url( [ 'task' => 'field.list', 'pid' => Sobi::Section() ] ), Sobi::Txt( 'NEW_FIELDS_ORDERING_HAS_BEEN_SAVED' ), true, SPC::SUCCESS_MSG ); } /** * @param $up * * @throws SPException * @throws \Sobi\Error\Exception */ private function singleReorder( $up ) { $up = ( bool ) $up; /* @var SPdb $db */ $db = SPFactory::db(); $fid = Input::Int( 'fid' ); $category = false; if ( !$fid ) { $fid = Input::Int( 'cfid' ); $category = true; } $fClass = SPLoader::loadModel( 'field', true ); $fdata = $this->loadField( $fid ); $field = new $fClass(); $field->extend( $fdata ); $eq = $up ? '<' : '>'; $dir = $up ? 'position.desc' : 'position.asc'; $current = $field->get( 'position' ); try { $condition = [ 'position' . $eq => $current, 'section' => Input::Int( 'sid' ) ]; if ( !( $category ) ) { $condition[ 'adminField>' ] = -1; } else { $condition[ 'adminField' ] = -1; } $interchange = $db ->select( 'position, fid', 'spdb_field', $condition, $dir, 1 ) ->loadAssocList(); if ( $interchange && count( $interchange ) ) { $db->update( 'spdb_field', [ 'position' => $interchange[ 0 ][ 'position' ] ], [ 'section' => Input::Int( 'sid' ), 'fid' => $field->get( 'fid' ) ], 1 ); $db->update( 'spdb_field', [ 'position' => $current ], [ 'section' => Input::Int( 'sid' ), 'fid' => $interchange[ 0 ][ 'fid' ] ], 1 ); } else { $current = $up ? $current-- : $current++; $db->update( 'spdb_field', [ 'position' => $current ], [ 'section' => Input::Int( 'sid' ), 'fid' => $field->get( 'fid' ) ], 1 ); } } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); } SPFactory::cache()->cleanSection(); $this->response( Sobi::Url( [ 'task' => 'field.list', 'pid' => Sobi::Section() ] ), Sobi::Txt( 'NEW_FIELDS_ORDERING_HAS_BEEN_SAVED' ), true, SPC::SUCCESS_MSG ); } /** * @param $task * * @return array|string|null * @throws SPException * @throws \Sobi\Error\Exception */ protected function changeState( $task ) { $fIds = Input::Arr( 'p_fid' ); $col = 'enabled'; $state = '0'; $msg = null; if ( !$fIds ) { if ( Input::Int( 'fid' ) ) { $fIds = [ Input::Int( 'fid' ) ]; } else { $fIds = []; } } if ( !( count( $fIds ) ) ) { return [ 'text' => Sobi::Txt( 'FMN.STATE_CHANGE_NO_ID' ), 'type' => SPC::ERROR_MSG ]; } switch ( $task ) { case 'hide': case 'publish': $col = 'enabled'; $state = ( $task == 'publish' ) ? 1 : 0; break; case 'setRequired': case 'setNotRequired': $col = 'required'; $state = ( $task == 'setRequired' ) ? 1 : 0; break; case 'setEditable': case 'setNotEditable': $col = 'editable'; $state = ( $task == 'setEditable' ) ? 1 : 0; break; case 'setFee': case 'setFree': $col = 'isFree'; $state = ( $task == 'setFree' ) ? 1 : 0; break; /** @since 1.1 - single row only from the field list */ case 'toggle': $fIds = []; $fid = Input::Int( 'fid' ); $attribute = explode( '.', Input::Task() ); /** now you know what a naming convention is for! Right? Damn!!! */ $attribute = in_array( $attribute[ 2 ], [ 'editable', 'enabled', 'required' ] ) ? $attribute[ 2 ] : 'is' . ucfirst( $attribute[ 2 ] ); $this->_model = SPFactory::Model( 'field', true ) ->init( $fid ); $current = $this->_model->get( $attribute ); try { SPFactory::db()->update( 'spdb_field', [ $attribute => !( $current ) ], [ 'fid' => $fid ], 1 ); $msg = Sobi::Txt( 'FM.STATE_CHANGED', [ 'fid' => $fid ] ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); $msg = Sobi::Txt( 'FM.STATE_NOT_CHANGED', [ 'fid' => $fid ] ); } break; } if ( count( $fIds ) ) { $msg = []; foreach ( $fIds as $fid ) { try { SPFactory::db() ->update( 'spdb_field', [ $col => $state ], [ 'fid' => $fid ], 1 ); $msg[] = [ 'text' => Sobi::Txt( 'FM.STATE_CHANGED', [ 'fid' => $fid ] ), 'type' => 'success' ]; } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); $msg[] = [ 'text' => Sobi::Txt( 'FM.STATE_NOT_CHANGED', [ 'fid' => $fid ] ), 'type' => 'error' ]; } } } SPFactory::cache()->cleanSection( Sobi::Section() ); return $msg; } /** * Route task. * * @return bool * @throws SPException * @throws \Sobi\Error\Exception */ public function execute() { /* parent class executes the plugins */ $r = false; $task = $this->_task; if ( strstr( $this->_task, '.' ) ) { $task = explode( '.', $this->_task ); if ( !( is_numeric( $task[ 1 ] ) ) ) { $this->_fieldType = $task[ 1 ]; } if ( isset( $task[ 2 ] ) && $task[ 2 ] == 'category' ) { $this->_category = true; } if ( isset ( $task[ 1 ] ) && is_numeric( $task[ 1 ] ) ) { $section = $task[ 1 ]; } $task = $task[ 0 ]; } switch ( $task ) { case 'list': $r = true; $this->listFields(); break; case 'add': case 'edit': $r = true; $this->edit(); break; case 'cancel': $r = true; $this->response( Sobi::Back() ); break; case 'addNew': $r = true; Sobi::Redirect( Sobi::Url( [ 'task' => 'field.edit', 'fid' => $this->saveNew(), 'sid' => Input::Sid() ] ) ); break; case 'apply': case 'save': $r = true; $this->save(); break; case 'clone': $r = true; $this->save( true ); break; case 'copyto': $r = true; $this->response( Sobi::Back(), $this->copyToSection( $section ), true ); break; case 'copy': $r = true; $this->response( Sobi::Back(), $this->copyToSection( 0 ), true ); break; case 'delete': $r = true; SPFactory::cache()->cleanSection(); $this->response( Sobi::Url( [ 'task' => 'field.list', 'pid' => Sobi::Section() ] ), $this->delete(), true ); break; case 'reorder': $r = true; $this->reorder(); break; case 'revisions': $r = true; $this->revisions(); //??? break; case 'up': case 'down': $r = true; $this->singleReorder( $this->_task == 'up' ); break; case 'hide': case 'publish': case 'setRequired': case 'setNotRequired': case 'setEditable': case 'setNotEditable': case 'setFee': case 'setFree': case 'toggle': $r = true; $this->_type = 'section'; $this->authorise( 'configure' ); SPFactory::cache()->cleanSection(); $this->response( Sobi::Back(), $this->changeState( $task ), true ); break; default: /* case plugin didn't register this task, it was an error */ if ( !( Sobi::Trigger( 'Execute', $this->name(), [ &$this ] ) ) ) { $fid = Input::Int( 'fid' ); $method = $this->_task; if ( $fid ) { SPLoader::loadModel( 'field', true ); $fdata = $this->loadField( $fid ); $field = new SPAdmField(); $field->extend( $fdata ); try { $field->$method(); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'SUCH_TASK_NOT_FOUND', Input::Task() ), SPC::NOTICE, 404, __LINE__, __FILE__ ); } } else { if ( !( parent::execute() ) ) { Sobi::Error( $this->name(), SPLang::e( 'SUCH_TASK_NOT_FOUND', Input::Task() ), SPC::NOTICE, 404, __LINE__, __FILE__ ); } } } break; } return $r; } /** * @param $fIds * * @return bool * @throws SPException * @throws \Sobi\Error\Exception */ private function _reorder( $fIds ) { asort( $fIds ); $c = 0; if ( !( count( $fIds ) ) ) { return true; } foreach ( $fIds as $fid => $pos ) { $c++; try { SPFactory::db()->update( 'spdb_field', [ 'position' => $c ], [ 'fid' => $fid ] ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 500, __LINE__, __FILE__ ); } } } }