/exchange/code/trunk/administrator/components/com_exchange/models/transforms/maprecords.php
PHP | 106 lines | 69 code | 8 blank | 29 comment | 11 complexity | acdcf57287ef8bd4cc1d19d8f50f847c MD5 | raw file
- <?php
- /**
- * @version $Id: maprecords.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 XMLTransformMapRecords 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*/)
- {
- $fldNames = '';
- $fldValues = array();
- $model = &$this->getModel();
- $logQueries = $model->getOption('log_queries');
- $db = &$model->getDestDBO();
- // get the source data
- $data = $this->parent->getSourceData();
- $n = count($data);
- $test = $model->getOption('test_mode', XMLHelper::isBool((string) $node['test']));
- $replace = XMLHelper::isBool((string) $node['replace']);
- $unique = (string) $node['unique'];
- $uniques = array();
- // Loop through each data record
- for ($i = 0; $i < $n; $i++)
- {
- $isInsert = true;
- $item = &$data[$i];
- if ($unique)
- {
- if (in_array($item[$unique], $uniques)) {
- continue;
- }
- }
- if ($fields = parent::iterate($node, $item))
- {
- // get the field names only once
- if (!$fldNames)
- {
- $temp = array();
- foreach ($fields as $field) {
- $temp[] = $db->nameQuote($field[0]);
- }
- $fldNames = '('.implode(',', $temp).')';
- }
- // get the field values
- $temp = array();
- foreach ($fields as $field)
- {
- // a required field does not have an associated value,
- // don't include these fields (this row) in database insert
- if ($field[2]) {
- continue 2;
- }
- $temp[] = $db->Quote(trim($field[1]));
- }
- $fldValues[] = '('.implode(',', $temp).')';
- }
- if ($unique) {
- $uniques[] = $item[$unique];
- }
- }
- if ($fldValues)
- {
- // do the extended insert
- $dest = $db->nameQuote('#__'.$this->parent->dest);
- $query = $replace ? 'REPLACE' : 'INSERT';
- $query .= ' INTO '.$dest.' '.$fldNames." VALUES \n".implode(",\n", $fldValues);
- $db->setQuery($query);
- // echo $query;
- // exit('<br><br><h1>exit</h1> at ' . __FILE__ . ' (' . __LINE__ . ') '. __METHOD__ . "<br>");
- if ($logQueries) {
- ExchangeLog::add(0, $db->getQuery());
- }
- if (!$test)
- {
- if (!$db->query()) {
- ExchangeLog::add(500, ' '.$db->getErrorMsg(), true);
- }
- }
- }
- }
- }