2017-03-27 12:38:51 +00:00
< ? php
/*--------------------------------------------------------------------------------------------------------| www . vdm . io |------/
__ __ _ _____ _ _ __ __ _ _ _
\ \ / / | | | __ \ | | | | | \ / | | | | | | |
\ \ / / _ _ ___ | | _ | | | | _____ _____ | | ___ _ __ _ __ ___ ___ _ __ | | _ | \ / | ___ | | _ | | __ ___ __ | |
\ \ / / _ ` / __| __| | | | |/ _ \ \ / / _ \ |/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __| | |\/| |/ _ \ __| ' _ \ / _ \ / _ ` |
\ / ( _ | \__ \ | _ | | __ | | __ / \ V / __ / | ( _ ) | | _ ) | | | | | | __ / | | | | _ | | | | __ / | _ | | | | ( _ ) | ( _ | |
\ / \__ , _ | ___ / \__ | | _____ / \___ | \_ / \___ | _ | \___ /| . __ /| _ | | _ | | _ | \___ | _ | | _ | \__ | | _ | | _ | \___ | \__ | _ | | _ | \___ / \__ , _ |
| |
| _ |
2017-11-06 14:15:03 +00:00
/-------------------------------------------------------------------------------------------------------------------------------/
2017-03-27 12:38:51 +00:00
2017-11-06 14:04:23 +00:00
@ version 2.6 . x
2017-03-27 12:38:51 +00:00
@ created 30 th April , 2015
@ package Component Builder
@ subpackage import_joomla_components . php
@ author Llewellyn van der Merwe < http :// vdm . bz / component - builder >
2017-11-06 14:04:23 +00:00
@ github Joomla Component Builder < https :// github . com / vdm - io / Joomla - Component - Builder >
2017-03-27 12:38:51 +00:00
@ copyright Copyright ( C ) 2015. All Rights Reserved
@ license GNU / GPL Version 2 or later - http :// www . gnu . org / licenses / gpl - 2.0 . html
Builds Complex Joomla Components
/-----------------------------------------------------------------------------------------------------------------------------*/
// No direct access to this file
defined ( '_JEXEC' ) or die ( 'Restricted access' );
/**
* Componentbuilder Import_joomla_components Model
*/
class ComponentbuilderModelImport_joomla_components extends JModelLegacy
{
// set uploading values
protected $use_streams = false ;
protected $allow_unsafe = false ;
protected $safeFileOptions = array ();
/**
* @ var object JTable object
*/
protected $_table = null ;
/**
* @ var object JTable object
*/
protected $_url = null ;
/**
* Model context string .
*
* @ var string
*/
protected $_context = 'com_componentbuilder.import_joomla_components' ;
/**
* Import Settings
*/
protected $getType = NULL ;
protected $dataType = NULL ;
/**
* Method to auto - populate the model state .
*
* Note . Calling getState in this method will result in recursion .
*
* @ return void
*
*/
protected function populateState ()
{
$app = JFactory :: getApplication ( 'administrator' );
$this -> setState ( 'message' , $app -> getUserState ( 'com_componentbuilder.message' ));
$app -> setUserState ( 'com_componentbuilder.message' , '' );
// Recall the 'Import from Directory' path.
$path = $app -> getUserStateFromRequest ( $this -> _context . '.import_directory' , 'import_directory' , $app -> get ( 'tmp_path' ));
$this -> setState ( 'import.directory' , $path );
parent :: populateState ();
}
2017-03-28 14:57:59 +00:00
protected $app ;
2017-10-20 16:17:46 +00:00
protected $dir = false ;
protected $data = false ;
2017-10-19 03:53:55 +00:00
protected $target = false ;
protected $newID = array ();
protected $forceUpdate = 0 ;
protected $hasKey = 0 ;
protected $sleutle = null ;
protected $updateAfter = array ( 'field' => array (), 'adminview' => array ());
protected $divergedDataMover = array ();
protected $fieldTypes = array ();
protected $isMultiple = array ();
protected $specialValue = false ;
2017-03-27 12:38:51 +00:00
/**
* Import an spreadsheet from either folder , url or upload .
*
* @ return boolean result of import
*
*/
public function import ()
{
$this -> setState ( 'action' , 'import' );
2017-03-28 14:57:59 +00:00
$this -> app = JFactory :: getApplication ();
2017-03-27 12:38:51 +00:00
$session = JFactory :: getSession ();
$package = null ;
$continue = false ;
// get import type
2017-03-28 14:57:59 +00:00
$this -> getType = $this -> app -> input -> getString ( 'gettype' , NULL );
2017-03-27 12:38:51 +00:00
// get import type
$this -> dataType = $session -> get ( 'dataType_VDM_IMPORTINTO' , NULL );
if ( $package === null )
{
if ( $this -> dataType === 'smart_package' )
{
$this -> allow_unsafe = true ;
}
switch ( $this -> getType )
{
case 'folder' :
// Remember the 'Import from Directory' path.
2017-03-28 14:57:59 +00:00
$this -> app -> getUserStateFromRequest ( $this -> _context . '.import_directory' , 'import_directory' );
2017-03-27 12:38:51 +00:00
$package = $this -> _getPackageFromFolder ();
break ;
case 'upload' :
$package = $this -> _getPackageFromUpload ();
break ;
case 'url' :
$package = $this -> _getPackageFromUrl ();
break ;
case 'continue-basic' :
case 'continue-ext' :
$continue = true ;
$package = $session -> get ( 'package' , null );
$package = json_decode ( $package , true );
// clear session
$session -> clear ( 'package' );
$session -> clear ( 'dataType' );
$session -> clear ( 'hasPackage' );
2017-03-30 21:19:12 +00:00
$session -> clear ( 'smart_package_info' );
2017-03-27 12:38:51 +00:00
break ;
default :
2017-03-28 14:57:59 +00:00
$this -> app -> setUserState ( 'com_componentbuilder.message' , JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_NO_IMPORT_TYPE_FOUND' ));
2017-03-27 12:38:51 +00:00
return false ;
break ;
}
}
// Was the package valid?
if ( ! $package || ! $package [ 'type' ])
{
if ( in_array ( $this -> getType , array ( 'upload' , 'url' )))
{
$this -> remove ( $package [ 'packagename' ]);
}
2017-03-28 14:57:59 +00:00
$this -> app -> setUserState ( 'com_componentbuilder.message' , JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_UNABLE_TO_FIND_IMPORT_PACKAGE' ));
2017-03-27 12:38:51 +00:00
return false ;
}
// first link data to table headers
2017-03-30 21:19:12 +00:00
if ( ! $continue )
{
// check if this a smart package, if true then get info
if ( $this -> dataType === 'smart_package' )
{
$this -> getInfo ( $package , $session );
}
2017-03-27 12:38:51 +00:00
$package = json_encode ( $package );
$session -> set ( 'package' , $package );
$session -> set ( 'dataType' , $this -> dataType );
$session -> set ( 'hasPackage' , true );
return true ;
}
2017-03-30 21:19:12 +00:00
2017-03-27 12:38:51 +00:00
// set the data
if ( 'continue-basic' == $this -> getType )
{
$headerList = json_decode ( $session -> get ( $this -> dataType . '_VDM_IMPORTHEADERS' , false ), true );
}
else
{
$headerList = null ;
2017-03-28 14:57:59 +00:00
// force update
$this -> forceUpdate = $this -> app -> input -> getInt ( 'force_update' , 0 );
// has a key
$this -> hasKey = $this -> app -> input -> getInt ( 'haskey' , 0 );
// die sleutle
$this -> sleutle = $this -> app -> input -> getString ( 'sleutle' , NULL );
2017-03-27 12:38:51 +00:00
}
if ( ! $this -> setData ( $package , $headerList ))
{
// There was an error importing the package
$msg = JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_ERROR' );
2017-03-28 14:57:59 +00:00
$msgType = 'error' ;
2017-03-27 12:38:51 +00:00
$back = $session -> get ( 'backto_VDM_IMPORT' , NULL );
if ( $back )
{
2017-03-28 14:57:59 +00:00
$this -> app -> setUserState ( 'com_componentbuilder.redirect_url' , 'index.php?option=com_componentbuilder&view=' . $back );
2017-03-27 12:38:51 +00:00
$session -> clear ( 'backto_VDM_IMPORT' );
}
$result = false ;
}
else
{
// Package imported sucessfully
$msg = JText :: sprintf ( 'COM_COMPONENTBUILDER_IMPORT_SUCCESS' , $package [ 'packagename' ]);
2017-03-28 14:57:59 +00:00
$msgType = 'success' ;
2017-03-27 12:38:51 +00:00
$back = $session -> get ( 'backto_VDM_IMPORT' , NULL );
if ( $back )
{
2017-03-28 14:57:59 +00:00
$this -> app -> setUserState ( 'com_componentbuilder.redirect_url' , 'index.php?option=com_componentbuilder&view=' . $back );
2017-03-27 12:38:51 +00:00
$session -> clear ( 'backto_VDM_IMPORT' );
}
$result = true ;
}
2017-03-28 14:57:59 +00:00
// Set message to qeue
$this -> app -> enqueueMessage ( $msg , $msgType );
2017-03-27 12:38:51 +00:00
// remove file after import
$this -> remove ( $package [ 'packagename' ]);
$session -> clear ( $this -> getType . '_VDM_IMPORTHEADERS' );
return $result ;
2017-03-30 21:19:12 +00:00
}
protected function getInfo ( & $package , & $session )
{
// set the data
if ( isset ( $package [ 'dir' ]))
{
// set auto loader
ComponentbuilderHelper :: autoLoader ( 'smart' );
// extract the package
if ( JFile :: exists ( $package [ 'dir' ]))
{
// get the zip adapter
$zip = JArchive :: getAdapter ( 'zip' );
// set the directory name
2017-10-20 16:17:46 +00:00
$this -> dir = JFile :: stripExt ( $package [ 'dir' ]);
2017-03-30 21:19:12 +00:00
// unzip the package
2017-10-20 16:17:46 +00:00
$zip -> extract ( $package [ 'dir' ], $this -> dir );
2017-03-30 21:19:12 +00:00
// check for database file
2017-10-20 16:17:46 +00:00
$infoFile = $this -> dir . '/info.vdm' ;
2017-03-30 21:19:12 +00:00
if ( JFile :: exists ( $infoFile ))
{
// load the data
if ( $info = @ file_get_contents ( $infoFile ))
{
// remove all line breaks
$info = str_replace ( " \n " , '' , $info );
// make sure we have base64
if ( $info === base64_encode ( base64_decode ( $info , true )))
{
// Get the encryption object.
$opener = new FOFEncryptAes ( 'V4stD3vel0pmEntMethOd@YoUrS3rv!s' , 128 );
$info = rtrim ( $opener -> decryptString ( $info ), " \0 " );
$session -> set ( 'smart_package_info' , $info );
return true ;
}
}
}
2017-10-20 16:17:46 +00:00
ComponentbuilderHelper :: removeFolder ( $this -> dir );
2017-03-30 21:19:12 +00:00
}
}
return false ;
2017-03-27 12:38:51 +00:00
}
/**
* Works out an importation spreadsheet from a HTTP upload
*
* @ return spreadsheet definition or false on failure
*/
protected function _getPackageFromUpload ()
{
// Get the uploaded file information
$app = JFactory :: getApplication ();
$input = $app -> input ;
// Do not change the filter type 'raw'. We need this to let files containing PHP code to upload. See JInputFiles::get.
$userfile = $input -> files -> get ( 'import_package' , null , 'raw' );
// Make sure that file uploads are enabled in php
if ( ! ( bool ) ini_get ( 'file_uploads' ))
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_WARNIMPORTFILE' ), 'warning' );
return false ;
}
// If there is no uploaded file, we have a problem...
if ( ! is_array ( $userfile ))
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_NO_FILE_SELECTED' ), 'warning' );
return false ;
}
// Check if there was a problem uploading the file.
if ( $userfile [ 'error' ] || $userfile [ 'size' ] < 1 )
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_WARNIMPORTUPLOADERROR' ), 'warning' );
return false ;
}
// Build the appropriate paths
$config = JFactory :: getConfig ();
$tmp_dest = $config -> get ( 'tmp_path' ) . '/' . $userfile [ 'name' ];
$tmp_src = $userfile [ 'tmp_name' ];
// Move uploaded file
jimport ( 'joomla.filesystem.file' );
$p_file = JFile :: upload ( $tmp_src , $tmp_dest , $this -> use_streams , $this -> allow_unsafe , $this -> safeFileOptions );
// Was the package downloaded?
if ( ! $p_file )
{
$session = JFactory :: getSession ();
$session -> clear ( 'package' );
$session -> clear ( 'dataType' );
$session -> clear ( 'hasPackage' );
// was not uploaded
return false ;
}
// check that this is a valid spreadsheet
$package = $this -> check ( $userfile [ 'name' ]);
return $package ;
}
/**
* Import an spreadsheet from a directory
*
* @ return array Spreadsheet details or false on failure
*
*/
protected function _getPackageFromFolder ()
{
$app = JFactory :: getApplication ();
$input = $app -> input ;
// Get the path to the package to import
$p_dir = $input -> getString ( 'import_directory' );
$p_dir = JPath :: clean ( $p_dir );
// Did you give us a valid path?
if ( ! file_exists ( $p_dir ))
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_PLEASE_ENTER_A_PACKAGE_DIRECTORY' ), 'warning' );
return false ;
}
// Detect the package type
$type = $this -> getType ;
// Did you give us a valid package?
if ( ! $type )
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_PATH_DOES_NOT_HAVE_A_VALID_PACKAGE' ), 'warning' );
}
// check the extention
if ( ! $this -> checkExtension ( $p_dir ))
{
// set error message
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_DOES_NOT_HAVE_A_VALID_FILE_TYPE' ), 'warning' );
return false ;
}
$package [ 'packagename' ] = null ;
$package [ 'dir' ] = $p_dir ;
$package [ 'type' ] = $type ;
return $package ;
}
/**
* Import an spreadsheet from a URL
*
* @ return Package details or false on failure
*
*/
protected function _getPackageFromUrl ()
{
$app = JFactory :: getApplication ();
$input = $app -> input ;
// Get the URL of the package to import
$url = $input -> getString ( 'import_url' );
// Did you give us a URL?
if ( ! $url )
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_ENTER_A_URL' ), 'warning' );
return false ;
}
// Download the package at the URL given
$p_file = JInstallerHelper :: downloadPackage ( $url );
// Was the package downloaded?
if ( ! $p_file )
{
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_INVALID_URL' ), 'warning' );
return false ;
}
// check that this is a valid spreadsheet
$package = $this -> check ( $p_file );
return $package ;
}
/**
* Check a file and verifies it as a spreadsheet file
* Supports . csv . xlsx . xls and . ods
*
* @ param string $p_filename The uploaded package filename or import directory
*
* @ return array of elements
*
*/
protected function check ( $archivename )
{
$app = JFactory :: getApplication ();
// Clean the name
$archivename = JPath :: clean ( $archivename );
// check the extention
if ( ! $this -> checkExtension ( $archivename ))
{
// Cleanup the import files
$this -> remove ( $archivename );
$app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_IMPORT_MSG_DOES_NOT_HAVE_A_VALID_FILE_TYPE' ), 'warning' );
return false ;
}
$config = JFactory :: getConfig ();
// set Package Name
$check [ 'packagename' ] = $archivename ;
// set directory
$check [ 'dir' ] = $config -> get ( 'tmp_path' ) . '/' . $archivename ;
// set type
$check [ 'type' ] = $this -> getType ;
return $check ;
}
/**
* Check the extension
*
* @ param string $file Name of the uploaded file
*
* @ return boolean True on success
*
*/
protected function checkExtension ( $file )
{
// check the extension
switch ( strtolower ( pathinfo ( $file , PATHINFO_EXTENSION )))
{
case 'xls' :
case 'ods' :
case 'csv' :
return true ;
break ;
case 'zip' :
if ( $this -> dataType === 'smart_package' )
{
return true ;
}
break ;
}
return false ;
}
/**
* Clean up temporary uploaded spreadsheet
*
* @ param string $package Name of the uploaded spreadsheet file
*
* @ return boolean True on success
*
*/
protected function remove ( $package )
{
jimport ( 'joomla.filesystem.file' );
$config = JFactory :: getConfig ();
$package = $config -> get ( 'tmp_path' ) . '/' . $package ;
// Is the package file a valid file?
if ( is_file ( $package ))
{
JFile :: delete ( $package );
}
elseif ( is_file ( JPath :: clean ( $package )))
{
// It might also be just a base filename
JFile :: delete ( JPath :: clean ( $package ));
}
}
/**
* Set the data from the spreadsheet to the database
*
* @ param string $package Paths to the uploaded package file
*
* @ return boolean false on failure
*
**/
protected function setData ( $package , $target_headers )
{
$jinput = JFactory :: getApplication () -> input ;
// set the data based on the type of import being done
if ( 'continue-basic' === $this -> getType && ComponentbuilderHelper :: checkArray ( $target_headers ))
{
foreach ( $target_headers as $header )
{
$data [ 'target_headers' ][ $header ] = $jinput -> getString ( $header , null );
}
// make sure the file is loaded
JLoader :: import ( 'PHPExcel' , JPATH_COMPONENT_ADMINISTRATOR . '/helpers' );
// set the data
if ( isset ( $package [ 'dir' ]))
{
$inputFileType = PHPExcel_IOFactory :: identify ( $package [ 'dir' ]);
$excelReader = PHPExcel_IOFactory :: createReader ( $inputFileType );
$excelReader -> setReadDataOnly ( true );
$excelObj = $excelReader -> load ( $package [ 'dir' ]);
$data [ 'array' ] = $excelObj -> getActiveSheet () -> toArray ( null , true , true , true );
$excelObj -> disconnectWorksheets ();
unset ( $excelObj );
return $this -> saveBasic ( $data );
}
}
elseif ( 'continue-ext' === $this -> getType )
{
// set the data
if ( isset ( $package [ 'dir' ]))
{
// set auto loader
ComponentbuilderHelper :: autoLoader ( 'smart' );
// extract the package
if ( JFile :: exists ( $package [ 'dir' ]))
{
// set the directory name
2017-10-20 16:17:46 +00:00
$this -> dir = JFile :: stripExt ( $package [ 'dir' ]);
2017-03-27 12:38:51 +00:00
// check for database file
2017-10-20 16:17:46 +00:00
$dbFile = $this -> dir . '/db.vdm' ;
2017-03-30 21:19:12 +00:00
if ( ! JFile :: exists ( $dbFile ))
{
// get the zip adapter
$zip = JArchive :: getAdapter ( 'zip' );
// unzip the package
2017-10-20 16:17:46 +00:00
$zip -> extract ( $package [ 'dir' ], $this -> dir );
2017-03-30 21:19:12 +00:00
}
// check again
2017-03-27 12:38:51 +00:00
if ( JFile :: exists ( $dbFile ))
{
// load the data
if ( $data = @ file_get_contents ( $dbFile ))
{
// prep the data
2017-10-20 16:17:46 +00:00
if ( $this -> extractData ( $data ))
2017-03-27 12:38:51 +00:00
{
2017-10-20 16:17:46 +00:00
if ( isset ( $this -> data [ 'joomla_component' ]) && ComponentbuilderHelper :: checkArray ( $this -> data [ 'joomla_component' ]))
2017-03-27 12:38:51 +00:00
{
2017-03-28 14:57:59 +00:00
// save the smart data
2017-10-20 16:17:46 +00:00
if ( $this -> saveSmartComponents ())
2017-03-28 14:57:59 +00:00
{
2017-10-20 16:17:46 +00:00
ComponentbuilderHelper :: removeFolder ( $this -> dir );
2017-03-28 14:57:59 +00:00
return true ;
}
2017-03-27 12:38:51 +00:00
}
}
}
}
2017-10-20 16:17:46 +00:00
ComponentbuilderHelper :: removeFolder ( $this -> dir );
2017-03-27 12:38:51 +00:00
}
}
}
return false ;
2017-03-28 14:57:59 +00:00
}
/**
* Extract the data from the string
*
* @ param string $data The data string
*
* @ return object false on failure
*
**/
protected function extractData ( $data )
{
// remove all line breaks
$data = str_replace ( " \n " , '' , $data );
// make sure we have base64
if ( $data === base64_encode ( base64_decode ( $data , true )))
{
// open the data
2017-03-30 21:19:12 +00:00
if ( ComponentbuilderHelper :: checkString ( $this -> sleutle ) && strlen ( $this -> sleutle ) == 32 )
2017-03-28 14:57:59 +00:00
{
// Get the encryption object.
$opener = new FOFEncryptAes ( $this -> sleutle , 128 );
$data = rtrim ( $opener -> decryptString ( $data ), " \0 " );
}
else
{
2017-03-30 21:19:12 +00:00
$data = base64_decode ( $data );
2017-03-28 14:57:59 +00:00
}
// final check if we have success
$data = @ unserialize ( $data );
if ( $data !== false )
{
2017-10-20 16:17:46 +00:00
// set the data global
$this -> data = $data ;
return true ;
2017-03-28 14:57:59 +00:00
}
$this -> app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_HTWODATA_IS_CORRUPTHTWOTHIS_COULD_BE_DUE_TO_KEY_ERROR_OR_BROKEN_PACKAGE' ), 'error' );
return false ;
}
$this -> app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_HTWODATA_IS_CORRUPTHTWOTHIS_COULD_BE_DUE_TO_BROKEN_PACKAGE' ), 'error' );
return false ;
2017-03-27 12:38:51 +00:00
}
/**
* Save the smart components
*
* @ return boolean false on failure
*
**/
2017-10-20 16:17:46 +00:00
protected function saveSmartComponents ()
2017-03-27 12:38:51 +00:00
{
// get user object
2017-08-23 20:38:09 +00:00
$this -> user = JFactory :: getUser ();
2017-03-27 12:38:51 +00:00
// set some defaults
2017-08-23 20:38:09 +00:00
$this -> today = JFactory :: getDate () -> toSql ();
2017-10-29 16:07:37 +00:00
// the array of tables to store
$tables = array (
'fieldtype' , 'field' , 'admin_view' , 'snippet' , 'dynamic_get' , 'custom_admin_view' , 'site_view' ,
'template' , 'layout' , 'joomla_component' , 'language' , 'language_translation' , 'custom_code' ,
'admin_fields' , 'admin_fields_conditions' , 'component_admin_views' , 'component_site_views' ,
'component_custom_admin_views' , 'component_updates' , 'component_mysql_tweaks' ,
'component_custom_admin_menus' , 'component_config' , 'component_dashboard' , 'component_files_folders'
);
// smart table loop
foreach ( $tables as $table )
{
// save the table to database
if ( ! $this -> saveSmartItems ( $table ))
{
return false ;
}
2017-03-27 12:38:51 +00:00
}
2017-03-31 21:28:16 +00:00
// do a after all run on all items that need it
2017-08-23 20:38:09 +00:00
$this -> updateAfter ();
2017-10-19 03:53:55 +00:00
// finally move the old datasets
$this -> moveDivergedData ();
2017-03-27 12:38:51 +00:00
// lets move all the files to its correct location
2017-10-20 16:17:46 +00:00
if ( ! $this -> moveSmartStuff ())
2017-03-27 12:38:51 +00:00
{
return false ;
}
return true ;
}
/**
2017-10-20 16:17:46 +00:00
* Save the smart items
*
* @ param string $type The type of values
2017-03-27 12:38:51 +00:00
*
2017-10-20 16:17:46 +00:00
* @ return boolean false on failure
*
**/
protected function saveSmartItems ( $table )
{
$success = true ;
if ( isset ( $this -> data [ $table ]) && ComponentbuilderHelper :: checkArray ( $this -> data [ $table ]))
{
// get global action permissions
$canDo = ComponentbuilderHelper :: getActions ( $table );
$canEdit = $canDo -> get ( 'core.edit' );
$canState = $canDo -> get ( 'core.edit.state' );
$canCreate = $canDo -> get ( 'core.create' );
// set id keeper
if ( ! isset ( $this -> newID [ $table ]))
{
$this -> newID [ $table ] = array ();
}
foreach ( $this -> data [ $table ] as $item )
{
$oldID = ( int ) $item -> id ;
// first check if exist
if ( $local = $this -> getLocalItem ( $item , $table , 1 ))
{
$dbDate = strtotime ( $item -> modified );
$localDate = strtotime ( $local -> modified );
// okay we have it local (check if the version is newer)
if ( $this -> forceUpdate == 1 || $dbDate > $localDate )
{
// add some local values to item to combine
if ( $table === 'language_translation' )
{
$item -> localComponents = $local -> components ;
$item -> localTranslation = $local -> translation ;
}
// make sure we have the correct ID set
$item -> id = $local -> id ;
// yes it is newer, lets update (or is being forced)
if ( $canEdit && $id = $this -> updateLocalItem ( $item , $table , $canState ))
{
// we had success in
$this -> newID [ $table ][ $oldID ] = ( int ) $id ;
// to debug if your mismatching is to much (TODO) we can add a debugging switch later
// $this->app->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_BSB_HAS_BEEN_UPDATED', $table.' id:'.$oldID . '->' . $id), 'success');
}
else
{
$notice = '!' ;
if ( ! $canEdit )
{
$notice = JText :: sprintf ( " COM_COMPONENTBUILDER__SINCE_YOU_DONT_HAVE_PERMISSION_TO_EDIT_S " , $table );
}
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_BSB_COULD_NOT_BE_IMPORTEDS' , $table . ' id:' . $oldID , $notice ), 'error' );
}
}
// insure to load the local ID to link imported values with it
if ( ! isset ( $this -> newID [ $table ][ $oldID ]))
{
$this -> newID [ $table ][ $oldID ] = ( int ) $local -> id ;
}
}
elseif ( $canCreate && $id = $this -> addLocalItem ( $item , $table ))
{
// not found in local db so add
$this -> newID [ $table ][ $oldID ] = ( int ) $id ;
// to debug if your mismatching is to much
// $this->app->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_BSB_HAS_BEEN_IMPORTED', $table.' id:'.$oldID . '->' . $id), 'success');
}
else
{
$notice = '!' ;
if ( ! $canCreate )
{
$notice = JText :: sprintf ( " COM_COMPONENTBUILDER__SINCE_YOU_DONT_HAVE_PERMISSION_TO_CREATE_S " , $table );
}
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_BSB_COULD_NOT_BE_IMPORTEDS' , $table . ' id:' . $oldID , $notice ), 'error' );
}
}
}
return $success ;
}
/**
* Move the smart content ( files & folders ) into place
2017-03-27 12:38:51 +00:00
*
* @ return boolean false on failure
*
**/
2017-10-20 16:17:46 +00:00
protected function moveSmartStuff ()
2017-03-27 12:38:51 +00:00
{
2017-04-01 13:59:54 +00:00
// make sure to first unlock files
2017-10-20 16:17:46 +00:00
$this -> unLockFiles ( $this -> dir );
2017-03-27 12:38:51 +00:00
// set params
$params = JComponentHelper :: getParams ( 'com_componentbuilder' );
// set custom folder path
2017-04-08 20:52:51 +00:00
$customPath = str_replace ( '//' , '/' , $params -> get ( 'custom_folder_path' , JPATH_COMPONENT_ADMINISTRATOR . '/custom' ));
$imagesPath = str_replace ( '//' , '/' , JPATH_SITE . '/images' );
2017-03-28 14:57:59 +00:00
$success = true ;
2017-03-27 12:38:51 +00:00
// check if we have custom files
2017-10-20 16:17:46 +00:00
$customDir = str_replace ( '//' , '/' , $this -> dir . '/custom' );
2017-04-08 20:52:51 +00:00
if ( JFolder :: exists ( $customDir ))
2017-03-27 12:38:51 +00:00
{
// great we have some custom stuff lets move it
2017-04-08 20:52:51 +00:00
if ( ! JFolder :: copy ( $customDir , $customPath , '' , true ))
2017-03-28 14:57:59 +00:00
{
$this -> app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_BCUSTOM_FILESB_NOT_MOVE_TO_CORRECT_LOCATION' ), 'error' );
$success = false ;
}
2017-03-27 12:38:51 +00:00
}
// check if we have images
2017-10-20 16:17:46 +00:00
$imageDir = str_replace ( '//' , '/' , $this -> dir . '/images' );
2017-04-08 20:52:51 +00:00
if ( JFolder :: exists ( $imageDir ))
2017-03-27 12:38:51 +00:00
{
// great we have some images lets move them
2017-04-08 20:52:51 +00:00
if ( ! JFolder :: copy ( $imageDir , $imagesPath , '' , true ))
2017-03-28 14:57:59 +00:00
{
$this -> app -> enqueueMessage ( JText :: _ ( 'COM_COMPONENTBUILDER_BIMAGESB_NOT_MOVE_TO_CORRECT_LOCATION' ), 'error' );
$success = false ;
}
2017-03-27 12:38:51 +00:00
}
2017-03-28 14:57:59 +00:00
return $success ;
2017-03-27 12:38:51 +00:00
}
2017-04-01 13:59:54 +00:00
/**
* Method to unlock all files
*
* @ return void
*/
2017-10-20 16:17:46 +00:00
protected function unLockFiles ()
2017-04-01 13:59:54 +00:00
{
// lock the data if set
if ( ComponentbuilderHelper :: checkString ( $this -> sleutle ) && strlen ( $this -> sleutle ) == 32 )
{
$unlocker = new FOFEncryptAes ( $this -> sleutle , 128 );
// we must first store the current working directory
$joomla = getcwd ();
// setup the type path
2017-10-20 16:17:46 +00:00
$customPath = str_replace ( '//' , '/' , $this -> dir . '/custom' );
2017-04-01 13:59:54 +00:00
// go to the custom folder if found
if ( JFolder :: exists ( $customPath ))
{
$this -> unlock ( $customPath , $unlocker );
}
// setup the type path
2017-10-20 16:17:46 +00:00
$imagesPath = str_replace ( '//' , '/' , $this -> dir . '/images' );
2017-04-01 13:59:54 +00:00
// go to the custom folder if found
if ( JFolder :: exists ( $imagesPath ))
{
$this -> unlock ( $imagesPath , $unlocker );
}
// change back to working dir
chdir ( $joomla );
}
}
/**
* The Unlocker
*
* @ return void
*/
protected function unlock ( & $tmpPath , & $unlocker )
{
// we are changing the working directory to the tmp path (important)
chdir ( $tmpPath );
// get a list of files in the current directory tree (all)
$files = JFolder :: files ( '.' , '.' , true , true );
// read in the file content
foreach ( $files as $file )
{
// check that the string is base64
$data = str_replace ( " \n " , '' , file_get_contents ( $file ));
if ( $data === base64_encode ( base64_decode ( $data , true )))
{
// write the decrypted data back to file
if ( ! ComponentbuilderHelper :: writeFile ( $file , rtrim ( $unlocker -> decryptString ( $data ), " \0 " )))
{
// we should add error handler here in case file could not be unlocked
}
}
}
}
2017-03-31 21:28:16 +00:00
/**
* Update some items after all
*
* @ return void
*
**/
2017-08-23 20:38:09 +00:00
protected function updateAfter ()
2017-03-31 21:28:16 +00:00
{
if ( ComponentbuilderHelper :: checkArray ( $this -> updateAfter [ 'field' ]))
{
// update repeatable
foreach ( $this -> updateAfter [ 'field' ] as $field )
{
if ( isset ( $this -> newID [ 'field' ][ $field ]))
{
$field = $this -> newID [ 'field' ][ $field ];
}
// get the field from db
if ( $xml = ComponentbuilderHelper :: getVar ( 'field' , $field , 'id' , 'xml' ))
{
if ( ComponentbuilderHelper :: checkJson ( $xml ))
{
$xml = json_decode ( $xml );
$fields = ComponentbuilderHelper :: getBetween ( $xml , 'fields="' , '"' );
$fieldsSets = array ();
if ( strpos ( $fields , ',' ) !== false )
{
// multiple fields
$fieldsSets = array_map ( 'trim' , ( array ) explode ( ',' , $fields ));
}
elseif ( is_numeric ( $fields ))
{
// single field
$fieldsSets [] = ( int ) $fields ;
}
// update the fields
if ( ComponentbuilderHelper :: checkArray ( $fieldsSets ))
{
$bucket = array ();
foreach ( $fieldsSets as $id )
{
2017-10-20 16:17:46 +00:00
if ( isset ( $this -> newID [ 'field' ][( int ) $id ]))
2017-03-31 21:28:16 +00:00
{
2017-10-20 16:17:46 +00:00
$bucket [] = $this -> newID [ 'field' ][( int ) $id ];
2017-03-31 21:28:16 +00:00
}
else
{
2017-10-20 16:17:46 +00:00
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_BMULTIPLE_FIELD_REPEATABLE_MODEB_IDS_MISMATCH_IN_BFIELDSB_AND_WAS_EMREMOVEDEM_FROM_THE_FIELD' , $id , $field ), 'warning' );
2017-03-31 21:28:16 +00:00
}
}
if ( ComponentbuilderHelper :: checkArray ( $bucket ))
{
$string = implode ( ',' , $bucket );
$xml = json_encode ( str_replace ( 'fields="' . $fields . '"' , 'fields="' . $string . '"' , $xml ));
}
else
{
$xml = '' ;
}
$object = new stdClass ;
$object -> id = $field ;
$object -> xml = $xml ;
// update the field
2017-08-23 20:38:09 +00:00
$this -> _db -> updateObject ( '#__componentbuilder_field' , $object , 'id' );
2017-03-31 21:28:16 +00:00
}
}
}
}
}
// do a after all run on admin views that need it
if ( ComponentbuilderHelper :: checkArray ( $this -> updateAfter [ 'adminview' ]))
{
// update the addlinked_views
foreach ( $this -> updateAfter [ 'adminview' ] as $adminview )
{
2017-10-20 16:17:46 +00:00
if ( isset ( $this -> newID [ 'admin_view' ][( int ) $adminview ]))
2017-03-31 21:28:16 +00:00
{
2017-10-20 16:17:46 +00:00
$adminview = $this -> newID [ 'admin_view' ][( int ) $adminview ];
2017-03-31 21:28:16 +00:00
}
// get the field from db
if ( $addlinked_views = ComponentbuilderHelper :: getVar ( 'admin_view' , $adminview , 'id' , 'addlinked_views' ))
2017-10-19 03:53:55 +00:00
{
2017-03-31 21:28:16 +00:00
if ( ComponentbuilderHelper :: checkJson ( $addlinked_views ))
{
$addlinked_views = json_decode ( $addlinked_views , true );
2017-10-19 03:53:55 +00:00
// convert Repetable Fields
if ( ComponentbuilderHelper :: checkArray ( $addlinked_views ) && isset ( $addlinked_views [ 'adminview' ]))
2017-03-31 21:28:16 +00:00
{
2017-10-29 11:03:06 +00:00
$addlinked_views = ComponentbuilderHelper :: convertRepeatable ( $addlinked_views , 'addlinked_views' );
2017-03-31 21:28:16 +00:00
}
2017-10-19 03:53:55 +00:00
// update the view IDs
if ( ComponentbuilderHelper :: checkArray ( $addlinked_views ))
{
// only update the view IDs
$addlinked_views = $this -> updateSubformIDs ( $addlinked_views , 'admin_view' , array ( 'adminview' => 'admin_view' ));
}
// update the fields
$object = new stdClass ;
$object -> id = $adminview ;
$object -> addlinked_views = json_encode ( $addlinked_views );
// update the admin view
$this -> _db -> updateObject ( '#__componentbuilder_admin_view' , $object , 'id' );
}
}
}
}
}
/**
* Moving of diverged data
*
* @ return void
*
**/
protected function moveDivergedData ()
{
// check if there is data to move
if ( ComponentbuilderHelper :: checkArray ( $this -> divergedDataMover ))
{
foreach ( $this -> divergedDataMover as $table => $values )
{
foreach ( $values as $value )
{
// first check if exist (only add if it does not)
if ( ! $this -> getLocalItem ( $value , $table , 1 ))
{
// add the diverged data
$this -> addLocalItem ( $value , $table );
2017-03-31 21:28:16 +00:00
}
}
}
}
}
2017-10-19 03:53:55 +00:00
/**
* Update Many Subform IDs
*
* @ param array $values The values to update the IDs in
* @ param string $table The table these values belong to
* @ param array $targets The target to update and its type
*
* @ return void
*/
protected function updateSubformsIDs ( & $item , $table , $targets )
{
// update the repeatable fields
foreach ( $targets as $field => $targetArray )
{
if ( isset ( $item -> { $field }) && ComponentbuilderHelper :: checkJson ( $item -> { $field }))
{
$updateArray = json_decode ( $item -> { $field }, true );
if ( ComponentbuilderHelper :: checkArray ( $updateArray ))
{
// load it back
$item -> { $field } = json_encode ( $this -> updateSubformIDs ( $updateArray , $table , $targetArray ));
}
}
}
}
/**
* Update One Subform IDs
*
* @ param array $values The values to update the IDs in
* @ param string $table The table these values belong to
* @ param array $targets The target to update and its type
*
* @ return void
*/
protected function updateSubformIDs ( $values , $table , $targets )
{
$isJson = false ;
if ( ComponentbuilderHelper :: checkJson ( $values ))
{
$values = json_decode ( $values , true );
$isJson = true ;
}
// now update the fields
if ( ComponentbuilderHelper :: checkArray ( $values ))
{
foreach ( $values as $nr => & $value )
{
foreach ( $targets as $target => $target_type )
{
if ( isset ( $value [ $target ]))
{
2017-10-20 16:17:46 +00:00
$value = $this -> setNewID ( $value , $target , $target_type , $table );
2017-10-19 03:53:55 +00:00
}
}
}
}
if ( $isJson )
{
return json_encode ( $values );
}
return $values ;
}
2017-10-20 16:17:46 +00:00
/**
* Set the new ID
*
* @ param array $item The values to update the IDs in
* @ param string $target The target field
* @ param string $type The table of that field
* @ param string $table The table these values belong to
*
* @ return boolean True on success
*
*/
protected function setNewID ( $item , $target , $type , $table )
{
$isJson = false ;
if ( ComponentbuilderHelper :: checkJson ( $item ))
{
$item = json_decode ( $item , true );
$isJson = true ;
}
if ( ComponentbuilderHelper :: checkArray ( $item ))
{
// set item ID
$itemId = ( isset ( $item [ 'id' ])) ? $item [ 'id' ] : 'newItem' ;
// check if it is json
$isJsonTarget = false ;
if ( ComponentbuilderHelper :: checkJson ( $item [ $target ]))
{
$item [ $target ] = json_decode ( $item [ $target ], true );
$isJsonTarget = true ;
}
// update the target
if ( ComponentbuilderHelper :: checkString ( $item [ $target ]) || is_numeric ( $item [ $target ]))
{
if ( $item [ $target ] == 0 )
{
$item [ $target ] = '' ;
}
elseif ( isset ( $this -> newID [ $type ][( int ) $item [ $target ]]))
{
$item [ $target ] = $this -> newID [ $type ][( int ) $item [ $target ]];
}
else
{
$this -> enqueueIdMismatchMessage ( $item [ $target ], $itemId , $target , $type , $table );
$item [ $target ] = '' ;
}
}
elseif ( ComponentbuilderHelper :: checkArray ( $item [ $target ]))
{
// the bucket to load the items back
$bucket = array ();
foreach ( $item [ $target ] as $nr => $id )
{
if ( $id == 0 )
{
continue ;
}
elseif (( ComponentbuilderHelper :: checkString ( $id ) || is_numeric ( $id )) && isset ( $this -> newID [ $type ][( int ) $id ]))
{
$bucket [] = $this -> newID [ $type ][( int ) $id ];
}
else
{
$this -> enqueueIdMismatchMessage ( $id , $itemId , $target , $type , $table );
}
}
// set ids back
if ( ComponentbuilderHelper :: checkArray ( $bucket ))
{
$item [ $target ] = $bucket ;
}
}
// convert back to json
if ( $isJsonTarget )
{
$item [ $target ] = json_encode ( $item [ $target ]);
}
}
elseif ( ComponentbuilderHelper :: checkObject ( $item ))
{
// set item ID
$itemId = ( isset ( $item -> id )) ? $item -> id : 'newItem' ;
// check if it is json
$isJsonTarget = false ;
if ( ComponentbuilderHelper :: checkJson ( $item -> { $target }))
{
$item -> { $target } = json_decode ( $item -> { $target }, true );
$isJsonTarget = true ;
}
// update the target
if ( ComponentbuilderHelper :: checkString ( $item -> { $target }) || is_numeric ( $item -> { $target }))
{
if ( $item -> { $target } == 0 )
{
$item -> { $target } = '' ;
}
elseif ( isset ( $this -> newID [ $type ][( int ) $item -> { $target }]))
{
$item -> { $target } = $this -> newID [ $type ][( int ) $item -> { $target }];
}
else
{
$this -> enqueueIdMismatchMessage ( $item -> { $target }, $itemId , $target , $type , $table );
$item -> { $target } = '' ;
}
}
elseif ( ComponentbuilderHelper :: checkArray ( $item -> { $target }))
{
// the bucket to load the items back
$bucket = array ();
foreach ( $item -> { $target } as $id )
{
if ( $id == 0 )
{
continue ;
}
elseif (( ComponentbuilderHelper :: checkString ( $id ) || is_numeric ( $id )) && isset ( $this -> newID [ $type ][( int ) $id ]))
{
$bucket [] = $this -> newID [ $type ][( int ) $id ];
}
else
{
$this -> enqueueIdMismatchMessage ( $id , $itemId , $target , $type , $table );
$bucket [] = '' ;
}
}
// set ids back
if ( ComponentbuilderHelper :: checkArray ( $bucket ))
{
$item -> { $target } = $bucket ;
}
}
// convert back to json
if ( $isJsonTarget )
{
$item -> { $target } = json_encode ( $item -> { $target });
}
}
// return as json if received as json
if ( $isJson )
{
return json_encode ( $item );
}
return $item ;
}
/**
* Set the new ID
*
* @ param int $id The field ID
* @ param int $itemId The item ID
* @ param string $target The target field
* @ param string $type The table of that field
* @ param string $table The table these values belong to
*
* @ return void
*
*/
protected function enqueueIdMismatchMessage ( $id , $itemId , $target , $type , $table )
{
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_BSBS_IN_BSB_HAS_ID_MISMATCH_SO_THE_BSB_WAS_REMOVED' , ComponentbuilderHelper :: safeString ( $type , 'Ww' ), ComponentbuilderHelper :: safeString ( $target , 'Ww' ) , ComponentbuilderHelper :: safeString ( $table , 'w' ) . ':' . $itemId , $type . ':' . $id ), 'warning' );
}
2017-03-27 12:38:51 +00:00
/**
* Prep the item
*
* @ param object $item The item to prep
* @ param string $type The type of values
2017-08-23 20:38:09 +00:00
* @ param string $action The action ( add / update )
2017-03-27 12:38:51 +00:00
*
* @ return mixed false on failure
2017-08-23 20:38:09 +00:00
* object on success
2017-03-27 12:38:51 +00:00
*
**/
2017-08-23 20:38:09 +00:00
protected function prepItem ( $item , & $type , $action )
2017-03-27 12:38:51 +00:00
{
2017-11-10 02:13:36 +00:00
// remove access
if ( isset ( $item -> access ))
{
unset ( $item -> access );
}
// remove metadata
if ( isset ( $item -> metadata ))
{
unset ( $item -> metadata );
}
// remove metadesc
if ( isset ( $item -> metadesc ))
{
unset ( $item -> metadesc );
}
// remove metakey
if ( isset ( $item -> metakey ))
{
unset ( $item -> metakey );
}
// actions to effect all
2017-03-27 12:38:51 +00:00
if ( isset ( $item -> asset_id ))
{
unset ( $item -> asset_id );
}
if ( isset ( $item -> checked_out ))
{
$item -> checked_out = 0 ;
}
if ( isset ( $item -> checked_out_time ))
{
$item -> checked_out_time = '0000-00-00 00:00:00' ;
}
// do the id fix for the new ids
switch ( $type )
{
2017-10-19 03:53:55 +00:00
case 'fieldtype' :
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'properties' => 'name'
);
// update the repeatable fields
2017-10-29 11:03:06 +00:00
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
2017-10-19 03:53:55 +00:00
break ;
2017-03-27 12:38:51 +00:00
case 'field' :
// update the fieldtype
2017-10-20 16:17:46 +00:00
if ( isset ( $item -> fieldtype ) && is_numeric ( $item -> fieldtype ) && $item -> fieldtype > 0 && isset ( $this -> newID [ 'fieldtype' ][( int ) $item -> fieldtype ]))
2017-03-27 12:38:51 +00:00
{
2017-10-20 16:17:46 +00:00
$item -> fieldtype = $this -> newID [ 'fieldtype' ][( int ) $item -> fieldtype ];
2017-09-18 02:20:50 +00:00
// update multi field values
if ( $this -> checkMultiFields ( $item -> fieldtype ))
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
$this -> updateAfter [ 'field' ][ $item -> id ] = $item -> id ; // multi field
2017-03-31 21:28:16 +00:00
}
}
elseif ( ! is_numeric ( $item -> fieldtype ) || $item -> fieldtype == 0 )
{
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_BFIELD_TYPEB_NOT_SET_FOR_BSB' , ComponentbuilderHelper :: safeString ( $type , 'w' ) . ':' . $item -> id ), 'warning' );
unset ( $item -> fieldtype );
2017-03-27 12:38:51 +00:00
}
else
{
2017-03-28 14:57:59 +00:00
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_BFIELD_TYPEB_IDS_MISMATCH_IN_BSB' , $item -> fieldtype , ComponentbuilderHelper :: safeString ( $type , 'w' ) . ':' . $item -> id ), 'error' );
2017-03-27 12:38:51 +00:00
return false ;
}
break ;
case 'dynamic_get' :
// update the view_table_main ID
2017-10-20 16:17:46 +00:00
if ( isset ( $item -> main_source ) && $item -> main_source == 1 )
2017-03-27 12:38:51 +00:00
{
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'view_table_main' , 'admin_view' , $type );
2017-03-27 12:38:51 +00:00
}
2017-10-06 14:53:22 +00:00
// repeatable fields to update
$updaterR = array (
2017-10-19 03:53:55 +00:00
// repeatablefield => checker
'join_view_table' => 'view_table' ,
2017-10-06 14:53:22 +00:00
'join_db_table' => 'db_table' ,
'order' => 'table_key' ,
'where' => 'table_key' ,
'global' => 'name' ,
'filter' => 'filter_type'
);
// update the repeatable fields
2017-10-29 11:03:06 +00:00
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
2017-10-19 03:53:55 +00:00
// subform fields to target
$updaterT = array (
// subformfield => field => type_value
'join_view_table' => array ( 'view_table' => 'admin_view' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'dynamic_get' , $updaterT );
2017-03-27 12:38:51 +00:00
break ;
case 'layout' :
case 'template' :
// update the dynamic_get
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'dynamic_get' , 'dynamic_get' , $type );
2017-03-31 21:28:16 +00:00
// update the snippet
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'snippet' , 'snippet' , $type );
2017-03-27 12:38:51 +00:00
break ;
case 'custom_admin_view' :
case 'site_view' :
// update the main_get
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'main_get' , 'dynamic_get' , $type );
2017-03-27 12:38:51 +00:00
// update the dynamic_get
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'dynamic_get' , 'dynamic_get' , $type );
2017-03-27 12:38:51 +00:00
// update the custom_get
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'custom_get' , 'dynamic_get' , $type );
2017-03-31 21:28:16 +00:00
// update the snippet
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'snippet' , 'snippet' , $type );
2017-10-16 17:14:23 +00:00
// repeatable fields to update
$updaterR = array (
2017-10-19 03:53:55 +00:00
// repeatablefield => checker
2017-10-16 17:14:23 +00:00
'ajax_input' => 'value_name' ,
'custom_button' => 'name'
);
2017-10-19 03:53:55 +00:00
// update the repeatable fields
2017-10-29 11:03:06 +00:00
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
2017-03-27 12:38:51 +00:00
break ;
case 'admin_view' :
2017-10-29 16:07:37 +00:00
// set the getters anchors
$getter = array ( 'admin_view' => $item -> id );
2017-03-27 12:38:51 +00:00
// we must clear the demo content (since it was not moved as far as we know) TODO
2017-08-23 14:52:31 +00:00
if ( $item -> add_sql == 1 && $item -> source == 1 )
{
// only if it is mapped to a table
unset ( $item -> add_sql );
unset ( $item -> source );
unset ( $item -> addtables );
}
2017-10-16 17:14:23 +00:00
// update the addfields (old dataset)
2017-03-27 12:38:51 +00:00
if ( isset ( $item -> addfields ) && ComponentbuilderHelper :: checkJson ( $item -> addfields ))
{
2017-10-16 17:14:23 +00:00
// move the old data
2017-10-19 03:53:55 +00:00
$this -> setDivergedDataMover ( $item -> addfields , 'admin_fields' , 'addfields' , $getter );
2017-03-27 12:38:51 +00:00
}
2017-10-29 16:07:37 +00:00
// remove from this dataset
unset ( $item -> addfields );
2017-03-27 12:38:51 +00:00
// update the addlinked_views
if ( isset ( $item -> addlinked_views ) && ComponentbuilderHelper :: checkJson ( $item -> addlinked_views ))
{
2017-03-31 21:28:16 +00:00
$this -> updateAfter [ 'adminview' ][ $item -> id ] = $item -> id ; // addlinked_views
2017-03-27 12:38:51 +00:00
}
2017-10-19 03:53:55 +00:00
elseif ( isset ( $item -> addlinked_views ))
2017-03-27 12:38:51 +00:00
{
unset ( $item -> addlinked_views );
}
2017-10-16 17:14:23 +00:00
// update the addconditions (old dataset)
2017-10-19 03:53:55 +00:00
if ( isset ( $item -> addconditions ) && ComponentbuilderHelper :: checkJson ( $item -> addconditions ))
2017-03-27 12:38:51 +00:00
{
2017-10-16 17:14:23 +00:00
// move the old data
2017-10-19 03:53:55 +00:00
$this -> setDivergedDataMover ( $item -> addconditions , 'admin_fields_conditions' , 'addconditions' , $getter );
2017-03-27 12:38:51 +00:00
}
2017-10-29 16:07:37 +00:00
// remove from this dataset
unset ( $item -> addconditions );
2017-10-16 17:14:23 +00:00
// repeatable fields to update
$updaterR = array (
2017-10-19 03:53:55 +00:00
// repeatablefield => checker
2017-10-16 17:14:23 +00:00
'ajax_input' => 'value_name' ,
'custom_button' => 'name' ,
'addtables' => 'table' ,
'addlinked_views' => 'adminview' ,
'addtabs' => 'name' ,
'addpermissions' => 'action'
);
// update the repeatable fields
2017-10-29 11:03:06 +00:00
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
2017-03-27 12:38:51 +00:00
break ;
case 'joomla_component' :
2017-10-29 16:07:37 +00:00
// set the anchors getters
$getter = array ( 'joomla_component' => $item -> id );
2017-03-27 12:38:51 +00:00
// update the addconfig
if ( isset ( $item -> addconfig ) && ComponentbuilderHelper :: checkJson ( $item -> addconfig ))
{
2017-10-29 16:07:37 +00:00
// move the old data
$this -> setDivergedDataMover ( $item -> addconfig , 'component_config' , 'addconfig' , $getter );
2017-03-27 12:38:51 +00:00
}
2017-10-29 16:07:37 +00:00
// remove from this dataset
unset ( $item -> addconfig );
2017-03-27 12:38:51 +00:00
// update the addadmin_views
if ( isset ( $item -> addadmin_views ) && ComponentbuilderHelper :: checkJson ( $item -> addadmin_views ))
{
2017-10-29 16:07:37 +00:00
// move the old data
$this -> setDivergedDataMover ( $item -> addadmin_views , 'component_admin_views' , 'addadmin_views' , $getter );
2017-03-27 12:38:51 +00:00
}
2017-10-29 16:07:37 +00:00
// remove from this dataset
unset ( $item -> addadmin_views );
2017-03-27 12:38:51 +00:00
// update the addcustom_admin_views
if ( isset ( $item -> addcustom_admin_views ) && ComponentbuilderHelper :: checkJson ( $item -> addcustom_admin_views ))
{
2017-10-29 16:07:37 +00:00
// move the old data
$this -> setDivergedDataMover ( $item -> addcustom_admin_views , 'component_custom_admin_views' , 'addcustom_admin_views' , $getter );
2017-03-27 12:38:51 +00:00
}
2017-10-29 16:07:37 +00:00
// remove from this dataset
unset ( $item -> addcustom_admin_views );
2017-03-27 12:38:51 +00:00
// update the addsite_views
if ( isset ( $item -> addsite_views ) && ComponentbuilderHelper :: checkJson ( $item -> addsite_views ))
{
2017-10-29 16:07:37 +00:00
// move the old data
$this -> setDivergedDataMover ( $item -> addsite_views , 'component_site_views' , 'addsite_views' , $getter );
}
// remove from this dataset
unset ( $item -> addsite_views );
// update the version_update
if ( isset ( $item -> version_update ) && ComponentbuilderHelper :: checkJson ( $item -> version_update ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> version_update , 'component_updates' , 'version_update' , $getter );
}
// remove from this dataset
unset ( $item -> version_update );
// update the sql_tweak
if ( isset ( $item -> sql_tweak ) && ComponentbuilderHelper :: checkJson ( $item -> sql_tweak ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> sql_tweak , 'component_mysql_tweaks' , 'sql_tweak' , $getter );
}
// remove from this dataset
unset ( $item -> sql_tweak );
// update the addcustommenus
if ( isset ( $item -> addcustommenus ) && ComponentbuilderHelper :: checkJson ( $item -> addcustommenus ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> addcustommenus , 'component_custom_admin_menus' , 'addcustommenus' , $getter );
}
// remove from this dataset
unset ( $item -> addcustommenus );
// update the dashboard_tab
if ( isset ( $item -> dashboard_tab ) && ComponentbuilderHelper :: checkJson ( $item -> dashboard_tab ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> dashboard_tab , 'component_dashboard' , 'dashboard_tab' , $getter );
}
// remove from this dataset
unset ( $item -> dashboard_tab );
// update the php_dashboard_methods
if ( isset ( $item -> php_dashboard_methods ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> php_dashboard_methods , 'component_dashboard' , 'php_dashboard_methods' , $getter );
2017-03-27 12:38:51 +00:00
}
2017-10-29 16:07:37 +00:00
// remove from this dataset
unset ( $item -> php_dashboard_methods );
unset ( $item -> add_php_dashboard_methods );
// update the addfiles
if ( isset ( $item -> addfiles ) && ComponentbuilderHelper :: checkJson ( $item -> addfiles ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> addfiles , 'component_files_folders' , 'addfiles' , $getter );
}
// remove from this dataset
unset ( $item -> addfiles );
// update the addfolders
if ( isset ( $item -> addfolders ) && ComponentbuilderHelper :: checkJson ( $item -> addfolders ))
{
// move the old data
$this -> setDivergedDataMover ( $item -> addfolders , 'component_files_folders' , 'addfolders' , $getter );
}
// remove from this dataset
unset ( $item -> addfolders );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addcontributors' => 'name'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
break ;
case 'component_admin_views' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addadmin_views' => 'adminview'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
// subform fields to target
$updaterT = array (
// subformfield => array( field => type_value )
'addadmin_views' => array ( 'adminview' => 'admin_view' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'component_admin_views' , $updaterT );
break ;
case 'component_site_views' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addsite_views' => 'siteview'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
// subform fields to target
$updaterT = array (
// subformfield => array( field => type_value )
'addsite_views' => array ( 'siteview' => 'site_view' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'component_site_views' , $updaterT );
break ;
case 'component_custom_admin_views' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addcustom_admin_views' => 'customadminview'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
// subform fields to target
$updaterT = array (
// subformfield => array( field => type_value )
'addcustom_admin_views' => array ( 'customadminview' => 'custom_admin_view' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'component_custom_admin_views' , $updaterT );
break ;
case 'component_updates' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'version_update' => 'version'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
break ;
case 'component_mysql_tweaks' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'sql_tweak' => 'adminview'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
// subform fields to target
$updaterT = array (
// subformfield => array( field => type_value )
'sql_tweak' => array ( 'adminview' => 'admin_view' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'component_mysql_tweaks' , $updaterT );
break ;
case 'component_custom_admin_menus' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addcustommenus' => 'name'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
// subform fields to target
$updaterT = array (
// subformfield => array( field => type_value )
'addcustommenus' => array ( 'before' => 'admin_view' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'component_custom_admin_menus' , $updaterT );
break ;
case 'component_config' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addconfig' => 'field'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
// subform fields to target
$updaterT = array (
// subformfield => array( field => type_value )
'addconfig' => array ( 'field' => 'field' )
);
// update the subform ids
$this -> updateSubformsIDs ( $item , 'component_config' , $updaterT );
break ;
case 'component_dashboard' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'dashboard_tab' => 'name'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
break ;
case 'component_files_folders' :
// update the joomla_component ID where needed
$item = $this -> setNewID ( $item , 'joomla_component' , 'joomla_component' , $type );
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addfiles' => 'file' ,
'addfolders' => 'folder'
);
// update the repeatable fields
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
2017-03-27 12:38:51 +00:00
break ;
case 'custom_code' :
// update the component ID where needed
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'component' , 'joomla_component' , $type );
2017-03-27 12:38:51 +00:00
break ;
2017-04-06 21:40:22 +00:00
case 'language_translation' :
// update the component ID where needed
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'component' , 'joomla_component' , $type );
2017-04-06 21:40:22 +00:00
// load the local components if found
if ( isset ( $item -> localComponents ) && ComponentbuilderHelper :: checkJson ( $item -> localComponents ))
{
2017-10-20 16:17:46 +00:00
$components = array ();
if ( isset ( $item -> components ) && ComponentbuilderHelper :: checkJson ( $item -> components ))
2017-04-06 21:40:22 +00:00
{
2017-10-20 16:17:46 +00:00
$components = json_decode ( $item -> components , true );
2017-04-06 21:40:22 +00:00
}
$localComponents = json_decode ( $item -> localComponents , true );
foreach ( $localComponents as $lid )
{
if ( ! is_numeric ( $lid ))
{
continue ;
}
// add if not already there
if ( ! in_array ( $lid , $components ))
{
$components [] = $lid ;
}
}
}
// remove the localComponents
if ( isset ( $item -> localComponents ))
{
unset ( $item -> localComponents );
}
// load it back
if ( isset ( $components ) && ComponentbuilderHelper :: checkArray ( $components ))
{
// load it back
$item -> components = json_encode ( array_values ( $components ));
}
// merge the translations where needed
if ( isset ( $item -> translation ) && isset ( $item -> localTranslation )
2017-09-18 00:18:23 +00:00
&& ComponentbuilderHelper :: checkJson ( $item -> translation )
2017-04-06 21:40:22 +00:00
&& ComponentbuilderHelper :: checkJson ( $item -> localTranslation ))
{
2017-09-18 00:18:23 +00:00
$newTranslations = json_decode ( $item -> translation , true );
$localTranslations = json_decode ( $item -> localTranslation , true ); // always the new format
$translations = array ();
$pointer = 0 ;
$checker = array ();
// okay we have the old format lets merge on that basis
if ( isset ( $newTranslations [ 'translation' ]))
2017-04-06 21:40:22 +00:00
{
2017-09-18 00:18:23 +00:00
foreach ( $localTranslations as $value )
2017-04-06 21:40:22 +00:00
{
2017-09-18 00:18:23 +00:00
// only keep old translation if the new does not have this translation & language
if ( ! in_array ( $value [ 'language' ], $newTranslations [ 'language' ]))
{
$translations [ 'translation' . $pointer ] = array ( 'translation' => $value [ 'translation' ], 'language' => $value [ 'language' ]);
$pointer ++ ;
}
}
foreach ( $newTranslations [ 'translation' ] as $nr => $newTrans )
{
// now convert the new translation array
$translations [ 'translation' . $pointer ] = array ( 'translation' => $newTrans , 'language' => $newTranslations [ 'language' ][ $nr ]);
$pointer ++ ;
}
}
// okay this is the new format lets merge on that basis
elseif ( ComponentbuilderHelper :: checkArray ( $newTranslations ))
{
$translations = $newTranslations ;
$pointer = count ( $translations );
foreach ( $localTranslations as $value )
{
$keepLocal = true ;
foreach ( $newTranslations as $newValue )
{
// only keep old translation if the new does not have this translation & language
if ( $value [ 'language' ] === $newValue [ 'language' ])
{
$keepLocal = false ;
}
}
if ( $keepLocal )
{
$translations [ 'translation' . $pointer ] = array ( 'translation' => $value [ 'translation' ], 'language' => $value [ 'language' ]);
$pointer ++ ;
}
2017-04-06 21:40:22 +00:00
}
}
2017-09-18 00:18:23 +00:00
// okay seem to only have local translations
elseif ( ComponentbuilderHelper :: checkArray ( $localTranslations ))
{
$translations = $localTranslations ;
}
// only update if we have translations
if ( ComponentbuilderHelper :: checkArray ( $translations ))
{
$item -> translation = json_encode ( $translations );
}
2017-04-06 21:40:22 +00:00
}
2017-09-18 00:18:23 +00:00
elseif ( isset ( $item -> localTranslation ) && ComponentbuilderHelper :: checkJson ( $item -> localTranslation ))
2017-04-06 21:40:22 +00:00
{
$item -> translation = $item -> localTranslation ;
}
// remove the localTranslation
if ( isset ( $item -> localTranslation ))
{
unset ( $item -> localTranslation );
}
break ;
2017-10-19 03:53:55 +00:00
case 'admin_fields' :
case 'admin_fields_conditions' :
// update the admin_view ID where needed
2017-10-20 16:17:46 +00:00
$item = $this -> setNewID ( $item , 'admin_view' , 'admin_view' , $type );
// set the updater
2017-10-19 03:53:55 +00:00
if ( 'admin_fields' === $type )
{
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addfields' => 'field'
);
// subform fields to target
$updaterT = array (
// subformfield => field => type_value
'addfields' => array ( 'field' => 'field' )
);
// little tweak... oops
if ( isset ( $item -> addconditions ))
{
unset ( $item -> addconditions );
}
}
else
{
// repeatable fields to update
$updaterR = array (
// repeatablefield => checker
'addconditions' => 'target_field'
);
// subform fields to target
$updaterT = array (
// subformfield => field => type_value
'addconditions' => array ( 'target_field' => 'field' , 'match_field' => 'field' )
);
}
// update the repeatable fields
2017-10-29 11:03:06 +00:00
$item = ComponentbuilderHelper :: convertRepeatableFields ( $item , $updaterR );
2017-10-19 03:53:55 +00:00
// update the subform ids
$this -> updateSubformsIDs ( $item , $type , $updaterT );
2017-03-27 12:38:51 +00:00
}
// final action prep
switch ( $action )
{
case 'update' :
// set values to follow the update conventions
if ( isset ( $item -> created_by ))
{
unset ( $item -> created_by );
}
2017-08-23 20:38:09 +00:00
$item -> modified_by = $this -> user -> id ;
$item -> modified = $this -> today ;
2017-03-27 12:38:51 +00:00
// return the item
return $item ;
break ;
case 'add' :
// remove the ID
2017-10-20 16:17:46 +00:00
if ( isset ( $item -> id ))
{
unset ( $item -> id );
}
2017-03-27 12:38:51 +00:00
// set values to follow the adding conventions
2017-08-23 20:38:09 +00:00
$item -> created_by = $this -> user -> id ;
$item -> modified_by = $this -> user -> id ;
$item -> modified = $this -> today ;
2017-03-27 12:38:51 +00:00
// return the item
return $item ;
break ;
}
return false ;
}
2017-10-19 03:53:55 +00:00
/**
* Set the data that should be moved
*
* @ param array / json $values The values / data to move
* @ param string $table The table to move the values to
* @ param string $type The type of values
* @ param array $getters The get values used to anchor the values to the new table
*
* @ return bool
*/
protected function setDivergedDataMover ( $values , $table , $type , $getters )
{
// we need to move this to the new $table based on anchors
if ( ComponentbuilderHelper :: checkArray ( $getters ))
{
if ( ! isset ( $this -> divergedDataMover [ $table ]))
{
$this -> divergedDataMover [ $table ] = array ();
}
// set unique key
$uniqueKey = md5 ( serialize ( $getters ));
if ( ! isset ( $this -> divergedDataMover [ $table ][ $uniqueKey ]))
{
$this -> divergedDataMover [ $table ][ $uniqueKey ] = new stdClass ;
foreach ( $getters as $name => $value )
{
$this -> divergedDataMover [ $table ][ $uniqueKey ] -> { $name } = $value ;
}
}
// add the data to the mover
$this -> divergedDataMover [ $table ][ $uniqueKey ] -> { $type } = $values ;
// success
return true ;
}
$this -> app -> enqueueMessage ( JText :: sprintf ( 'COM_COMPONENTBUILDER_WE_FAILED_TO_MOVE_BSB' , ComponentbuilderHelper :: safeString ( $type , 'Ww' ) . ' to (' . ComponentbuilderHelper :: safeString ( $table , 'w' ) . ')' ), 'warning' );
// failure
return false ;
}
2017-03-31 21:28:16 +00:00
/**
2017-09-18 02:20:50 +00:00
* Check if a field has multiple fields
2017-03-31 21:28:16 +00:00
*
* @ param string $typeID The type ID
*
* @ return bool true on success
*
*/
2017-09-18 02:20:50 +00:00
protected function checkMultiFields ( $typeID )
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
if ( isset ( $this -> isMultiple [ $typeID ]))
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
return $this -> isMultiple [ $typeID ];
2017-03-31 21:28:16 +00:00
}
elseif ( $type = $this -> getFieldType ( $typeID ))
{
2017-09-18 02:20:50 +00:00
if ( 'repeatable' === $type || 'subform' === $type )
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
$this -> isMultiple [ $typeID ] = true ;
}
else
{
$this -> isMultiple [ $typeID ] = false ;
2017-03-31 21:28:16 +00:00
}
2017-09-18 02:20:50 +00:00
return $this -> isMultiple [ $typeID ];
2017-03-31 21:28:16 +00:00
}
return false ;
}
/**
* Get the field type
*
* @ param string $id The field type id
*
* @ return string field type
*
*/
protected function getFieldType ( $id )
{
if ( ! isset ( $this -> fieldTypes [ $id ]))
{
$properties = ComponentbuilderHelper :: getVar ( 'fieldtype' , $id , 'id' , 'properties' );
if ( ComponentbuilderHelper :: checkJson ( $properties ))
{
$properties = json_decode ( $properties , true );
2017-10-06 14:53:22 +00:00
// check if this is old values for repeatable fields
if ( isset ( $properties [ 'name' ]))
{
2017-10-29 11:03:06 +00:00
$properties = ComponentbuilderHelper :: convertRepeatable ( $properties , 'properties' );
2017-10-06 14:53:22 +00:00
}
// now check to find type
2017-09-18 02:20:50 +00:00
foreach ( $properties as $property )
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
if ( 'type' === $property [ 'name' ])
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
if ( isset ( $property [ 'example' ]) && ComponentbuilderHelper :: checkString ( $property [ 'example' ]))
2017-03-31 21:28:16 +00:00
{
2017-09-18 02:20:50 +00:00
$this -> fieldTypes [ $id ] = $property [ 'example' ];
break ;
2017-03-31 21:28:16 +00:00
}
}
}
}
// if not found
if ( ! isset ( $this -> fieldTypes [ $id ]) && $name = ComponentbuilderHelper :: getVar ( 'fieldtype' , $id , 'id' , 'name' ))
{
$this -> fieldTypes [ $id ] = ComponentbuilderHelper :: safeString ( $name );
}
}
// return the type
if ( isset ( $this -> fieldTypes [ $id ]))
{
return $this -> fieldTypes [ $id ];
}
return false ;
}
2017-03-27 12:38:51 +00:00
/**
* Update the local item
*
2017-10-16 17:14:23 +00:00
* @ param object $item The item to update
* @ param string $type The type of values
* @ param bool $canState The switch to set state
2017-03-27 12:38:51 +00:00
*
* @ return mixed false on failure
* ID int on success
*
**/
2017-08-23 20:38:09 +00:00
protected function updateLocalItem ( & $item , & $type , & $canState )
2017-03-27 12:38:51 +00:00
{
// prep the item
2017-08-23 20:38:09 +00:00
if ( $update = $this -> prepItem ( $item , $type , 'update' ))
2017-03-27 12:38:51 +00:00
{
// remove the published state if not allowed to edit it
if ( ! $canState && isset ( $update -> published ))
{
unset ( $update -> published );
}
// update the item
2017-08-23 20:38:09 +00:00
if ( $result = $this -> _db -> updateObject ( '#__componentbuilder_' . $type , $update , 'id' ))
2017-03-27 12:38:51 +00:00
{
// return current ID
return $update -> id ;
}
}
return false ;
}
/**
* add the local item
*
* @ param object $item The item to update
* @ param string $type The type of values
*
* @ return mixed false on failure
* ID int on success
*
**/
2017-08-23 20:38:09 +00:00
protected function addLocalItem ( & $item , & $type )
2017-03-27 12:38:51 +00:00
{
// prep the item
2017-08-23 20:38:09 +00:00
if ( $add = $this -> prepItem ( $item , $type , 'add' ))
2017-03-27 12:38:51 +00:00
{
// insert/add the item
2017-08-23 20:38:09 +00:00
if ( $result = $this -> _db -> insertObject ( '#__componentbuilder_' . $type , $add ))
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$aId = $this -> _db -> insertid ();
2017-03-27 12:38:51 +00:00
// make sure the access of asset is set
ComponentbuilderHelper :: setAsset ( $aId , $type );
// set new ID
return $aId ;
}
}
return false ;
}
/**
* Get the local item
*
* @ param object $item The item to get
* @ param string $type The type of values
* @ param bool $retry The retry switch
* @ param bool $get The query get switch
*
* @ return mixed false on failure
2017-08-23 20:38:09 +00:00
* ID int on success
2017-03-27 12:38:51 +00:00
*
**/
2017-08-23 20:38:09 +00:00
protected function getLocalItem ( & $item , & $type , $retry = false , $get = 1 )
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$query = $this -> _db -> getQuery ( true );
2017-05-20 07:36:26 +00:00
$query -> select ( 'a.*' );
2017-08-23 20:38:09 +00:00
$query -> from ( $this -> _db -> quoteName ( '#__componentbuilder_' . $type , 'a' ));
2017-10-19 03:53:55 +00:00
// only run query if where is set
$runQuery = false ;
if ( $get == 1 && isset ( $item -> created ) && isset ( $item -> id ))
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.created' ) . ' = ' . $this -> _db -> quote ( $item -> created ));
$query -> where ( $this -> _db -> quoteName ( 'a.id' ) . ' = ' . ( int ) $item -> id );
2017-10-19 03:53:55 +00:00
$runQuery = true ;
2017-03-27 12:38:51 +00:00
}
elseif ( componentbuilderHelper :: checkArray ( $get ))
{
foreach ( $get as $field )
{
2017-10-19 03:53:55 +00:00
if ( isset ( $item -> { $field }))
2017-03-27 12:38:51 +00:00
{
2017-04-01 13:59:54 +00:00
// set the value
2017-10-19 03:53:55 +00:00
$value = $item -> { $field };
2017-04-01 13:59:54 +00:00
// check if we have special value
if ( $this -> specialValue && ComponentbuilderHelper :: checkArray ( $this -> specialValue ) && isset ( $this -> specialValue [ $field ]))
2017-03-27 12:38:51 +00:00
{
2017-04-01 13:59:54 +00:00
$value = $this -> specialValue [ $field ];
2017-03-27 12:38:51 +00:00
}
2017-04-01 13:59:54 +00:00
// load to query
if ( is_numeric ( $value ) && is_int ( $value ))
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.' . $field ) . ' = ' . ( int ) $value );
2017-03-27 12:38:51 +00:00
}
2017-04-01 13:59:54 +00:00
elseif ( is_numeric ( $value ) && is_float ( $value ))
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.' . $field ) . ' = ' . ( float ) $value );
2017-04-01 13:59:54 +00:00
}
elseif ( componentbuilderHelper :: checkString ( $value )) // do not allow empty strings (since it could be major mis match)
{
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.' . $field ) . ' = ' . $this -> _db -> quote ( $value ));
2017-03-27 12:38:51 +00:00
}
else
{
return false ;
}
2017-10-19 03:53:55 +00:00
$runQuery = true ;
2017-03-27 12:38:51 +00:00
}
else
{
return false ;
}
}
}
2017-10-19 03:53:55 +00:00
elseif ( isset ( $item -> { $get }) && componentbuilderHelper :: checkString ( $item -> { $get })) // do not allow empty strings (since it could be major mis match)
2017-04-01 13:59:54 +00:00
{
// set the value
2017-10-19 03:53:55 +00:00
$value = $item -> { $get };
2017-04-01 13:59:54 +00:00
// check if we have special value
if ( $this -> specialValue && ComponentbuilderHelper :: checkArray ( $this -> specialValue ) && isset ( $this -> specialValue [ $get ]))
{
$value = $this -> specialValue [ $get ];
}
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.' . $get ) . ' = ' . $this -> _db -> quote ( $value ));
2017-10-19 03:53:55 +00:00
$runQuery = true ;
2017-04-01 13:59:54 +00:00
}
2017-10-19 03:53:55 +00:00
elseif ( isset ( $item -> { $get }) && is_numeric ( $item -> { $get }))
2017-03-27 12:38:51 +00:00
{
2017-04-01 13:59:54 +00:00
// set the value
2017-10-19 03:53:55 +00:00
$value = $item -> { $get };
2017-04-01 13:59:54 +00:00
// check if we have special value
if ( $this -> specialValue && ComponentbuilderHelper :: checkArray ( $this -> specialValue ) && isset ( $this -> specialValue [ $get ]))
{
$value = $this -> specialValue [ $get ];
}
// load to query
if ( is_int ( $value ))
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.' . $get ) . ' = ' . ( int ) $value );
2017-03-27 12:38:51 +00:00
}
2017-04-01 13:59:54 +00:00
elseif ( is_float ( $value ))
2017-03-27 12:38:51 +00:00
{
2017-08-23 20:38:09 +00:00
$query -> where ( $this -> _db -> quoteName ( 'a.' . $get ) . ' = ' . ( float ) $value );
2017-03-27 12:38:51 +00:00
}
else
{
return false ; // really not needed but who knows for sure...
}
2017-10-19 03:53:55 +00:00
$runQuery = true ;
2017-03-27 12:38:51 +00:00
}
2017-10-19 03:53:55 +00:00
// since where has been set run the query
if ( $runQuery )
2017-03-27 12:38:51 +00:00
{
2017-10-19 03:53:55 +00:00
// see if we get an item
$this -> _db -> setQuery ( $query );
$this -> _db -> execute ();
if ( $this -> _db -> getNumRows ())
{
return $this -> _db -> loadObject ();
}
2017-03-27 12:38:51 +00:00
}
2017-10-19 03:53:55 +00:00
// retry to get the item
if ( $retry )
2017-03-27 12:38:51 +00:00
{
$retryAgain = false ;
2017-04-01 13:59:54 +00:00
$this -> specialValue = false ;
2017-03-27 12:38:51 +00:00
// set the getter
switch ( $type )
{
2017-10-16 17:14:23 +00:00
case 'admin_fields' :
case 'admin_fields_conditions' :
// get by admin_view (since there should only be one of each name)
2017-10-19 03:53:55 +00:00
$getter = array ( 'admin_view' );
$this -> specialValue = array ();
2017-10-20 16:17:46 +00:00
$this -> specialValue [ 'admin_view' ] = $this -> newID [ 'admin_view' ][( int ) $item -> admin_view ];
2017-10-16 17:14:23 +00:00
break ;
2017-03-27 12:38:51 +00:00
case 'fieldtype' :
// get by name (since there should only be one of each name)
$getter = 'name' ;
break ;
case 'field' :
// get by name and xml to target correct field
2017-04-01 13:59:54 +00:00
if ( $retry == 2 )
{
// get by id name..
$getter = array ( 'name' , 'datatype' , 'store' , 'indexes' , 'null_switch' , 'xml' );
}
else
{
// get by id name..
$getter = array ( 'name' , 'datatype' , 'store' , 'indexes' , 'null_switch' );
// lets try to add the fieldtype
2017-10-20 16:17:46 +00:00
if ( isset ( $item -> fieldtype ) && is_numeric ( $item -> fieldtype ) && $item -> fieldtype > 0 && isset ( $this -> newID [ 'fieldtype' ][( int ) $item -> fieldtype ]) && $this -> newID [ 'fieldtype' ][( int ) $item -> fieldtype ] > 0 )
2017-04-01 13:59:54 +00:00
{
$getter [] = 'fieldtype' ;
$this -> specialValue = array ();
2017-10-20 16:17:46 +00:00
$this -> specialValue [ 'fieldtype' ] = $this -> newID [ 'fieldtype' ][( int ) $item -> fieldtype ];
2017-04-01 13:59:54 +00:00
$retryAgain = 2 ;
}
else
{
$getter [] = 'xml' ;
}
}
2017-03-27 12:38:51 +00:00
break ;
case 'site_view' :
case 'custom_admin_view' :
// get by name, system_name and codename
2017-04-01 13:59:54 +00:00
$getter = array ( 'name' , 'system_name' , 'codename' );
// lets try to add the main_get
2017-10-20 16:17:46 +00:00
if ( isset ( $item -> main_get ) && is_numeric ( $item -> main_get ) && $item -> main_get > 0 && isset ( $this -> newID [ 'dynamic_get' ][( int ) $item -> main_get ]) && $this -> newID [ 'dynamic_get' ][( int ) $item -> main_get ] > 0 )
2017-04-01 13:59:54 +00:00
{
$getter [] = 'main_get' ;
$this -> specialValue = array ();
2017-10-20 16:17:46 +00:00
$this -> specialValue [ 'main_get' ] = $this -> newID [ 'dynamic_get' ][( int ) $item -> main_get ];
2017-04-01 13:59:54 +00:00
}
2017-03-27 12:38:51 +00:00
break ;
case 'template' :
case 'layout' :
// get by code name (since there should only be one)
$getter = 'alias' ;
break ;
2017-03-31 21:28:16 +00:00
case 'snippet' :
// get by snippet (since there should only be one snippet like that)
if ( $retry == 2 )
{
$getter = array ( 'name' , 'snippet' , 'url' , 'type' , 'heading' );
}
else
{
// get by id name..
$getter = array ( 'id' , 'name' , 'snippet' , 'url' , 'type' , 'heading' );
$retryAgain = 2 ;
}
break ;
2017-03-27 12:38:51 +00:00
case 'custom_code' :
// get by code to insure its correctly matched
$getter = array ( 'code' , 'comment_type' , 'target' );
// add some more advanced search
if ( isset ( $item -> path ) && ComponentbuilderHelper :: checkString ( $item -> path ))
{
$getter [] = 'path' ;
}
elseif ( isset ( $item -> function_name ) && ComponentbuilderHelper :: checkString ( $item -> function_name ))
{
$getter [] = 'function_name' ;
}
elseif ( isset ( $item -> hashtarget ) && ComponentbuilderHelper :: checkString ( $item -> hashtarget ))
{
$getter [] = 'hashtarget' ;
}
break ;
case 'dynamic_get' :
if ( $retry == 2 )
{
// get by name ...
$getter = array ( 'name' , 'gettype' , 'main_source' ); // risky will look at this again
// add some more advanced search
if ( isset ( $item -> main_source ) && $item -> main_source == 1 && isset ( $item -> view_selection ) && ComponentbuilderHelper :: checkString ( $item -> view_selection ))
{
$getter [] = 'view_selection' ;
}
elseif ( isset ( $item -> main_source ) && $item -> main_source == 2 && isset ( $item -> db_selection ) && ComponentbuilderHelper :: checkString ( $item -> db_selection ))
{
$getter [] = 'db_selection' ;
}
elseif ( isset ( $item -> main_source ) && $item -> main_source == 3 && isset ( $item -> php_custom_get ) && ComponentbuilderHelper :: checkString ( $item -> php_custom_get ))
{
$getter [] = 'php_custom_get' ;
}
// add some extra
if ( isset ( $item -> getcustom ) && ComponentbuilderHelper :: checkString ( $item -> getcustom ))
{
$getter [] = 'getcustom' ;
}
}
else
{
// get by id name gettype and main_source
$getter = array ( 'id' , 'name' , 'gettype' , 'main_source' ); // risky will look at this again
$retryAgain = 2 ;
}
break ;
case 'admin_view' :
if ( $retry == 2 )
{
// get by name ...
$getter = array ( 'name_list' , 'name_single' , 'short_description' , 'system_name' ); // risky will look at this again
}
else
{
// get by id name ...
$getter = array ( 'id' , 'name_list' , 'name_single' , 'short_description' , 'system_name' ); // risky will look at this again
$retryAgain = 2 ;
}
break ;
case 'joomla_component' :
if ( $retry == 2 )
{
// get by name ...
$getter = array ( 'name' , 'name_code' , 'short_description' , 'author' , 'email' , 'component_version' , 'companyname' , 'system_name' , 'website' , 'bom' , 'copyright' , 'license' ); // risky will look at this again
}
else
{
// get by id name ...
$getter = array ( 'id' , 'name' , 'name_code' , 'short_description' , 'author' , 'component_version' , 'companyname' , 'system_name' ); // risky will look at this again
$retryAgain = 2 ;
}
break ;
2017-10-29 16:07:37 +00:00
case 'component_admin_views' :
case 'component_site_views' :
case 'component_custom_admin_views' :
case 'component_updates' :
case 'component_mysql_tweaks' :
case 'component_custom_admin_menus' :
case 'component_config' :
case 'component_dashboard' :
case 'component_files_folders' :
// get by admin_view (since there should only be one of each name)
$getter = array ( 'joomla_component' );
$this -> specialValue = array ();
$this -> specialValue [ 'joomla_component' ] = $this -> newID [ 'joomla_component' ][( int ) $item -> joomla_component ];
break ;
2017-04-06 21:40:22 +00:00
case 'language_translation' :
// get by English translation since there should just be one
$getter = 'entranslation' ;
break ;
case 'language' :
// get by language tag since there should just be one
$getter = 'langtag' ;
break ;
2017-03-27 12:38:51 +00:00
default :
// can't be found so return false
return false ;
break ;
}
// we try again
2017-08-23 20:38:09 +00:00
return $this -> getLocalItem ( $item , $type , $retryAgain , $getter );
2017-03-27 12:38:51 +00:00
}
return false ;
}
/**
* Save the data from the file to the database
*
* @ param array $data The values to save
*
* @ return boolean false on failure
*
**/
protected function saveBasic ( $data )
{
// import the data if there is any
if ( ComponentbuilderHelper :: checkArray ( $data [ 'array' ]))
{
// get user object
2017-08-23 20:38:09 +00:00
$this -> user = JFactory :: getUser ();
2017-03-27 12:38:51 +00:00
// remove header if it has headers
$id_key = $data [ 'target_headers' ][ 'id' ];
$published_key = $data [ 'target_headers' ][ 'published' ];
$ordering_key = $data [ 'target_headers' ][ 'ordering' ];
// get the first array set
$firstSet = reset ( $data [ 'array' ]);
// check if first array is a header array and remove if true
if ( $firstSet [ $id_key ] == 'id' || $firstSet [ $published_key ] == 'published' || $firstSet [ $ordering_key ] == 'ordering' )
{
array_shift ( $data [ 'array' ]);
}
// make sure there is still values in array and that it was not only headers
2017-08-23 20:38:09 +00:00
if ( ComponentbuilderHelper :: checkArray ( $data [ 'array' ]) && $this -> user -> authorise ( $this -> dataType . '.import' , 'com_componentbuilder' ) && $this -> user -> authorise ( 'core.import' , 'com_componentbuilder' ))
2017-03-27 12:38:51 +00:00
{
// set target.
$target = array_flip ( $data [ 'target_headers' ]);
// set some defaults
$todayDate = JFactory :: getDate () -> toSql ();
// get global action permissions
$canDo = ComponentbuilderHelper :: getActions ( $this -> dataType );
$canEdit = $canDo -> get ( 'core.edit' );
$canState = $canDo -> get ( 'core.edit.state' );
$canCreate = $canDo -> get ( 'core.create' );
$hasAlias = $this -> getAliasesUsed ( $this -> dataType );
// prosses the data
foreach ( $data [ 'array' ] as $row )
{
$found = false ;
if ( isset ( $row [ $id_key ]) && is_numeric ( $row [ $id_key ]) && $row [ $id_key ] > 0 )
{
// raw items import & update!
2017-08-23 20:38:09 +00:00
$query = $this -> _db -> getQuery ( true );
2017-03-27 12:38:51 +00:00
$query
-> select ( 'version' )
2017-08-23 20:38:09 +00:00
-> from ( $this -> _db -> quoteName ( '#__componentbuilder_' . $this -> dataType ))
-> where ( $this -> _db -> quoteName ( 'id' ) . ' = ' . $this -> _db -> quote ( $row [ $id_key ]));
2017-03-27 12:38:51 +00:00
// Reset the query using our newly populated query object.
2017-08-23 20:38:09 +00:00
$this -> _db -> setQuery ( $query );
$this -> _db -> execute ();
$found = $this -> _db -> getNumRows ();
2017-03-27 12:38:51 +00:00
}
if ( $found && $canEdit )
{
// update item
$id = $row [ $id_key ];
2017-08-23 20:38:09 +00:00
$version = $this -> _db -> loadResult ();
2017-03-27 12:38:51 +00:00
// reset all buckets
2017-08-23 20:38:09 +00:00
$query = $this -> _db -> getQuery ( true );
2017-03-27 12:38:51 +00:00
$fields = array ();
// Fields to update.
foreach ( $row as $key => $cell )
{
// ignore column
if ( 'IGNORE' == $target [ $key ])
{
continue ;
}
// update modified
if ( 'modified_by' == $target [ $key ])
{
continue ;
}
// update modified
if ( 'modified' == $target [ $key ])
{
continue ;
}
// update version
if ( 'version' == $target [ $key ])
{
$cell = ( int ) $version + 1 ;
}
// verify publish authority
if ( 'published' == $target [ $key ] && ! $canState )
{
continue ;
}
// set to update array
if ( in_array ( $key , $data [ 'target_headers' ]) && is_numeric ( $cell ))
{
2017-08-23 20:38:09 +00:00
$fields [] = $this -> _db -> quoteName ( $target [ $key ]) . ' = ' . $cell ;
2017-03-27 12:38:51 +00:00
}
elseif ( in_array ( $key , $data [ 'target_headers' ]) && is_string ( $cell ))
{
2017-08-23 20:38:09 +00:00
$fields [] = $this -> _db -> quoteName ( $target [ $key ]) . ' = ' . $this -> _db -> quote ( $cell );
2017-03-27 12:38:51 +00:00
}
elseif ( in_array ( $key , $data [ 'target_headers' ]) && is_null ( $cell ))
{
// if import data is null then set empty
2017-08-23 20:38:09 +00:00
$fields [] = $this -> _db -> quoteName ( $target [ $key ]) . " = '' " ;
2017-03-27 12:38:51 +00:00
}
}
// load the defaults
2017-08-23 20:38:09 +00:00
$fields [] = $this -> _db -> quoteName ( 'modified_by' ) . ' = ' . $this -> _db -> quote ( $this -> user -> id );
$fields [] = $this -> _db -> quoteName ( 'modified' ) . ' = ' . $this -> _db -> quote ( $todayDate );
2017-03-27 12:38:51 +00:00
// Conditions for which records should be updated.
$conditions = array (
2017-08-23 20:38:09 +00:00
$this -> _db -> quoteName ( 'id' ) . ' = ' . $id
2017-03-27 12:38:51 +00:00
);
2017-08-23 20:38:09 +00:00
$query -> update ( $this -> _db -> quoteName ( '#__componentbuilder_' . $this -> dataType )) -> set ( $fields ) -> where ( $conditions );
$this -> _db -> setQuery ( $query );
$this -> _db -> execute ();
2017-03-27 12:38:51 +00:00
}
elseif ( $canCreate )
{
// insert item
2017-08-23 20:38:09 +00:00
$query = $this -> _db -> getQuery ( true );
2017-03-27 12:38:51 +00:00
// reset all buckets
$columns = array ();
$values = array ();
$version = false ;
// Insert columns. Insert values.
foreach ( $row as $key => $cell )
{
// ignore column
if ( 'IGNORE' == $target [ $key ])
{
continue ;
}
// remove id
if ( 'id' == $target [ $key ])
{
continue ;
}
// update created
if ( 'created_by' == $target [ $key ])
{
continue ;
}
// update created
if ( 'created' == $target [ $key ])
{
continue ;
}
// Make sure the alias is incremented
if ( 'alias' == $target [ $key ])
{
$cell = $this -> getAlias ( $cell , $this -> dataType );
}
// update version
if ( 'version' == $target [ $key ])
{
$cell = 1 ;
$version = true ;
}
// set to insert array
if ( in_array ( $key , $data [ 'target_headers' ]) && is_numeric ( $cell ))
{
$columns [] = $target [ $key ];
$values [] = $cell ;
}
elseif ( in_array ( $key , $data [ 'target_headers' ]) && is_string ( $cell ))
{
$columns [] = $target [ $key ];
2017-08-23 20:38:09 +00:00
$values [] = $this -> _db -> quote ( $cell );
2017-03-27 12:38:51 +00:00
}
elseif ( in_array ( $key , $data [ 'target_headers' ]) && is_null ( $cell ))
{
// if import data is null then set empty
$columns [] = $target [ $key ];
$values [] = " '' " ;
}
}
// load the defaults
$columns [] = 'created_by' ;
2017-08-23 20:38:09 +00:00
$values [] = $this -> _db -> quote ( $this -> user -> id );
2017-03-27 12:38:51 +00:00
$columns [] = 'created' ;
2017-08-23 20:38:09 +00:00
$values [] = $this -> _db -> quote ( $todayDate );
2017-03-27 12:38:51 +00:00
if ( ! $version )
{
$columns [] = 'version' ;
$values [] = 1 ;
}
// Prepare the insert query.
$query
2017-08-23 20:38:09 +00:00
-> insert ( $this -> _db -> quoteName ( '#__componentbuilder_' . $this -> dataType ))
-> columns ( $this -> _db -> quoteName ( $columns ))
2017-03-27 12:38:51 +00:00
-> values ( implode ( ',' , $values ));
// Set the query using our newly populated query object and execute it.
2017-08-23 20:38:09 +00:00
$this -> _db -> setQuery ( $query );
$done = $this -> _db -> execute ();
2017-03-27 12:38:51 +00:00
if ( $done )
{
2017-08-23 20:38:09 +00:00
$aId = $this -> _db -> insertid ();
2017-03-27 12:38:51 +00:00
// make sure the access of asset is set
ComponentbuilderHelper :: setAsset ( $aId , $this -> dataType );
}
}
else
{
return false ;
}
}
return true ;
}
}
return false ;
}
protected function getAlias ( $name , $type = false )
{
// sanitize the name to an alias
if ( JFactory :: getConfig () -> get ( 'unicodeslugs' ) == 1 )
{
$alias = JFilterOutput :: stringURLUnicodeSlug ( $name );
}
else
{
$alias = JFilterOutput :: stringURLSafe ( $name );
}
// must be a uniqe alias
if ( $type )
{
return $this -> getUniqe ( $alias , 'alias' , $type );
}
return $alias ;
}
/**
* Method to generate a uniqe value .
*
* @ param string $field name .
* @ param string $value data .
* @ param string $type table .
*
* @ return string New value .
*/
protected function getUniqe ( $value , $field , $type )
{
// insure the filed is always uniqe
while ( isset ( $this -> uniqeValueArray [ $type ][ $field ][ $value ]))
{
$value = JString :: increment ( $value , 'dash' );
}
$this -> uniqeValueArray [ $type ][ $field ][ $value ] = $value ;
return $value ;
}
protected function getAliasesUsed ( $table )
{
// Get a db connection.
$db = JFactory :: getDbo ();
// first we check if there is a alias column
$columns = $db -> getTableColumns ( '#__componentbuilder_' . $table );
if ( isset ( $columns [ 'alias' ])){
// Create a new query object.
$query = $db -> getQuery ( true );
$query -> select ( $db -> quoteName ( array ( 'alias' )));
$query -> from ( $db -> quoteName ( '#__componentbuilder_' . $table ));
$db -> setQuery ( $query );
$db -> execute ();
if ( $db -> getNumRows ())
{
$aliases = $db -> loadColumn ();
foreach ( $aliases as $alias )
{
$this -> uniqeValueArray [ $table ][ 'alias' ][ $alias ] = $alias ;
}
}
return true ;
}
return false ;
}
}