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

Implemanted custom variables / variable replacements

parent 1c80c8fd
No related branches found
No related tags found
No related merge requests found
...@@ -4,11 +4,11 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst ...@@ -4,11 +4,11 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst
protected $_addRowButtonHtml = array(); protected $_addRowButtonHtml = array();
protected $_deleteRowButtonHtml = array(); protected $_deleteRowButtonHtml = array();
// function logitem($label, $item) { function logitem($label, $item) {
// Mage::Log($label . " " . get_class($item) . "\n", null, 'ordernumber.log'); Mage::Log($label . " " . get_class($item) . "\n", null, 'ordernumber.log');
// Mage::Log(is_array($item)?$item:$item->debug(), null, 'ordernumber.log'); Mage::Log(is_array($item)?$item:$item->debug(), null, 'ordernumber.log');
// Mage::Log(get_class_methods(get_class($item)), null, 'ordernumber.log'); Mage::Log(get_class_methods(get_class($item)), null, 'ordernumber.log');
// } }
/** /**
* Returns html part of the setting. * Returns html part of the setting.
...@@ -29,9 +29,9 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst ...@@ -29,9 +29,9 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst
$html .= $this->_getRowHeader(); $html .= $this->_getRowHeader();
// $this->logitem("render element: ", $element); // $this->logitem("render element: ", $element);
if ($this->_getValue('conditionvar')) { if ($this->_getValue('')) {
foreach ($this->_getValue('conditionvar') as $i => $var) { foreach ($this->_getValue('') as $i => $values) {
$html .= $this->_getRowHtml($i); $html .= $this->_getRowHtml($i, $values);
} }
} else { } else {
$html .= $this->_getEmptyRowHtml(); $html .= $this->_getEmptyRowHtml();
...@@ -161,16 +161,17 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst ...@@ -161,16 +161,17 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst
* @param int $rowIndex * @param int $rowIndex
* @return string * @return string
*/ */
protected function _getRowHtml($rowIndex = -1) protected function _getRowHtml($rowIndex = -1, $values = array())
{ {
static $class = 'odd'; static $class = 'odd';
$disabled = $this->_getDisabled($rowIndex == -1);
$html = '<tr class="'.$class.'">'; $html = '<tr class="'.$class.'">';
$class = ($class=='odd')?'even':'odd'; $class = ($class=='odd')?'even':'odd';
$html .= '<td class="oton-replacement-variable"><input name="' . $this->getElement()->getName() . '[conditionvar][]" value="' . $this->_getValue('conditionvar/' . $rowIndex) . '" ' . $this->_getDisabled($rowIndex == -1) . '/></td>'; $html .= '<td class="oton-replacement-variable"><input name="' . $this->getElement()->getName() . '[conditionvar][]" value="' . $values['conditionvar'] . '" ' . $disabled . '/></td>';
$html .= '<td class="oton-replacement-value" ><input name="' . $this->getElement()->getName() . '[conditionval][]" value="' . $this->_getValue('conditionval/' . $rowIndex) . '" ' . $this->_getDisabled($rowIndex == -1) . '/></td>'; $html .= '<td class="oton-replacement-value" ><input name="' . $this->getElement()->getName() . '[conditionval][]" value="' . $values['conditionval'] . '" ' . $disabled . '/></td>';
$html .= '<td>=></td>'; $html .= '<td>=></td>';
$html .= '<td class="oton-replacement-variable"><input name="' . $this->getElement()->getName() . '[newvar][]" value="' . $this->_getValue('newvar/' . $rowIndex) . '" ' . $this->_getDisabled($rowIndex == -1) . '/></td>'; $html .= '<td class="oton-replacement-variable"><input name="' . $this->getElement()->getName() . '[newvar][]" value="' . $values['newvar'] . '" ' . $disabled . '/></td>';
$html .= '<td class="oton-replacement-newvalue"><input name="' . $this->getElement()->getName() . '[newval][]" value="' . $this->_getValue('newval/' . $rowIndex) . '" ' . $this->_getDisabled($rowIndex == -1) . '/></td>'; $html .= '<td class="oton-replacement-newvalue"><input name="' . $this->getElement()->getName() . '[newval][]" value="' . $values['newval'] . '" ' . $disabled . '/></td>';
$html .= '<td>' . $this->_getDeleteRowButtonHtml() . '</td>'; $html .= '<td>' . $this->_getDeleteRowButtonHtml() . '</td>';
$html .= '</tr>'; $html .= '</tr>';
...@@ -195,13 +196,9 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst ...@@ -195,13 +196,9 @@ class OpenTools_Ordernumber_Block_Replacements extends Mage_Adminhtml_Block_Syst
protected function _getValue($key) protected function _getValue($key)
{ {
return $this->getElement()->getData('value/' . $key); return $this->getElement()->getData(empty($key)?'value':('value/' . $key));
} }
protected function _getSelected($key, $value)
{
return $this->getElement()->getData('value/' . $key) == $value ? 'selected="selected"' : '';
}
protected function _getJSAddRow($container, $template) { protected function _getJSAddRow($container, $template) {
$js = " $js = "
......
...@@ -121,19 +121,78 @@ function logitem($label, $item) { ...@@ -121,19 +121,78 @@ function logitem($label, $item) {
if (isset($info['creditmemo'])) { if (isset($info['creditmemo'])) {
$this->setupCreditMemoReplacements($reps, $info['creditmemo'], $order, $nrtype); $this->setupCreditMemoReplacements($reps, $info['creditmemo'], $order, $nrtype);
} }
Mage::Log('Replacements at end of setupReplacements(nrtype='.$nrtype.'): '.print_r($reps,1), null, 'ordernumber.log'); // Mage::Log('Replacements at end of setupReplacements(nrtype='.$nrtype.'): '.print_r($reps,1), null, 'ordernumber.log');
return $reps; return $reps;
} }
function applyCustomVariables ($nrtype, $info, $reps, $customvars) {
static $listvars = array("groups", "skus");
// Mage::getSingleton('core/session')->addWarning('<pre>custom variables, conditionvar='.$conditionvar.', reps='.print_r($reps,1).', customvars='.print_r($customvars,1).'</pre>');
$order = $info['order'];
$customer = $order->getCustomer();
$address = $order->getShippingAddress();
$store = $order->getStore();
// $this->logitem("Order: ", $order);
foreach ($customvars as $c) {
$conditionvar = $c['conditionvar'];
$found = false;
$compareval = null;
if (!$found && isset($reps[$conditionvar])) {
$found = true;
$compareval = $reps[$conditionvar];
} elseif (isset($reps['['.$conditionvar.']'])) {
$found = true;
$compareval = $reps['['.$conditionvar.']'];
} elseif (in_array($conditionvar, $listvars)) {
// TODO: Handle lists
$found = true;
$compareval = null /* TODO */;
} elseif ($compareval = $order->getData($conditionvar)) {
// TODO: Handle order property
$found = true;
} elseif ($compareval = $customer->getData($conditionvar)) {
// TODO: Handle customer property
$found = true;
} elseif ($compareval = $address->getData($conditionvar)) {
// TODO: Handle address property
$found = true;
} elseif ($compareval = $store->getData($conditionvar)) {
// TODO: Handle store property
$found = true;
} else {
// TODO: Handly other possible properties!
// TODO: Print out warning that variable could not be found.
Mage::getSingleton('core/session')->addWarning($this->__('Unable to find variable "%s" used in the ordernumber custom variable definitions.', $conditionvar));
}
if ($found) {
if (is_array($compareval)) {
$match = in_array($c['conditionval'], $compareval);
} else {
$match = ($c['conditionval'] == $compareval);
}
}
if ($found && $match) {
$varname = '['.strtolower($c['newvar']).']';
$reps[$varname] = $c['newval'];
}
// $this->logitem("Reps after $conditionvar: ", $order);
}
return $reps;
}
function doReplacements ($fmt, $reps) { function doReplacements ($fmt, $reps) {
// First, replace all randomXXX[n] fields. This needs to be done with a regexp and a callback: // First, replace all randomXXX[n] fields. This needs to be done with a regexp and a callback:
$fmt = preg_replace_callback ('/\[(random)(.*?)([0-9]*?)\]/', array($this, 'replaceRandom'), $fmt); $fmt = preg_replace_callback ('/\[(random)(.*?)([0-9]*?)\]/', array($this, 'replaceRandom'), $fmt);
return str_ireplace (array_keys($reps), array_values($reps), $fmt); return str_ireplace (array_keys($reps), array_values($reps), $fmt);
} }
function replace_fields ($fmt, $nrtype, $info) { function replace_fields ($fmt, $nrtype, $info, $customvars) {
$reps = $this->setupReplacements ($nrtype, $info); $reps = $this->setupReplacements ($nrtype, $info);
$reps = $this->applyCustomVariables ($nrtype, $info, $reps, $customvars);
// $this->logitem("All replacements after custom variables: ", $reps);
return $this->doReplacements($fmt, $reps); return $this->doReplacements($fmt, $reps);
} }
......
<?php
class OpenTools_Ordernumber_Model_Backend_Replacements extends Mage_Adminhtml_Model_System_Config_Backend_Serialized
{
function logitem($label, $item) {
Mage::Log($label . " " . get_class($item) . "\n", null, 'ordernumber.log');
Mage::Log(is_array($item)?$item:$item->debug(), null, 'ordernumber.log');
Mage::Log(get_class_methods(get_class($item)), null, 'ordernumber.log');
}
var $keywords = array("conditionvar", "conditionval", "newvar", "newval");
/**
* The form contains the values as one array for the conditionvars, one for the conditionvals,
* one for the newvariables, one for the newvalues. In the database we want to store it
* transposed as an array of (conditionvar, conditionval, newvariable, newval) arrays.
* Everything else should be inherited from the serialized config backend.
*/
protected function _beforeSave()
{
$this->logitem("OpenTools_Ordernumber_Model_Backend_Replacements: ", $this);
$vals = $this->getValue();
// Transpose the vals:
$vallist = array();
if ($vals) {
foreach ($vals[$this->keywords[1]] as $i => $dummy) {
$entry = array();
foreach ($this->keywords as $k) {
$entry[$k] = $vals[$k][$i];
}
$vallist[] = $entry;
}
}
Mage::Log("transposed vallist: ".print_r($vallist,1), null, 'ordernumber.log');
$this->setValue($vallist);
return parent::_beforeSave();
}
}
\ No newline at end of file
...@@ -99,14 +99,16 @@ class OpenTools_Ordernumber_Model_Observer extends Mage_Core_Model_Abstract ...@@ -99,14 +99,16 @@ class OpenTools_Ordernumber_Model_Observer extends Mage_Core_Model_Abstract
default: /* Pre-defined counter formats saved in the /reset config field */ default: /* Pre-defined counter formats saved in the /reset config field */
$counterfmt = $format . '|' . $reset; break; $counterfmt = $format . '|' . $reset; break;
} }
// Now apply the replacements
$nr = $helper->replace_fields ($format, $nrtype, $info);
$customvars = Mage::getStoreConfig('ordernumber/replacements', $storeId); $customvars = Mage::getStoreConfig('ordernumber/replacements', $storeId);
if (isset($customvars['replacements'])) if (isset($customvars['replacements']))
$customvars = $customvars['replacements']; $customvars = $customvars['replacements'];
if ($customvars) if ($customvars)
$customvars = unserialize($customvars); $customvars = unserialize($customvars);
Mage::Log('customvars: '.print_r($customvars,1), null, 'ordernumber.log'); Mage::Log('customvars: '.print_r($customvars,1), null, 'ordernumber.log');
// Now apply the replacements
$nr = $helper->replace_fields ($format, $nrtype, $info, $customvars);
// Split at a | to get the number format and a possibly different counter increment format // Split at a | to get the number format and a possibly different counter increment format
// If a separate counter format is given after the |, use it, otherwise reuse the number format itself as counter format // If a separate counter format is given after the |, use it, otherwise reuse the number format itself as counter format
$parts = explode ("|", $nr); $parts = explode ("|", $nr);
......
...@@ -364,7 +364,7 @@ ...@@ -364,7 +364,7 @@
<fields> <fields>
<replacements translate="label tooltip comment"> <replacements translate="label tooltip comment">
<frontend_model>opentools_ordernumber/replacements</frontend_model> <frontend_model>opentools_ordernumber/replacements</frontend_model>
<backend_model>adminhtml/system_config_backend_serialized</backend_model> <backend_model>opentools_ordernumber/backend_replacements</backend_model>
<sort_order>2</sort_order> <sort_order>2</sort_order>
<show_in_default>1</show_in_default> <show_in_default>1</show_in_default>
<show_in_website>1</show_in_website> <show_in_website>1</show_in_website>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment