Beispiel #1
0
 /**
  * Prepare the tmp table for updates, use the deletion rules for it
  *
  * @param $objRun
  * @return bool
  */
 public static function clearTable($objRun)
 {
     $Database = Database::getInstance();
     // perform the deletion rules defined in the converter for every internal target table
     foreach ($objRun->targets as $strTarget) {
         if (!in_array($strTarget, $objRun->cleared)) {
             $objConverter = false;
             foreach ($objRun->steps as $intStep) {
                 $objStep = StepModel::findByPk($intStep);
                 if ($objStep->action == 'converter' && !$objConverter) {
                     $objConverter = ConverterModel::findOneBy(array('id=?', 'targetTable=?'), array($objStep->converter, str_replace('cvx_', '', $strTarget)));
                 }
             }
             // there are tables not bound on a converter
             if (!$objConverter) {
                 return $strTarget;
             } else {
                 $objConverter->deleteRules = Helper::arrayOnly($objConverter->deleteRules);
                 $objConverter->fieldsTarget = Helper::arrayOnly($objConverter->fieldsTarget);
                 $objConverter->fieldsSource = Helper::arrayOnly($objConverter->fieldsSource);
                 // for updates we need all existent keys of the source and the target table
                 if ($objConverter->allowUpdate || $objConverter->deleteOnStart != '' && $objConverter->deleteOnStart != 'all') {
                     // source
                     $strSource = $objConverter->sourceType == 'InternalTable' ? $objConverter->sourceTable : 'cvx_' . $objConverter->id . '_source';
                     $objKeys = $Database->prepare("SELECT " . $objConverter->keySource . " FROM " . $strSource)->execute();
                     $arrSourceKeys = $objKeys->fetchEach($objConverter->keySource);
                     if ($objConverter->sourceHasFieldnames) {
                         // seems to be an artefact
                         //                            array_shift($arrSourceKeys);
                     }
                     // target
                     $objKeys = $Database->prepare("SELECT " . $objConverter->keyTarget . " FROM " . $strTarget)->execute();
                     $arrTargetKeys = $objKeys->fetchEach($objConverter->keyTarget);
                     $objConverter->targetKeys = serialize($arrTargetKeys);
                 }
                 $objConverter->save();
                 $blnOk = true;
                 // delete all data in target table
                 if ($objConverter->deleteOnStart == 'all') {
                     if (!($objDeletion = $Database->prepare("TRUNCATE TABLE " . $strTarget)->execute())) {
                         $blnOk = false;
                     }
                 } elseif ($objConverter->deleteOnStart == 'missing') {
                     // delete data missing in target, but existing in the source table
                     if (!($objDeletion = $Database->prepare("DELETE FROM " . $strTarget . " WHERE " . $objConverter->keyTarget . " IN ('" . implode("','", array_diff($arrTargetKeys, $arrSourceKeys)) . "')")->execute())) {
                         $blnOk = false;
                     }
                 } elseif ($objConverter->deleteOnStart == 'existent') {
                     // delete data already existing in target table
                     if (!($objDeletion = $Database->prepare("DELETE FROM " . $strTarget . " WHERE " . $objConverter->keyTarget . " IN ('" . implode("','", array_diff($arrTargetKeys, array_diff($arrTargetKeys, $arrSourceKeys))) . "')")->execute())) {
                         $blnOk = false;
                     }
                 }
                 // delete depending on rules
                 if ($objConverter->deleteOnRules) {
                     $strWhere = '';
                     foreach ($objConverter->deleteRules as $k => $v) {
                         $strWhere .= ($k > 0 ? ' ' . $v['type'] . ' ' : '') . $v['field'] . ' ';
                         switch ($v['operator']) {
                             case 'gteq':
                                 $strWhere .= '>= \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'gt':
                                 $strWhere .= '> \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'lt':
                                 $strWhere .= '< \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'lteq':
                                 $strWhere .= '<= \'' . addslashes($v['value']) . '\'';
                                 break;
                             case 'begins':
                                 $strWhere .= 'LIKE \'' . addslashes($v['value']) . '%\'';
                                 break;
                             case 'ends':
                                 $strWhere .= 'LIKE \'%' . addslashes($v['value']) . '\'';
                                 break;
                             default:
                                 $strWhere .= '= \'' . addslashes($v['value']) . '\'';
                                 break;
                         }
                     }
                     if (!($objDeletion = $Database->prepare("DELETE FROM " . $strTarget . " WHERE " . $strWhere)->execute())) {
                         $blnOk = false;
                     }
                 }
                 if ($blnOk) {
                     Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['temporaryTargetCleared'], $strTarget), 'entry');
                     return $strTarget;
                 } else {
                     Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['temporaryTargetNotCleared'], $strTarget), 'entry', 'error');
                     return false;
                 }
             }
             // fill exactly 1 of the temp source tables at a time
             continue;
         }
     }
 }
