Example #1
0
 /**
  * Does the merge or a preview of the merge.
  * The p_rules array is an associative array with the key being the DESTINATION fieldname
  * and the values being the SOURCE fieldname (without Fs).
  * E.g.
  * $p_rules = array('a' => 'a', 'b' => 'title', 'd' => 'body');
  *
  * p_rules is verified elsewhere (see article_types/merge3.php).
  *
  * If we are doing an actual merge, all that happens is that we rename the Type in Articles
  * from SrcType to DestType and run the merge (p_rules) on the XSrcTable entries and move
  * them over to XDestType.  Merged articles have the same ArticleNumber as their originals.
  *
  * @param string p_src
  * @param string p_dest
  * @param array p_rules
  *
  * @return boolean
  **/
 public function merge($p_src, $p_dest, $p_rules)
 {
     global $g_ado_db;
     // non-preview mode, the actual merge
     // all that needs to be done is to copy entries from Xsrc to Xdest
     // and then reassign the type in the Articles table
     $sql = "SELECT * FROM X{$p_src}";
     $rows = $g_ado_db->GetAll($sql);
     if (!count($rows)) {
         return 0;
     }
     foreach ($rows as $row) {
         $articleObj = new Article($row['IdLanguage'], $row['NrArticle']);
         $articleObj->resetCache();
         unset($articleObj);
         $fields = array();
         $values = array();
         foreach ($p_rules as $destC => $srcC) {
             $fields[] = 'F' . $destC;
             if ($srcC == 'NULL') {
                 $values[] = "''";
             } else {
                 if (is_numeric($row['F' . $srcC])) {
                     $values[] = $row['F' . $srcC];
                 } else {
                     $values[] = $g_ado_db->escape($row['F' . $srcC]);
                 }
             }
         }
         $fields[] = 'NrArticle';
         $values[] = $row['NrArticle'];
         $fields[] = 'IdLanguage';
         $values[] = $row['IdLanguage'];
         $fieldsString = implode(',', $fields);
         $valuesString = implode(',', $values);
         $sql = "INSERT IGNORE INTO X{$p_dest} ({$fieldsString}) VALUES ({$valuesString})";
         if (!$g_ado_db->Execute($sql)) {
             return 0;
         }
     }
     $sql = "UPDATE Articles SET Type='{$p_dest}' WHERE Type='{$p_src}'";
     if (!$g_ado_db->Execute($sql)) {
         return 0;
     }
     $sql = "DELETE FROM X{$p_src}";
     $g_ado_db->Execute($sql);
     CampCache::singleton()->clear('user');
     return 1;
 }