PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/exchange/code/trunk/administrator/components/com_exchange/models/transforms/updatetable.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 89 lines | 58 code | 8 blank | 23 comment | 5 complexity | 26bba5224ddfcbf9aff95533e2e28c42 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: updatetable.php 280 2010-09-18 02:14:15Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_exchange
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. /**
  13. * @package NewLifeInIT
  14. * @subpackage com_exchange
  15. */
  16. class XMLTransformUpdateTable extends XMLIterator
  17. {
  18. /**
  19. * Evaluates an expression
  20. * @param object The tag node
  21. * @param mixed The value of the field
  22. * @return mixed The result of the expression
  23. */
  24. function iterate($node, $obj, $fieldName)
  25. {
  26. $model = &$this->getModel();
  27. $test = $model->getOption('test_mode', XMLHelper::isBool((string) $node['test']));
  28. for ($j = 0; $j < $node->childCount; $j++)
  29. {
  30. $table = &$node->childNodes[$j];
  31. $srcTable = $this->_db->getEscaped($this->_src_prefix . $table->getAttribute('from'));
  32. $pk = $this->_db->getEscaped($table->getAttribute('pk'));
  33. $destTable = $this->_db->getEscaped($this->_dest_prefix . $table->getAttribute('to'));
  34. $fk = $this->_db->getEscaped($table->getAttribute('fk'));
  35. $where = $table->getAttribute('where');
  36. // assemble the sets of from and to fields
  37. $sets = array();
  38. //$sets['src'][] = $pk;
  39. for ($i = 0; $i < $table->childCount; $i++)
  40. {
  41. $field = &$table->childNodes[$i];
  42. $srcField = $this->_db->getEscaped($field->getAttribute('from'));
  43. $destField = $this->_db->getEscaped($field->getAttribute('to'));
  44. $sets['nodes'][$srcField] = $i;
  45. $sets['src'][] = $srcField;
  46. $sets['dest'][$srcField] = $destField;
  47. }
  48. // select the data
  49. $fieldNames = implode(',', $sets['src']);
  50. $query = "SELECT $pk,$fieldNames FROM $srcTable";
  51. if ($where) {
  52. $query .= ' WHERE ' . $where;
  53. }
  54. $this->_db->setQuery($query);
  55. $this->log($query, true);
  56. $rows = $this->_db->loadObjectList();
  57. // go through each row of data and copy
  58. $fieldNames = implode(',', $sets['dest']);
  59. foreach ($rows as $row)
  60. {
  61. $temp = array();
  62. foreach ($sets['src'] as $fieldName) {
  63. $i = $sets['nodes'][$fieldName];
  64. $val = $this->fieldModifiers($table->childNodes[$i], $row, $fieldName);
  65. $temp[] = $sets['dest'][$fieldName].'='.$this->_db->Quote($val);
  66. }
  67. $fieldSets = implode(',', $temp);
  68. $query = 'UPDATE ' . $destTable
  69. . ' SET ' . $fieldSets
  70. . ' WHERE ' . $fk . ' = ' . $this->_db->Quote($row->$pk);
  71. $this->_db->setQuery($query);
  72. $this->log(substr($query, 0, 256));
  73. if (!$test) {
  74. if (!$this->_db->query()) {
  75. $this->log(' ' . $this->_db->getErrorMsg(), true);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }