Current Path :
/
home
/
develito
/
httpdocs
/
components
/
com_sobipro
/
lib
/
models
/
adm
/
Or
Select Your Path :
Upload File :
New :
File
Dir
//home/develito/httpdocs/components/com_sobipro/lib/models/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 09-Mar-2009 by Radek Suski * @modified 01 December 2020 by Sigrid Suski */ defined( 'SOBIPRO' ) || exit( 'Restricted access' ); use Sobi\Input\Input; SPLoader::loadModel( 'field' ); /** * Class SPAdmField */ final class SPAdmField extends SPField { /** @var array */ private $_translatable = [ 'name', 'description' ]; /** * @param $base * * @TODO: this method is idiotic. It has to be automated Tue, Oct 13, 2020 14:36:09 * @throws SPException */ private function checkAttr( &$base ) { $db = \Sobi\Database\MySQLi::getInstance(); if ( isset( $base[ 'name' ] ) ) { $base[ 'name' ] = $db->escape( $base[ 'name' ] ); } else { $base[ 'name' ] = 'missing name - something went wrong'; } if ( isset( $base[ 'description' ] ) ) { $base[ 'description' ] = $db->escape( $base[ 'description' ] ); } else { $base[ 'description' ] = null; } if ( isset( $base[ 'suffix' ] ) ) { $base[ 'suffix' ] = $db->escape( $base[ 'suffix' ] ); } else { $base[ 'suffix' ] = null; } if ( isset( $base[ 'cssClass' ] ) ) { $base[ 'cssClass' ] = $db->escape( preg_replace( '/[^[:alnum:]\-\_ ]/', null, $base[ 'cssClass' ] ) ); } if ( isset( $base[ 'notice' ] ) ) { $base[ 'notice' ] = $db->escape( $base[ 'notice' ] ); } if ( isset( $base[ 'showIn' ] ) ) { $base[ 'showIn' ] = $db->escape( preg_replace( '/[^[:alnum:]\.\-\_]/', null, $base[ 'showIn' ] ) ); } if ( isset( $base[ 'filter' ] ) ) { $base[ 'filter' ] = preg_replace( '/[^[:alnum:]\.\-\_]/', null, $base[ 'filter' ] ); } if ( isset( $base[ 'fieldType' ] ) ) { $base[ 'fieldType' ] = preg_replace( '/[^[:alnum:]\.\-\_]/', null, $base[ 'fieldType' ] ); } if ( isset( $base[ 'type' ] ) ) { $base[ 'fieldType' ] = preg_replace( '/[^[:alnum:]\.\-\_]/', null, $base[ 'type' ] ); } if ( isset( $base[ 'enabled' ] ) ) { $base[ 'enabled' ] = ( int ) $base[ 'enabled' ]; } if ( isset( $base[ 'required' ] ) ) { $base[ 'required' ] = ( int ) $base[ 'required' ]; } if ( isset( $base[ 'showLabel' ] ) ) { $base[ 'showLabel' ] = ( int ) $base[ 'showLabel' ]; } if ( isset( $base[ 'showEditLabel' ] ) ) { $base[ 'showEditLabel' ] = ( int ) $base[ 'showEditLabel' ]; } if ( isset( $base[ 'adminField' ] ) ) { $base[ 'adminField' ] = ( int ) $base[ 'adminField' ]; } if ( isset( $base[ 'adminField' ] ) && $base[ 'adminField' ] ) { $base[ 'required' ] = false; } if ( isset( $base[ 'editable' ] ) ) { $base[ 'editable' ] = ( int ) $base[ 'editable' ]; } if ( isset( $base[ 'inSearch' ] ) ) { $base[ 'inSearch' ] = ( int ) $base[ 'inSearch' ]; } $base[ 'editLimit' ] = isset( $base[ 'editLimit' ] ) && $base[ 'editLimit' ] > 0 ? $base[ 'editLimit' ] : -1; if ( isset( $base[ 'editLimit' ] ) ) { $base[ 'editLimit' ] = ( int ) $base[ 'editLimit' ]; } if ( isset( $base[ 'isFree' ] ) ) { $base[ 'isFree' ] = ( int ) $base[ 'isFree' ]; } if ( isset( $base[ 'withLabel' ] ) ) { $base[ 'withLabel' ] = ( int ) $base[ 'withLabel' ]; } if ( isset( $base[ 'admList' ] ) ) { $base[ 'admList' ] = ( int ) $base[ 'admList' ]; } if ( isset( $base[ 'fee' ] ) ) { $base[ 'fee' ] = ( double ) str_replace( ',', '.', $base[ 'fee' ] ); } if ( isset( $base[ 'uniqueData' ] ) ) { $base[ 'uniqueData' ] = ( int ) $base[ 'uniqueData' ]; } if ( isset( $base[ 'addToMetaDesc' ] ) ) { $base[ 'addToMetaDesc' ] = ( int ) $base[ 'addToMetaDesc' ]; } if ( isset( $base[ 'addToMetaKeys' ] ) ) { $base[ 'addToMetaKeys' ] = ( int ) $base[ 'addToMetaKeys' ]; } /** * @TODO See method's header */ $base[ 'adminField' ] = $base[ 'adminField' ] ?? 0; $base[ 'adminList' ] = $base[ 'adminList' ] ?? 0; $base[ 'admList' ] = $base[ 'admList' ] ?? 0; $base[ 'uniqueData' ] = $base[ 'uniqueData' ] ?? 0; $base[ 'addToMetaDesc' ] = $base[ 'addToMetaDesc' ] ?? 0; $base[ 'withLabel' ] = $base[ 'withLabel' ] ?? 0; /* both strpos are removed because it does not allow to have one parameter only */ // if( isset( $base[ 'allowedAttributes' ] ) && strpos( $base[ 'allowedAttributes' ], '|' ) ) if ( isset( $base[ 'allowedAttributes' ] ) ) { $att = SPFactory::config()->structuralData( $base[ 'allowedAttributes' ], true ); if ( is_array( $att ) && count( $att ) ) { foreach ( $att as $i => $k ) { $att[ $i ] = trim( $k ); } } $base[ 'allowedAttributes' ] = SPConfig::serialize( $att ); } if ( isset( $base[ 'allowedTags' ] ) ) { $tags = SPFactory::config()->structuralData( $base[ 'allowedTags' ], true ); if ( is_array( $tags ) && count( $tags ) ) { foreach ( $tags as $i => $k ) { $tags[ $i ] = trim( $k ); } } $base[ 'allowedTags' ] = SPConfig::serialize( $tags ); } /* bind attributes to this object */ foreach ( $base as $a => $v ) { $a = trim( $a ); if ( $this->has( $a ) ) { $this->$a = $v; } } } /** * @param $attr * * @throws SPException * @throws \Sobi\Error\Exception */ public function save( $attr ) { $db = \Sobi\Database\MySQLi::getInstance(); $base = $attr; $this->loadType(); $base[ 'section' ] = ( isset( $base[ 'section' ] ) && $base[ 'section' ] ) ? $base[ 'section' ] : Input::Sid(); // if ( !( ( isset( $base[ 'section' ] ) && $base[ 'section' ] ) ) ) { // if ( !( Input::Int( 'fid' ) ) ) { // $base[ 'section' ] = Input::Sid(); // } // } $this->version++; $base[ 'version' ] = $this->version; if ( isset( $base[ 'nid' ] ) ) { $base[ 'nid' ] = $this->nid( $db->escape( preg_replace( '/[^[:alnum:]\-\_]/', null, $base[ 'nid' ] ) ), false, $base[ 'section' ] ); } $this->checkAttr( $base ); $baseIndexes = array_keys( $base ); $attrIndexes = array_keys( $attr ); $additionalInfo = array_diff( $attrIndexes, $baseIndexes ); if ( $this->_type && method_exists( $this->_type, 'save' ) ) { $this->_type->save( $base, $additionalInfo ); } /* get database columns and their ordering */ $cols = $db->getColumns( 'spdb_field' ); $values = []; /* and sort the properties in the same order */ foreach ( $cols as $col ) { if ( array_key_exists( $col, $base ) ) { $values[ $col ] = $base[ $col ]; } } /* save field */ try { $db->update( 'spdb_field', $values, [ 'fid' => $this->fid ] ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } /* save language depend properties */ $labels = []; $defLabels = []; $labels[] = [ 'sKey' => 'name', 'sValue' => $base[ 'name' ], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $labels[] = [ 'sKey' => 'description', 'sValue' => $base[ 'description' ], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $labels[] = [ 'sKey' => 'suffix', 'sValue' => $base[ 'suffix' ], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; if ( Sobi::Lang() != Sobi::DefLang() ) { $defLabels[] = [ 'sKey' => 'name', 'sValue' => $base[ 'name' ], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $defLabels[] = [ 'sKey' => 'suffix', 'sValue' => $base[ 'suffix' ], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $defLabels[] = [ 'sKey' => 'description', 'sValue' => $base[ 'description' ], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; } if ( is_array( $labels ) && count( $labels ) ) { try { if ( Sobi::Lang() != Sobi::DefLang() ) { $db->insertArray( 'spdb_language', $defLabels, false, true ); } $db->insertArray( 'spdb_language', $labels, true ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_SAVE_FIELD_DB_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } } SPFactory::cache()->cleanSection(); } /** * Add new field - Saves base data. * * @param $attr * * @return int * @throws SPException * @throws \Sobi\Error\Exception */ public function saveNew( $attr ) { $db = \Sobi\Database\MySQLi::getInstance(); /* cast all needed data and clean - it is possible just in admin panel but "strzeżonego pan Bóg strzeże" ;-) */ $base = $attr; unset ( $base[ 'fid' ] ); $this->loadType( $base[ 'type' ] ); $base[ 'section' ] = ( isset( $base[ 'section' ] ) && $base[ 'section' ] ) ? $base[ 'section' ] : Input::Sid(); $base[ 'version' ] = 1; if ( isset( $base[ 'nid' ] ) ) { $base[ 'nid' ] = $this->nid( $db->escape( preg_replace( '/[^[:alnum:]\-\_]/', null, $base[ 'nid' ] ) ), true, $base[ 'section' ] ); } $this->checkAttr( $base ); /* determine the right position */ try { $condition = [ 'section' => Input::Sid() ]; if ( !( Input::Int( 'category-field' ) ) ) { $condition[ 'adminField>' ] = -1; } $base[ 'position' ] = ( int ) $db->select( 'MAX( position )', 'spdb_field', $condition ) ->loadResult() + 1; if ( !$base[ 'position' ] ) { $base[ 'position' ] = 1; } } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_GET_FIELD_POSITION_DB_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } /* get database columns and their ordering */ $cols = $db->getColumns( 'spdb_field' ); $values = []; /* and sort the properties in the same order */ foreach ( $cols as $col ) { $values[ $col ] = array_key_exists( $col, $base ) ? $base[ $col ] : ''; } /* save new field */ try { $db->insert( 'spdb_field', $values ); $this->fid = $db->insertid(); } catch ( SPException $x ) { Sobi::Error( $this->name(), $x->getMessage(), SPC::ERROR, 500, __LINE__, __FILE__ ); } // save option values if available if ( $this->_type && method_exists( $this->_type, 'saveNew' ) ) { $this->_type->saveNew( $base, $this->fid ); } /* save language depend properties */ $labels = []; $defLabels = []; $labels[] = [ 'sKey' => 'name', 'sValue' => $base[ 'name' ], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $labels[] = [ 'sKey' => 'description', 'sValue' => $base[ 'description' ], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $labels[] = [ 'sKey' => 'suffix', 'sValue' => $base[ 'suffix' ], 'language' => Sobi::Lang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; if ( Sobi::Lang() != Sobi::DefLang() ) { $defLabels[] = [ 'sKey' => 'name', 'sValue' => $base[ 'name' ], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $defLabels[] = [ 'sKey' => 'suffix', 'sValue' => $base[ 'suffix' ], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; $defLabels[] = [ 'sKey' => 'description', 'sValue' => $base[ 'description' ], 'language' => Sobi::DefLang(), 'id' => 0, 'oType' => 'field', 'fid' => $this->fid ]; } if ( is_array( $labels ) && count( $labels ) ) { try { if ( Sobi::Lang() != Sobi::DefLang() ) { $db->insertArray( 'spdb_language', $defLabels, false, true ); } $db->insertArray( 'spdb_language', $labels, true ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_SAVE_FIELD_DB_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } } SPFactory::cache()->cleanSection(); return $this->fid; } /** * @param $nid * @param $new * * @return string * @throws \Sobi\Error\Exception */ private function nid( $nid, $new, $section ) { $c = 1; $a = 2; $suffix = null; while ( $c ) { /* field alias has to be unique */ try { $condition = [ 'nid' => $nid . $suffix, 'section' => $section ]; if ( !( $new ) ) { $condition[ '!fid' ] = $this->id; } $c = \Sobi\Database\MySQLi::getInstance() ->select( 'COUNT( nid )', 'spdb_field', $condition ) ->loadResult(); if ( $c > 0 ) { $suffix = '_' . $a++; } } catch ( SPException $x ) { } } return $nid . $suffix; } /** * @param null $type * * @throws SPException */ public function loadType( $type = null ) { if ( $type ) { $this->type = $type; } else { $this->type =& $this->fieldType; } if ( $this->type && SPLoader::translatePath( 'opt.fields.adm.' . $this->type ) ) { SPLoader::loadClass( 'opt.fields.fieldtype' ); $fType = SPLoader::loadClass( 'opt.fields.adm.' . $this->type ); $this->_type = new $fType( $this ); } else { if ( $this->type && SPLoader::translatePath( 'opt.fields.' . $this->type ) ) { SPLoader::loadClass( 'opt.fields.fieldtype' ); $fType = SPLoader::loadClass( 'opt.fields.' . $this->type ); $this->_type = new $fType( $this ); } else { parent::loadType(); } } } /** * @param $view * * @throws SPException */ public function onFieldEdit( &$view ) { $this->loadType(); $this->editLimit = $this->editLimit > 0 ? $this->editLimit : 0; $this->fee = SPLang::currency( $this->fee, false ); if ( is_array( $this->allowedAttributes ) && !( is_string( $this->allowedAttributes ) ) ) { $this->allowedAttributes = implode( ', ', $this->allowedAttributes ); } if ( is_array( $this->allowedTags ) && !( is_string( $this->allowedTags ) ) ) { $this->allowedTags = implode( ', ', $this->allowedTags ); } if ( $this->_type && method_exists( $this->_type, 'onFieldEdit' ) ) { $this->_type->onFieldEdit( $view ); } } }