Beispiel #2
0
 /**
  * Fill the tmp table in the way specified in the dada converter
  *
  * @param $objRun
  * @return bool|mixed
  */
 public static function fillTable($objRun)
 {
     foreach ($objRun->sources as $strSource) {
         if (!in_array($strSource, $objRun->filled)) {
             $intConverter = str_replace('_source', '', str_replace('cvx_', '', $strSource));
             $objConverter = ConverterModel::findByPk($intConverter);
             // get the glass of the converter
             $strClass = ($GLOBALS['convertx']['classpath'][$objConverter->sourceType] ? $GLOBALS['convertx']['classpath'][$objConverter->sourceType] : 'Delahaye\\ConvertX\\Container') . '\\' . $objConverter->sourceType;
             // fill temp table
             if ($strClass::rawImport($intConverter, $objRun)) {
                 Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['temporarySourceFilled'], $intConverter), 'entry');
                 return $intConverter;
             } else {
                 Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['temporarySourceNotFilled'], $intConverter), 'entry', 'error');
                 return false;
             }
             // fill exactly 1 of the temp source tables at a time
             continue;
         }
     }
 }
Beispiel #3
0
 /**
  * insert or update the data set
  *
  * @param $strType
  * @param $objRun
  * @param $objConverter
  * @param $objSource
  * @return bool|mixed|null
  */
 protected static function insertOrUpdate($strType, $objRun, $objConverter, $objSource)
 {
     // target
     $strTarget = $objConverter->targetType == 'InternalTable' ? 'cvx_' . $objConverter->targetTable : 'cvx_' . $objConverter->id;
     $arrData = self::getData($strType, $objRun, $objConverter, $objSource, $strTarget);
     if (!is_array($arrData) && strpos($arrData, 'ConvertX-') !== false) {
         Tracking::log($objRun->id, $objRun->rootRun, $GLOBALS['TL_LANG']['tl_convertx_job']['insertError'] . str_replace('ConvertX-', ' ', $arrData), 'entry', 'error', print_r($arrData, true));
         return false;
     }
     $strKeySource = $objConverter->keySource;
     if (is_array($arrData)) {
         if (count($arrData) > 0 || $objRun->simulation) {
             // in simulation the database statement always works fine
             if ($objRun->simulation) {
                 return 'sim';
             }
             // skip withor error if empty set shall be inserted (maybe comes from csv-file)
             $allEmpty = true;
             foreach ($arrData as $k => $v) {
                 if (is_numeric($v) && $v > 0 || !is_numeric($v) && $v != '') {
                     $allEmpty = false;
                 }
                 // prevent null
                 if (is_null($v)) {
                     $arrData[$k] = '';
                 }
             }
             if ($allEmpty) {
                 Tracking::log($objRun->id, $objRun->rootRun, $GLOBALS['TL_LANG']['tl_convertx_job']['insertError'], 'entry', 'error', print_r($arrData, true));
                 return false;
             }
             switch ($strType) {
                 case 'update':
                     // perform the update and return the key of the row
                     if (Database::getInstance()->prepare("UPDATE " . $strTarget . " %s WHERE " . $objConverter->keyTarget . "=?")->set($arrData)->execute($objSource->{$strKeySource})) {
                         return $objSource->{$strKeySource};
                     }
                     // update failed
                     Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['updateError'], sprintf('%s=%s', $objConverter->keyTarget, $objSource->{$strKeySource})), 'entry', 'error', print_r($arrData, true));
                     return false;
                     break;
                 default:
                     $objIns = Database::getInstance()->prepare("INSERT INTO " . $strTarget . " %s")->set($arrData)->execute();
                     // insert is ok
                     if ($objIns->insert_id()) {
                         return $objIns->insertId;
                     }
                     // insert failed
                     Tracking::log($objRun->id, $objRun->rootRun, $GLOBALS['TL_LANG']['tl_convertx_job']['insertError'], 'entry', 'error', print_r($arrData, true));
                     return false;
                     break;
             }
         }
     }
     return false;
 }
Beispiel #4
0
 /**
  * Do a step
  *
  * @param $objRun
  * @return mixed
  */
 public static function doStep($objRun)
 {
     $objStep = StepModel::findByPk($objRun->step);
     // -----------------------------------------
     // use the class of the step
     $strClass = 'Delahaye\\ConvertX\\Step\\' . ucfirst($objStep->action);
     Tracking::log($objRun->id, $objRun->rootRun, sprintf('%s: %s', $GLOBALS['TL_LANG']['tl_convertx_job']['step_' . $objStep->action], $objStep->title));
     $objReturn = $strClass::doStep($objRun, $objStep);
     if ($objReturn->error) {
         Tracking::log($objRun->id, $objRun->rootRun, $objReturn->error, 'entry', 'error', $objReturn->details);
         if ($objStep->abortOnError) {
             $objReturn->title = $GLOBALS['TL_LANG']['tl_convertx_job']['abortion'];
             Tracking::log($objRun->id, $objRun->rootRun, sprintf($GLOBALS['TL_LANG']['tl_convertx_job']['abortionInStep'], $objStep->id, $objStep->action, $objStep->title), 'step', 'error');
             return $objReturn;
         }
     }
     return $objReturn;
 }