Current Path :
/
home
/
develito
/
www
/
components
/
com_sobipro
/
opt
/
fields
/
adm
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/develito/www/components/com_sobipro/opt/fields/adm/multiselect.php
<?php /** * @package: SobiPro Component for Joomla! * * @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/GPL Version 3 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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/gpl.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 General Public License for more details. * * @created 26-Nov-2009 by Radek Suski * @modified 24 June 2020 by Sigrid Suski */ defined( 'SOBIPRO' ) || exit( 'Restricted access' ); use \Sobi\Input\Input; SPLoader::loadClass( 'opt.fields.multiselect' ); /** * Class SPField_MultiSelectAdm */ class SPField_MultiSelectAdm extends SPField_MultiSelect { /** * @param $attr * * @throws SPException * @throws \Sobi\Error\Exception */ public function save( &$attr ) { static $lang = null; static $defLang = null; if ( !( $lang ) ) { $lang = Sobi::Lang(); $defLang = Sobi::DefLang(); } $file = Input::File( 'spfieldsopts', 'tmp_name' ); if ( $file ) { $data = parse_ini_file( $file, true ); } else if ( is_string( $attr[ 'options' ] ) ) { $data = parse_ini_string( $attr[ 'options' ], true ); } else { $data = null; } $options = $this->parseOptsFile( $data ); if ( count( $options ) ) { $p = 0; // $newdata = []; foreach ( $options as $o ) { if ( is_numeric( $o[ 'id' ] ) ) { $o[ 'id' ] = $this->nid . '_' . $o[ 'id' ]; } if ( isset( $o[ 'id' ] ) ) { $newoptions[] = [ 'id' => $o[ 'id' ], 'name' => $o[ 'name' ], 'parent' => $o[ 'parent' ], 'position' => ++$p ]; // $newdata[ $o[ 'id' ] ] = $o[ 'name' ]; } else if ( isset( $o[ 'gid' ] ) ) { $newoptions[] = [ 'id' => $o[ 'gid' ], 'name' => $o[ 'name' ], 'parent' => null, 'position' => ++$p ]; if ( count( $o ) ) { $gid = $o[ 'gid' ]; unset( $o[ 'gid' ] ); unset( $o[ 'name' ] ); $index = 0; foreach ( $o as $so ) { if ( is_numeric( $so[ 'id' ] ) ) { $so[ 'id' ] = $this->nid . '_' . $so[ 'id' ]; } $newoptions[] = [ 'id' => $so[ 'id' ], 'name' => $so[ 'name' ], 'parent' => $gid, 'position' => ++$index ]; // $newdata[ $so[ 'id' ] ] = $so[ 'name' ]; } } } } $options = $newoptions; //$data = $newdata; } $isgrouped = false; if ( count( $options ) ) { unset( $attr[ 'options' ] ); $optionsArr = []; $labelsArr = []; $optsIds = []; $defLabelsArr = []; $duplicates = false; foreach ( $options as $i => $option ) { /* check for doubles */ foreach ( $options as $pos => $opt ) { if ( $i == $pos ) { continue; } if ( $option[ 'id' ] == $opt[ 'id' ] ) { $option[ 'id' ] = $option[ 'id' ] . '_' . substr( ( string ) microtime(), 2, 8 ) . rand( 1, 100 ); $duplicates = true; } } $optionsArr[] = [ 'fid' => $this->id, 'optValue' => $option[ 'id' ], 'optPos' => $option[ 'position' ], 'optParent' => $option[ 'parent' ] ]; $defLabelsArr[] = [ 'sKey' => $option[ 'id' ], 'sValue' => $option[ 'name' ], 'language' => $defLang, 'oType' => 'field_option', 'fid' => $this->id ]; $labelsArr[] = [ 'sKey' => $option[ 'id' ], 'sValue' => $option[ 'name' ], 'language' => $lang, 'oType' => 'field_option', 'fid' => $this->id ]; $optsIds[] = $option[ 'id' ]; /* need to know if there are groups */ $isgrouped = ( !( is_null( $option[ 'parent' ] ) ) ) ? true : $isgrouped; } if ( $duplicates ) { SPFactory::message()->warning( 'FIELD_WARN_DUPLICATE_OPT_ID' ); } $db = &SPFactory::db(); /* try to delete the existing labels */ try { $db->delete( 'spdb_field_option', [ 'fid' => $this->id ] ); $db->delete( 'spdb_language', [ 'oType' => 'field_option', 'fid' => $this->id, '!sKey' => $optsIds ] ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_DELETE_SELECTED_OPTIONS', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } /* insert new values */ try { $db->insertArray( 'spdb_field_option', $optionsArr ); $db->insertArray( 'spdb_language', $labelsArr, true ); if ( $defLang != $lang ) { $db->insertArray( 'spdb_language', $defLabelsArr, false, true ); } } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_STORE_FIELD_OPTIONS_DB_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } } if ( !isset( $attr[ 'params' ] ) ) { $attr[ 'params' ] = []; } $myAttr = $this->getAttr(); $properties = []; if ( count( $myAttr ) ) { foreach ( $myAttr as $property ) { $properties[ $property ] = isset( $attr[ $property ] ) ? ( $attr[ $property ] ) : null; } } $attr[ 'params' ] = $properties; $opt = []; foreach ( $options as $option ) { if ( $option[ 'parent' ] ) { if ( !( isset( $opt[ $option[ 'parent' ] ] ) ) ) { $opt[ $option[ 'parent' ] ] = []; } $opt[ $option[ 'parent' ] ][ $option[ 'id' ] ] = $option[ 'name' ]; } else { if ( !( isset( $opt[ $option[ 'id' ] ] ) ) ) { /* if the data are grouped, all data without a parent are a group name (nested groups not possible) */ if ( $isgrouped ) { $opt[ $option[ 'id' ] ] = []; } else { $opt[ $option[ 'id' ] ] = $option[ 'name' ]; } } } } $this->sets[ 'field.options' ] = SPFactory::Instance( 'types.array' ) ->toINIString( $opt ); $this->saveSelectLabel( $attr ); } /** * @param $attr * @param $fid * * @throws SPException * @throws \Sobi\Error\Exception */ public function saveNew( &$attr, $fid ) { static $lang = null; static $defLang = null; if ( !( $lang ) ) { $lang = Sobi::Lang(); $defLang = Sobi::DefLang(); } if ( is_array( $attr[ 'options' ] ) ) { $options = $attr[ 'options' ]; $values = [ 'optionsArray' => [], 'labelsArray' => [], 'defaultLabelsArray' => [] ]; if ( count( $options ) ) { $this->generateOptionsToSave( $options, $values, $fid, $lang, $defLang ); /* insert new values */ $db = &SPFactory::db(); try { $db->insertArray( 'spdb_field_option', $values[ 'optionsArray' ] ); $db->insertArray( 'spdb_language', $values[ 'labelsArray' ], true ); if ( $defLang != $lang ) { $db->insertArray( 'spdb_language', $values[ 'defaultLabelsArray' ], false, true ); } } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'CANNOT_STORE_FIELD_OPTIONS_DB_ERR', $x->getMessage() ), SPC::ERROR, 500, __LINE__, __FILE__ ); } } } $this->saveSelectLabel( $attr ); } }