/exchange/code/trunk/administrator/components/com_exchange/models/transforms/updatetable.php
PHP | 89 lines | 58 code | 8 blank | 23 comment | 5 complexity | 26bba5224ddfcbf9aff95533e2e28c42 MD5 | raw file
- <?php
- /**
- * @version $Id: updatetable.php 280 2010-09-18 02:14:15Z eddieajau $
- * @package NewLifeInIT
- * @subpackage com_exchange
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License version 2 or later.
- * @link http://www.theartofjoomla.com
- */
- // no direct access
- defined('_JEXEC') or die;
- /**
- * @package NewLifeInIT
- * @subpackage com_exchange
- */
- class XMLTransformUpdateTable extends XMLIterator
- {
- /**
- * Evaluates an expression
- * @param object The tag node
- * @param mixed The value of the field
- * @return mixed The result of the expression
- */
- function iterate($node, $obj, $fieldName)
- {
- $model = &$this->getModel();
- $test = $model->getOption('test_mode', XMLHelper::isBool((string) $node['test']));
- for ($j = 0; $j < $node->childCount; $j++)
- {
- $table = &$node->childNodes[$j];
- $srcTable = $this->_db->getEscaped($this->_src_prefix . $table->getAttribute('from'));
- $pk = $this->_db->getEscaped($table->getAttribute('pk'));
- $destTable = $this->_db->getEscaped($this->_dest_prefix . $table->getAttribute('to'));
- $fk = $this->_db->getEscaped($table->getAttribute('fk'));
- $where = $table->getAttribute('where');
- // assemble the sets of from and to fields
- $sets = array();
- //$sets['src'][] = $pk;
- for ($i = 0; $i < $table->childCount; $i++)
- {
- $field = &$table->childNodes[$i];
- $srcField = $this->_db->getEscaped($field->getAttribute('from'));
- $destField = $this->_db->getEscaped($field->getAttribute('to'));
- $sets['nodes'][$srcField] = $i;
- $sets['src'][] = $srcField;
- $sets['dest'][$srcField] = $destField;
- }
- // select the data
- $fieldNames = implode(',', $sets['src']);
- $query = "SELECT $pk,$fieldNames FROM $srcTable";
- if ($where) {
- $query .= ' WHERE ' . $where;
- }
- $this->_db->setQuery($query);
- $this->log($query, true);
- $rows = $this->_db->loadObjectList();
- // go through each row of data and copy
- $fieldNames = implode(',', $sets['dest']);
- foreach ($rows as $row)
- {
- $temp = array();
- foreach ($sets['src'] as $fieldName) {
- $i = $sets['nodes'][$fieldName];
- $val = $this->fieldModifiers($table->childNodes[$i], $row, $fieldName);
- $temp[] = $sets['dest'][$fieldName].'='.$this->_db->Quote($val);
- }
- $fieldSets = implode(',', $temp);
- $query = 'UPDATE ' . $destTable
- . ' SET ' . $fieldSets
- . ' WHERE ' . $fk . ' = ' . $this->_db->Quote($row->$pk);
- $this->_db->setQuery($query);
- $this->log(substr($query, 0, 256));
- if (!$test) {
- if (!$this->_db->query()) {
- $this->log(' ' . $this->_db->getErrorMsg(), true);
- }
- }
- }
- }
- }
- }