Skip to content
Snippets Groups Projects
Commit c103f51c authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Properly implement custom variable replacements

parent 5e86e4f8
No related branches found
No related tags found
No related merge requests found
BASE=ordernumber
PLUGINTYPE=vmshopper
VERSION=2.2
VERSION=3.0
PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html $(BASE)/
......
<?php
defined('_JEXEC') or die();
/**
*
* @package VirtueMart
* @subpackage Plugins - Fields
* @author Reinhold Kainhofer, Open Tools
* @link http://www.open-tools.net
* @copyright Copyright (c) 2014 Reinhold Kainhofer. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
if (!class_exists( 'VmConfig' ))
require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
class JFormFieldVmOrdernumberReplacements extends JFormField {
var $_name = 'vmOrdernumberReplacements';
static $pluginpath = '/plugins/vmshopper/ordernumber/ordernumber/';
protected $countertype;
// VM2 on J2 works, VM3 on J3 works out of the box, but
// VM3 on J2 does NOT work by simply calling vmJsApi::jQuery, because
// the JS is never added to the page header, so we have to add this manually
protected function loadjQuery() {
vmJsApi::jQuery();
// TODO: jquery::ui available only in J3:
JHtml::_('jquery.ui', array('core', 'sortable'));
// If we are on Joomla 2.5 and VM 3, manually add the script declarations
// cached in vmJsApi to the document header:
if (version_compare(JVERSION, '3.0', 'lt') && defined('VM_VERSION') && VM_VERSION>=3) {
$document = JFactory::getDocument();
$scripts = vmJsApi::getJScripts();
foreach ($scripts as $name => $jsToAdd) {
if($jsToAdd['written']) continue;
$file = $jsToAdd['script'] ? $jsToAdd['script'] : $name;
if(strpos($file,'/')!==0){
$file = vmJsApi::setPath($file,false,'');
} else if(strpos($file,'//')!==0){
$file = JURI::root(true).$file;
}
$ver = '';
if(!empty($jsToAdd['ver'])) $ver = '?vmver='.$jsToAdd['ver'];
$document->addScript( $file .$ver,"text/javascript",$jsToAdd['defer'],$jsToAdd['async'] );
vmJsApi::removeJScript($name);
}
}
}
static function img_url($file) {
return JURI::root(true) . self::$pluginpath . 'assets/images/' . $file;
}
static function css_url($file) {
return JURI::root(true) . self::$pluginpath . 'assets/css/' . $file;
}
static function js_url($file) {
return JURI::root(true) . self::$pluginpath . 'assets/js/' . $file;
}
static function __($string) {
return JText::_($string);
}
protected function makeJSTranslationsAvailable() {
// JText::script('PLG_ORDERNUMBER_JS_JSONERROR');
}
protected function getInput() {
$html=array();
$doc = JFactory::getDocument()->addStyleSheet(self::css_url('ordernumber.css'));
$doc->addScript( self::js_url('ordernumber.js'));
$this->makeJSTranslationsAvailable();
$this->loadjQuery();
$value = $this->value;
// $html[] = "<pre> value: ".print_r($value,1)."</pre>";
// $html[] = "<pre>Form Field: ".print_r($this,1)."</pre>";
$variables = array();
if (!is_array($value))
$value = array();
if (!empty($value)) {
$keys = array_keys($value);
foreach (array_keys($value[$keys[0]]) as $i) {
$entry = array();
foreach ($keys as $k) {
$entry[$k] = $value[$k][$i];
}
$variables[] = $entry;
}
}
$id = $this->id;
$name = $this->name;
// $html[] = "<pre>Variables: ".print_r($variables,1)."</pre>";
$html[] = '<table id="ordernumber_variables_template" style="display:none">';
$html[] = $this->create_replacements_row_html($name, array(), 'disabled');
$html[] = '</table>';
$html[] = '<table id="ordernumber_variables" class="ordernumber_variables widefat wc_input_table sortable" cellspacing="0">';
$columns = array(
'variables_ifvar' => self::__( 'If variable ...'),
'variables_ifop' => '',
'variables_ifval' => self::__( 'Value'),
'variables_then' => self::__( ''),
'variables_thenvar' => self::__( 'Set variable ...'),
'variables_thenval' => self::__( 'to value ...'),
'sort' => '',
'variables_settings' => '',
);
$html[] = ' <thead>';
$html[] = ' <tr class="ordernumber_variables_header">';
foreach ( $columns as $key => $column ) {
$html[] = '<th class="' . $key . '">' . htmlspecialchars( $column ) . '</th>';
}
$html[] = ' </tr>';
$html[] = ' <tr id="ordernumber-replacements-empty-row" class="oton-empty-row-notice ' . (empty($variables)?"":"rowhidden") . '">';
$html[] = ' <td class="oton-empty-row-notice" colspan="8">';
$html[] = ' <em>a' . self::__('No custom variables have been defined.') . '</em>';
$html[] = ' <input type="hidden" name="' . $name . '" value="" ' . (empty($variables))?'':'disabled' . '>';
$html[] = ' </td>';
$html[] = ' </tr>';
$html[] = ' </thead>';
$html[] = ' <colgroup>';
foreach ($columns as $key => $column) {
$html[] = '<col class="' . $key . '" />';
}
$html[] = ' </colgroup>';
$html[] = '';
$html[] = ' <tbody>';
foreach ($variables as $var) {
$html[] = $this->create_replacements_row_html($name, $var);
}
$html[] = ' </tbody>';
$html[] = ' <tfoot>';
$html[] = ' <tr class="addreplacement_row">';
$html[] = ' <td colspan=8 class="variable_add">';
$html[] = ' <div class="ordernumber-variables-addbtn ordernumber-btn" onClick="ordernumberVariablesAddRow(\'ordernumber_variables_template\', \'ordernumber_variables\')">';
$html[] = ' <div class="ordernumber-ajax-loading"><img src="' . self::img_url( 'icon-16-new.png' ) . '" class="ordernumber-counter-addbtn" /></div>';
$html[] = self::__('Add new custom variable');
$html[] = ' </div>';
$html[] = ' </td>';
$html[] = ' </tr>';
$html[] = ' </tfoot>';
$html[] = '</table>';
return implode("\n", $html);
}
protected function create_replacements_row_html($name, $values = array(), $disabled = '') {
$operator = (isset($values['conditionop'])?$values['conditionop']:'');
$operators = array(
'equals' => '=',
'contains' => self::__('contains'),
'smaller' => '<',
'smallerequal' => '<=',
'larger' => '>',
'largerequal' => '>=',
'startswith' => self::__('starts with'),
'endswith' => self::__('ends with'),
);
$html = '
<tr>
<td class="variables_ifvar"><input name="' . $name . '[conditionvar][]" value="' . (isset($values['conditionvar'])?$values['conditionvar']:'') . '" ' . $disabled . '/></td>
<td class="variables_ifop" ><select name="' . $name . '[conditionop][]" ' . $disabled . ' style="width: 100px">';
foreach ($operators as $op => $opname) {
$html .= ' <option value="' . $op . '" ' . (($op === $operator)?'selected':'') . '>' . htmlspecialchars($opname) . '</option>';
}
$html .= '</select></td>
<td class="variables_ifval" ><input name="' . $name . '[conditionval][]" value="' . (isset($values['conditionval'])?$values['conditionval']:'') . '" ' . $disabled . '/></td>
<td class="variables_then">=></td>
<td class="variables_thenvar"><input name="' . $name . '[newvar][]" value="' . (isset($values['newvar'])?$values['newvar']:'') . '" ' . $disabled . '/></td>
<td class="variables_thenval"><input name="' . $name . '[newval][]" value="' . (isset($values['newval'])?$values['newval']:'') . '" ' . $disabled . '/></td>
<td class="sort"></td>
<td class="variables_settings"><img src="' . self::img_url( 'icon-16-delete.png' ) . '" class="ordernumber-replacement-deletebtn ordernumber-btn"></td>
</tr>';
return $html;
}
}
\ No newline at end of file
......@@ -52,9 +52,12 @@ PLG_ORDERNUMBER_COUNTERLIST_HEADER_VALUE="Counter value"
PLG_ORDERNUMBER_COUNTERLIST_ADD="Add new counter"
PLG_ORDERNUMBER_COUNTERLIST_EXISTS="Counter '%s' already exists."
PLG_ORDERNUMBER_REPLACEMENTS_DESC="Here you can define contingent custom variables, which you can then use in the number format like any pre-defined variable."
PLG_ORDERNUMBER_FIELDSET_ORDERNUMBER="Order Numbers"
PLG_ORDERNUMBER_FIELDSET_INVOICENUMBER="Invoice Numbers"
PLG_ORDERNUMBER_FIELDSET_CUSTOMERNUMBER="Customer Numbers"
PLG_ORDERNUMBER_FIELDSET_REPLACEMENTS="Custom Variables"
PLG_ORDERNUMBER_JS_NOT_AUTHORIZED="You are not authorized to modify order number counters."
PLG_ORDERNUMBER_JS_NEWCOUNTER="Please enter the format/name of the new counter:"
......
......@@ -52,9 +52,12 @@ PLG_ORDERNUMBER_COUNTERLIST_HEADER_VALUE="Counter value"
PLG_ORDERNUMBER_COUNTERLIST_ADD="Add new counter"
PLG_ORDERNUMBER_COUNTERLIST_EXISTS="Counter '%s' already exists."
PLG_ORDERNUMBER_REPLACEMENTS_DESC="Here you can define contingent custom variables, which you can then use in the number format like any pre-defined variable."
PLG_ORDERNUMBER_FIELDSET_ORDERNUMBER="Order Numbers"
PLG_ORDERNUMBER_FIELDSET_INVOICENUMBER="Invoice Numbers"
PLG_ORDERNUMBER_FIELDSET_CUSTOMERNUMBER="Customer Numbers"
PLG_ORDERNUMBER_FIELDSET_REPLACEMENTS="Custom Variables"
PLG_ORDERNUMBER_JS_NOT_AUTHORIZED="You are not authorized to modify order number counters."
PLG_ORDERNUMBER_JS_NEWCOUNTER="Please enter the format/name of the new counter:"
......
This diff is collapsed.
......@@ -75,6 +75,21 @@ class plgVmShopperOrdernumberInstallerScript
*/
public function update(JAdapterInstance $adapter)
{
$db = JFactory::getDBO();
$db->setQuery('ALTER TABLE `#__virtuemart_shopper_plg_ordernumber` CHANGE `number_type` `number_type` VARCHAR(30) NULL DEFAULT NULL;');
$db->query();
$countertypes = array(
'order_number' => 0,
'invoice_number' => 1,
'customer_number' => 2,
'order_password' => 3,
);
foreach ($countertypes as $new => $old) {
$db->setQuery('update `#__virtuemart_shopper_plg_ordernumber` SET `number_type`="'.$new.'" WHERE `number_type`='.(int)$old.';');
$db->query();
}
}
// jimport( 'joomla.filesystem.file' );
// $file = JPATH_ROOT . DS . "administrator" . DS . "language" . DS . "en-GB" . DS . "en-GB.plg_vmshopper_ordernumber.sys.ini";
// if (JFile::exists($file)) JFile::delete($file);
......
......@@ -7,10 +7,10 @@
<authorUrl>http://www.open-tools.net/</authorUrl>
<copyright>Copyright (C) 2012-2014 Reinhold Kainhofer. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3</license>
<version>2.2</version>
<releaseDate>2014-12-07</releaseDate>
<releaseType>Minor update</releaseType>
<downloadUrl>http://www.open-tools.net/virtuemart-2-extensions/vm2-ordernumber-plugin.html</downloadUrl>
<version>3.0</version>
<releaseDate>2015-12-07</releaseDate>
<releaseType>Major update</releaseType>
<downloadUrl>http://open-tools.net/virtuemart/advanced-ordernumbers.html</downloadUrl>
<description>VMSHOPPER_ORDERNUMBER_DESC</description>
......@@ -83,7 +83,12 @@
</field>
<field name="customer_number_allcounters" type="VmOrdernumberCounters" label="PLG_ORDERNUMBER_ORDERNR_ALLCOUNTERS" countertype="customer_number" showon="customize_customer_number:1" />
</fieldset>
<fieldset name="replacements" label="PLG_ORDERNUMBER_FIELDSET_REPLACEMENTS">
<field name="replacements_options" type="spacer" label="PLG_ORDERNUMBER_REPLACEMENTS_DESC" />
<field name="replacements" type="VmOrdernumberReplacements" label="" />
</fieldset>
</fields>
</config>
</extension>
......@@ -31,3 +31,68 @@ div.ordernumber-ajax-loading, div.ordernumber-ajax-loading img.vmordernumber-btn
div.ordernumber-ajax-loading img {
z-index:0;
}
/* Counter custom variable replacements */
table.ordernumber_variables {
border: 1px solid #888888;
width: inherit;
}
table.ordernumber_variables td, table.ordernumber_variables th {
padding: 0px;
vertical-align: middle;
}
/* table.ordernumber_variables td.sort:before { */
/* float: none; */
/* display: inline-block; */
/* } */
table.ordernumber_variables thead th {
text-align: center;
width: auto;
}
td.counter_value {
text-align: center;
}
table.ordernumber_variables input {
background-color: rgba(255,255,255,0.75);
}
table.ordernumber_variables thead > tr:nth-child(odd) > th,
table.ordernumber_variables tfoot > tr.addreplacement_row > td {
background: #E0E0E0;
}
table.ordernumber_variables tbody > tr:nth-child(even) > td {
background: #F0F0F0;
}
table.ordernumber_variables tbody tr td input {
width: 100%;
}
.ordernumber-btn {
cursor: pointer;
}
table.ordernumber_variables img {
padding: 0;
margin: 0;
}
tr.rowhidden {
display: none;
}
/* Adjust the columns of the replacements table */
col.variables_ifvar, col.variables_ifval {
width: 15%;
}
col.variables_ifop {
width: 10%;
}
col.variables_thenvar, col.variables_thenval {
width: 25%;
}
.variables_then, .variables_settings {
text-align: center;
width: 20px;
}
/**********************************************************************************
*
* Javascript for the counter modification table
*
**********************************************************************************/
var updateMessages = function(messages, area) {
jQuery( "#system-message-container #system-message ."+area+"-message").remove();
// Extract the messages from the returned string, add the ordernumber-message class (so the next ajax call
......@@ -144,3 +149,46 @@ var ajaxAddCounter = function (btn, nrtype) {
});
}
}
/**********************************************************************************
*
* Javascript for the Custom Variables table
*
**********************************************************************************/
var ordernumberVariablesAddRow = function (template, element) {
var cl = jQuery("#" + template + " tr").clone(true);
// Enable all form controls
jQuery(cl).find('input,select,button,img').removeAttr('disabled');
// select boxes handled by the chosen juery plugin cannot simply be cloned,
// instead we need to re-initialize chosen!
jQuery(cl).find('select').removeClass("chzn-done").removeAttr("id").css("display", "block").next().remove();
jQuery(cl).find('select').chosen({width: "50px"});
// Now insert this new row into the table
jQuery(cl).appendTo("table#" + element + " tbody");
jQuery("tr#ordernumber-replacements-empty-row")
.addClass("rowhidden")
.find('input')
.attr('disabled', 'disabled');
}
jQuery(document).ready (function () {
jQuery('img.ordernumber-replacement-deletebtn').click(
function () {
jQuery(this).closest('tr').remove();
var count = jQuery(this).closest('table').find('tbody tr').length;
if (count==0) {
jQuery("tr#ordernumber-replacements-empty-row")
.removeClass("rowhidden")
.find('input,select,button,img')
.removeAttr('disabled');
}
}
);
jQuery("#ordernumber_variables tbody").sortable();
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment