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/select.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 19-Nov-2009 by Radek Suski * @modified 24 June 2020 by Sigrid Suski */ defined( 'SOBIPRO' ) || exit( 'Restricted access' ); use \Sobi\Input\Input; use Sobi\FileSystem\Archive; use Sobi\FileSystem\FileSystem; SPLoader::loadClass( 'opt.fields.select' ); /** * Class SPField_SelectAdm */ class SPField_SelectAdm extends SPField_Select { /** * @param $attr * * @throws SPException * @throws \Sobi\Error\Exception */ public function save( &$attr ) { static $lang = null; static $defLang = null; $options = null; if ( array_key_exists( 'mselect', $attr ) && $attr[ 'searchMethod' ] == 'mselect' && array_key_exists( 'dependency', $attr ) && $attr[ 'dependency' ] ) { throw new SPException( SPLang::e( 'SELECT_FIELD_MULTIPLE_DEPENDENCY' ) ); } 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 { if ( is_array( $attr[ 'options' ] ) ) { $options = $attr[ 'options' ]; } else { $data = null; } } } if ( !( $options ) ) { $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' ]; } } $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; } } $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 ); /** handle upload of new definition file */ $XMLFile = Input::File( 'select-list-dependency', 'tmp_name' ); if ( $XMLFile && file_exists( $XMLFile ) ) { $XMLFileName = Input::File( 'select-list-dependency', 'name' ); if ( FileSystem::GetExt( $XMLFileName ) == 'zip' ) { // $arch = SPFactory::Instance( 'base.fs.archive' ); $arch = new Archive(); $name = str_replace( '.zip', null, $XMLFileName ); $path = SPLoader::dirPath( 'tmp.install.' . $name, 'front', false ); $c = 0; while ( FileSystem::Exists( $path ) ) { $path = SPLoader::dirPath( 'tmp.install.' . $name . '_' . ++$c, 'front', false ); } $arch->upload( $XMLFile, $path . '/' . $XMLFileName ); $arch->extract( $path ); $files = scandir( $path ); if ( count( $files ) ) { foreach ( $files as $defFile ) { switch ( FileSystem::GetExt( $defFile ) ) { case 'xml': $properties[ 'dependencyDefinition' ] = $defFile; FileSystem::Move( $path . '/' . $defFile, SOBI_PATH . '/etc/fields/select-list/' . $defFile ); break; case 'ini': $defLang = explode( '.', $defFile ); $defLang = $defLang[ 0 ]; if ( file_exists( SOBI_ROOT . '/language/' . $defLang ) ) { FileSystem::Move( $path . '/' . $defFile, SOBI_ROOT . '/language/' . $defLang . '/' . $defFile ); } break; } } } } else { if ( FileSystem::GetExt( $XMLFileName ) == 'xml' ) { if ( FileSystem::Upload( $XMLFile, SOBI_PATH . '/etc/fields/select-list/' . $XMLFileName ) ) { $properties[ 'dependencyDefinition' ] = $XMLFileName; } } } } /** if we use it - let's transform the XML file */ if ( $properties[ 'dependency' ] && $properties[ 'dependencyDefinition' ] ) { $this->parseDependencyDefinition( $properties[ 'dependencyDefinition' ] ); } $attr[ 'params' ] = $properties; $this->saveSelectLabel( $attr ); } /** * @param $file * * @throws \Sobi\Error\Exception */ protected function parseDependencyDefinition( $file ) { $dom = new DOMDocument(); $dom->load( SOBI_PATH . '/etc/fields/select-list/' . $file ); $xpath = new DOMXPath( $dom ); $definition = []; $root = $xpath->query( '/definition' ); $definition[ 'prefix' ] = $root->item( 0 )->attributes->getNamedItem( 'prefix' )->nodeValue; $definition[ 'translation' ] = $root->item( 0 )->attributes->getNamedItem( 'translation' )->nodeValue; $definition[ 'options' ] = []; $this->_parseXML( $xpath->query( '/definition/option' ), $definition[ 'options' ] ); FileSystem::Write( SOBI_PATH . '/etc/fields/select-list/definitions/' . ( str_replace( '.xml', '.json', $file ) ), json_encode( $definition ) ); } /** * @param DOMNodeList $nodes * @param $definition */ protected function _parseXML( DOMNodeList $nodes, &$definition ) { foreach ( $nodes as $node ) { if ( !( $node->attributes ) ) { continue; } $option = [ 'id' => $node->attributes->getNamedItem( 'id' )->nodeValue, 'childs' => [], ]; if ( $node->attributes->getNamedItem( 'child-type' ) ) { $option[ 'child-type' ] = $node->attributes->getNamedItem( 'child-type' )->nodeValue; } if ( $node->hasChildNodes() ) { $this->_parseXML( $node->childNodes, $option[ 'childs' ] ); } $definition[ $option[ 'id' ] ] = $option; } } /** * */ public function delete() { /* @var SPdb $db */ $db = &SPFactory::db(); try { $db->delete( 'spdb_field_option', [ 'fid' => $this->id ] ); } catch ( SPException $x ) { Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 0, __LINE__, __FILE__ ); } } /** * @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 ); } }