PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 106 lines | 69 code | 8 blank | 29 comment | 11 complexity | acdcf57287ef8bd4cc1d19d8f50f847c MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: maprecords.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 XMLTransformMapRecords extends XMLIterator
  17. {
  18. /**
  19. * Evaluates an expression
  20. *
  21. * @param object The tag node
  22. * @param mixed The value of the field
  23. * @return mixed The result of the expression
  24. */
  25. function iterate(&$node /*, $obj, $fieldName*/)
  26. {
  27. $fldNames = '';
  28. $fldValues = array();
  29. $model = &$this->getModel();
  30. $logQueries = $model->getOption('log_queries');
  31. $db = &$model->getDestDBO();
  32. // get the source data
  33. $data = $this->parent->getSourceData();
  34. $n = count($data);
  35. $test = $model->getOption('test_mode', XMLHelper::isBool((string) $node['test']));
  36. $replace = XMLHelper::isBool((string) $node['replace']);
  37. $unique = (string) $node['unique'];
  38. $uniques = array();
  39. // Loop through each data record
  40. for ($i = 0; $i < $n; $i++)
  41. {
  42. $isInsert = true;
  43. $item = &$data[$i];
  44. if ($unique)
  45. {
  46. if (in_array($item[$unique], $uniques)) {
  47. continue;
  48. }
  49. }
  50. if ($fields = parent::iterate($node, $item))
  51. {
  52. // get the field names only once
  53. if (!$fldNames)
  54. {
  55. $temp = array();
  56. foreach ($fields as $field) {
  57. $temp[] = $db->nameQuote($field[0]);
  58. }
  59. $fldNames = '('.implode(',', $temp).')';
  60. }
  61. // get the field values
  62. $temp = array();
  63. foreach ($fields as $field)
  64. {
  65. // a required field does not have an associated value,
  66. // don't include these fields (this row) in database insert
  67. if ($field[2]) {
  68. continue 2;
  69. }
  70. $temp[] = $db->Quote(trim($field[1]));
  71. }
  72. $fldValues[] = '('.implode(',', $temp).')';
  73. }
  74. if ($unique) {
  75. $uniques[] = $item[$unique];
  76. }
  77. }
  78. if ($fldValues)
  79. {
  80. // do the extended insert
  81. $dest = $db->nameQuote('#__'.$this->parent->dest);
  82. $query = $replace ? 'REPLACE' : 'INSERT';
  83. $query .= ' INTO '.$dest.' '.$fldNames." VALUES \n".implode(",\n", $fldValues);
  84. $db->setQuery($query);
  85. // echo $query;
  86. // exit('<br><br><h1>exit</h1> at ' . __FILE__ . ' (' . __LINE__ . ') '. __METHOD__ . "<br>");
  87. if ($logQueries) {
  88. ExchangeLog::add(0, $db->getQuery());
  89. }
  90. if (!$test)
  91. {
  92. if (!$db->query()) {
  93. ExchangeLog::add(500, ' '.$db->getErrorMsg(), true);
  94. }
  95. }
  96. }
  97. }
  98. }