%PDF- %PDF-
| Direktori : /home1/lightco1/www/plugins/csvirules/replace/ |
| Current File : //home1/lightco1/www/plugins/csvirules/replace/replace.php |
<?php
/**
* @package CSVI
* @subpackage Plugin.Replace
*
* @author Roland Dalmulder <contact@csvimproved.com>
* @copyright Copyright (C) 2006 - 2016 RolandD Cyber Produksi. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
* @link http://www.csvimproved.com
*/
defined('_JEXEC') or die;
/**
* Replaces values.
*
* @package CSVI
* @subpackage Plugin.Replace
* @since 6.0
*/
class PlgCsvirulesReplace extends RantaiPluginDispatcher
{
/**
* The unique ID of the plugin
*
* @var string
* @since 6.0
*/
private $id = 'csvireplace';
/**
* Return the name of the plugin.
*
* @return array The name and ID of the plugin.
*
* @since 6.0
*/
public function getName()
{
return array('value' => $this->id, 'text' => 'CSVI Replace');
}
/**
* Method to get the name only of the plugin.
*
* @param string $plugin The ID of the plugin
*
* @return string The name of the plugin.
*
* @since 6.0
*/
public function getSingleName($plugin)
{
if ($plugin === $this->id)
{
return 'CSVI Replace';
}
return '';
}
/**
* Method to get the field options.
*
* @param string $plugin The ID of the plugin
* @param array $options An array of settings
*
* @return string The rendered form with plugin options.
*
* @since 6.0
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public function getForm($plugin, $options = array())
{
if ($plugin === $this->id)
{
// Load the language files
$lang = JFactory::getLanguage();
$lang->load('plg_csvirules_replace', JPATH_ADMINISTRATOR, 'en-GB', true);
$lang->load('plg_csvirules_replace', JPATH_ADMINISTRATOR, null, true);
// Add the form path for this plugin
JForm::addFormPath(JPATH_PLUGINS . '/csvirules/replace/');
// Load the helper that renders the form
$helper = new CsviHelperCsvi;
// Instantiate the form
$form = JForm::getInstance('replace', 'form_replace');
// Bind any existing data
$form->bind(array('pluginform' => $options));
// Create some dummies
$input = new JInput;
// Render the form
return $helper->renderCsviForm($form, $input);
}
}
/**
* Run the rule.
*
* @param string $plugin The ID of the plugin
* @param object $settings The plugin settings set for the field
* @param object $field The field being process
* @param CsviHelperFields $fields All fields used for import/export
*
* @return void.
*
* @since 6.0
*/
public function runRule($plugin, $settings, $field, $fields)
{
if ($plugin === $this->id && !empty($settings))
{
// Check if we have a multiple values
$findText = $settings->findtext;
$replaceText = $settings->replacetext;
if ($settings->multivalue)
{
$separator = $settings->separator;
$findText = explode($separator, $settings->findtext);
$replaceText = explode($separator, $settings->replacetext);
}
// Set the old value
$value = $field->value;
// Check if we need to apply the change
if (!isset($settings->applywhenempty))
{
$settings->applywhenempty = 1;
}
// If the field is empty and applywhenempty setting is no then do nothing
if ('' !== $value || $settings->applywhenempty)
{
switch ($settings->method)
{
case 'text':
$value = str_ireplace($findText, $replaceText, $field->value);
break;
case 'regex':
$value = preg_replace($findText, $replaceText, $field->value);
break;
}
// Update the field if needed
if ($field->value !== $value)
{
$fields->updateField($field, $value);
}
}
}
}
}