Geo_Map::OnArticleCopy PHP Method

OnArticleCopy() public static method

- read and copy all links / basic data on: - maps (Map table) with links into the given src article, set link into dest art no. - points (MapLocation table) with links into read map ids, set link into new map ids - text contents (MapLocationLanguages table) with links into read maploc ids and into the given languages - multimedia (MapLocationMultimedia table) with links into read maploc ids
public static OnArticleCopy ( integer $p_srcArticleNumber, integer $p_destArticleNumber, array $p_copyTranslations, $p_userId = null ) : void
$p_srcArticleNumber integer
$p_destArticleNumber integer
$p_copyTranslations array
return void
    public static function OnArticleCopy($p_srcArticleNumber, $p_destArticleNumber, $p_copyTranslations, $p_userId = null)
    {
        global $g_ado_db;
        Geo_MapLocation::CleanFound();
        $list_fill = '%%id_list%%';
        $lang_fill = '%%id_langs%%';
        $map_columns = array('fk_article_number', 'MapRank', 'MapUsage', 'MapCenterLongitude', 'MapCenterLatitude', 'MapDisplayResolution', 'MapProvider', 'MapWidth', 'MapHeight', 'MapName', 'IdUser');
        $map_colstr = implode(', ', $map_columns);
        $map_colqms = implode(', ', str_split(str_repeat('?', count($map_columns))));
        $queryStr_map_sel = "SELECT id, {$map_colstr} FROM Maps WHERE fk_article_number = ?";
        $queryStr_map_ins = "INSERT INTO Maps ({$map_colstr}) VALUES ({$map_colqms})";
        $maploc_columns = array('fk_map_id', 'fk_location_id', 'poi_style', 'rank');
        $maploc_colstr = implode(', ', $maploc_columns);
        $maploc_colqms = implode(', ', str_split(str_repeat('?', count($maploc_columns))));
        $queryStr_maploc_sel = "SELECT id, {$maploc_colstr} FROM MapLocations WHERE fk_map_id IN (%%id_list%%)";
        $queryStr_maploc_ins = "INSERT INTO MapLocations ({$maploc_colstr}) VALUES ({$maploc_colqms})";
        $maploclan_columns = array('fk_maplocation_id', 'fk_language_id', 'fk_content_id', 'poi_display');
        $maploclan_colstr = implode(', ', $maploclan_columns);
        $maploclan_colqms = implode(', ', str_split(str_repeat('?', count($maploclan_columns))));
        $queryStr_maploclan_sel = "SELECT {$maploclan_colstr} FROM MapLocationLanguages WHERE fk_maplocation_id IN (%%id_list%%) AND fk_language_id IN (%%id_langs%%)";
        $queryStr_maploclan_ins = "INSERT INTO MapLocationLanguages ({$maploclan_colstr}) VALUES ({$maploclan_colqms})";
        $maplocmed_columns = array('fk_maplocation_id', 'fk_multimedia_id');
        $maplocmed_colstr = implode(', ', $maplocmed_columns);
        $maplocmed_colqms = implode(', ', str_split(str_repeat('?', count($maplocmed_columns))));
        $queryStr_maplocmed_sel = "SELECT {$maplocmed_colstr} FROM MapLocationMultimedia WHERE fk_maplocation_id IN (%%id_list%%)";
        $queryStr_maplocmed_ins = "INSERT INTO MapLocationMultimedia ({$maplocmed_colstr}) VALUES ({$maplocmed_colqms})";
        if (0 == count($p_copyTranslations)) {
            return;
        }
        $lang_str = implode(', ', $p_copyTranslations);
        $map_ids = array();
        $map_sel_params = array();
        $map_sel_params[] = $p_srcArticleNumber;
        $rows = $g_ado_db->GetAll($queryStr_map_sel, $map_sel_params);
        foreach ($rows as $row) {
            $old_map_id = $row['id'];
            $new_user_id = $p_userId;
            if (is_null($new_user_id)) {
                $new_user_id = $row['IdUser'];
            }
            $map_ins_params = array();
            $map_ins_params[] = $p_destArticleNumber;
            $map_ins_params[] = $row['MapRank'];
            $map_ins_params[] = $row['MapUsage'];
            $map_ins_params[] = $row['MapCenterLongitude'];
            $map_ins_params[] = $row['MapCenterLatitude'];
            $map_ins_params[] = $row['MapDisplayResolution'];
            $map_ins_params[] = $row['MapProvider'];
            $map_ins_params[] = $row['MapWidth'];
            $map_ins_params[] = $row['MapHeight'];
            $map_ins_params[] = $row['MapName'];
            $map_ins_params[] = $new_user_id;
            $success = $g_ado_db->Execute($queryStr_map_ins, $map_ins_params);
            // taking the map ID
            $new_map_id = $g_ado_db->Insert_ID();
            $map_ids[$old_map_id] = $new_map_id;
        }
        if (0 == count($map_ids)) {
            return;
        }
        $map_ids_str = implode(', ', array_keys($map_ids));
        $queryStr_maploc_sel = str_replace($list_fill, $map_ids_str, $queryStr_maploc_sel);
        $maploc_ids = array();
        $maploc_sel_params = array();
        $rows = $g_ado_db->GetAll($queryStr_maploc_sel, $maploc_sel_params);
        foreach ($rows as $row) {
            $old_maploc_id = $row['id'];
            $old_map_id = $row['fk_map_id'];
            $new_map_id = $map_ids[$old_map_id];
            $maploc_ins_params = array();
            $maploc_ins_params[] = $new_map_id;
            $maploc_ins_params[] = $row['fk_location_id'];
            $maploc_ins_params[] = $row['poi_style'];
            $maploc_ins_params[] = $row['rank'];
            $success = $g_ado_db->Execute($queryStr_maploc_ins, $maploc_ins_params);
            // taking the map ID
            $new_maploc_id = $g_ado_db->Insert_ID();
            $maploc_ids[$old_maploc_id] = $new_maploc_id;
        }
        if (0 == count($maploc_ids)) {
            return;
        }
        $maploc_ids_str = implode(', ', array_keys($maploc_ids));
        $queryStr_maploclan_sel = str_replace($list_fill, $maploc_ids_str, $queryStr_maploclan_sel);
        $queryStr_maploclan_sel = str_replace($lang_fill, $lang_str, $queryStr_maploclan_sel);
        $maploclan_sel_params = array();
        $rows = $g_ado_db->GetAll($queryStr_maploclan_sel, $maploclan_sel_params);
        foreach ($rows as $row) {
            $old_maploc_id = $row['fk_maplocation_id'];
            $new_maploc_id = $maploc_ids[$old_maploc_id];
            $maploclan_ins_params = array();
            $maploclan_ins_params[] = $new_maploc_id;
            $maploclan_ins_params[] = $row['fk_language_id'];
            $maploclan_ins_params[] = $row['fk_content_id'];
            $maploclan_ins_params[] = $row['poi_display'];
            $success = $g_ado_db->Execute($queryStr_maploclan_ins, $maploclan_ins_params);
        }
        $queryStr_maplocmed_sel = str_replace($list_fill, $maploc_ids_str, $queryStr_maplocmed_sel);
        $maplocmed_sel_params = array();
        $rows = $g_ado_db->GetAll($queryStr_maplocmed_sel, $maplocmed_sel_params);
        foreach ($rows as $row) {
            $old_maploc_id = $row['fk_maplocation_id'];
            $new_maploc_id = $maploc_ids[$old_maploc_id];
            $maplocmed_ins_params = array();
            $maplocmed_ins_params[] = $new_maploc_id;
            $maplocmed_ins_params[] = $row['fk_multimedia_id'];
            $success = $g_ado_db->Execute($queryStr_maplocmed_ins, $maplocmed_ins_params);
        }
    }

Usage Example

Ejemplo n.º 1
0
    /**
     * Create a copy of this article.
     *
     * @param int $p_destPublicationId -
     *		The destination publication ID.
     * @param int $p_destIssueNumber -
     *		The destination issue number.
     * @param int $p_destSectionNumber -
     * 		The destination section number.
     * @param int $p_userId -
     *		The user creating the copy.  If null, keep the same user ID as the original.
     * @param mixed $p_copyTranslations -
     *		If false (default), only this article will be copied.
     * 		If true, all translations will be copied.
     *		If an array is passed, the translations given will be copied.
     *		Any translations that do not exist will be ignored.
     *
     * @return Article
     *     If $p_copyTranslations is TRUE or an array, return an array of newly created articles.
     *     If $p_copyTranslations is FALSE, return the new Article.
     */
    public function copy($p_destPublicationId = 0, $p_destIssueNumber = 0,
                         $p_destSectionNumber = 0, $p_userId = null,
                         $p_copyTranslations = false)
    {
        global $g_ado_db;

        // It is an optimization to put these here because in most cases
        // you dont need these files.
        require_once($GLOBALS['g_campsiteDir'].'/classes/ArticleImage.php');
        require_once($GLOBALS['g_campsiteDir'].'/classes/ArticleTopic.php');
        require_once($GLOBALS['g_campsiteDir'].'/classes/ArticleAttachment.php');

        $copyArticles = array();
        if ($p_copyTranslations) {
            // Get all translations for this article
            $copyArticles = $this->getTranslations();

            // Remove any translations that are not requested to be translated.
            if (is_array($p_copyTranslations)) {
                $tmpArray = array();
                foreach ($copyArticles as $tmpArticle) {
                    if (in_array($tmpArticle->m_data['IdLanguage'], $p_copyTranslations)) {
                        $tmpArray[] = $tmpArticle;
                    }
                }
                $copyArticles = $tmpArray;
            }
        } else {
            $copyArticles[] = $this;
        }
        $newArticleNumber = $this->__generateArticleNumber();

        // geo-map copying
        if (0 < count($copyArticles))
        {
            $map_user_id = $p_userId;
            if (is_null($map_user_id)) {
                $map_user_id = $this->m_data['IdUser'];
            }

            $map_artilce_src = (int)$this->m_data['Number'];
            $map_artilce_dest = (int)$newArticleNumber;
            $map_translations = array();
            foreach ($copyArticles as $copyMe) {
                $map_translations[] = (int)$copyMe->m_data['IdLanguage'];
            }
            Geo_Map::OnArticleCopy($map_artilce_src, $map_artilce_dest, $map_translations, $map_user_id);
        }

        // Load translation file for log message.
        if (function_exists("camp_load_translation_strings")) {
            camp_load_translation_strings("api");
        }
        $articleOrder = null;
        $logtext = '';
        $newArticles = array();
        foreach ($copyArticles as $copyMe) {
            // Construct the duplicate article object.
            $articleCopy = new Article();
            $articleCopy->m_data['IdPublication'] = (int)$p_destPublicationId;
            $articleCopy->m_data['NrIssue'] = (int)$p_destIssueNumber;
            $articleCopy->m_data['NrSection'] = (int)$p_destSectionNumber;
            $articleCopy->m_data['IdLanguage'] = (int)$copyMe->m_data['IdLanguage'];
            $articleCopy->m_data['Number'] = (int)$newArticleNumber;
            $values = array();
            // Copy some attributes
            $values['ShortName'] = $newArticleNumber;
            $values['Type'] = $copyMe->m_data['Type'];
            $values['OnFrontPage'] = $copyMe->m_data['OnFrontPage'];
            $values['OnSection'] = $copyMe->m_data['OnSection'];
            $values['Public'] = $copyMe->m_data['Public'];
            $values['ArticleOrder'] = $articleOrder;
            $values['Keywords'] = $copyMe->m_data['Keywords'];
            // Change some attributes
            $values['Published'] = 'N';
            $values['IsIndexed'] = 'N';
            $values['LockUser'] = 0;
            $values['LockTime'] = 0;

            if (!is_null($p_userId)) {
                $values['IdUser'] = $p_userId;
            } else {
                $values['IdUser'] = $copyMe->m_data['IdUser'];
            }
            $values['Name'] = $articleCopy->getUniqueName($copyMe->m_data['Name']);

            $articleCopy->__create($values);
            $articleCopy->setProperty('UploadDate', 'NOW()', true, true);
            if (is_null($articleOrder)) {
                $g_ado_db->Execute('LOCK TABLES Articles WRITE');
                $articleOrder = $g_ado_db->GetOne('SELECT MAX(ArticleOrder) + 1 FROM Articles');
                $articleCopy->setProperty('ArticleOrder', $articleOrder);
                $g_ado_db->Execute('UNLOCK TABLES');
            }

            // Insert an entry into the article type table.
            $newArticleData = new ArticleData($articleCopy->m_data['Type'],
                $articleCopy->m_data['Number'],
                $articleCopy->m_data['IdLanguage']);
            $newArticleData->create();
            $origArticleData = $copyMe->getArticleData();
            $origArticleData->copyToExistingRecord($articleCopy->m_data['Number']);

            // Copy image pointers
            ArticleImage::OnArticleCopy($copyMe->m_data['Number'], $articleCopy->m_data['Number']);

            // Copy topic pointers
            ArticleTopic::OnArticleCopy($copyMe->m_data['Number'], $articleCopy->m_data['Number']);

            // Copy file pointers
            ArticleAttachment::OnArticleCopy($copyMe->m_data['Number'], $articleCopy->m_data['Number']);

            // Position the new article at the beginning of the section
            $articleCopy->positionAbsolute(1);

            $newArticles[] = $articleCopy;
            $languageObj = new Language($copyMe->getLanguageId());
            $logtext .= getGS('Article copied to Article #$4 (publication $5, issue $6, section $7).',
                $articleCopy->getArticleNumber(), $articleCopy->getPublicationId(),
                $articleCopy->getIssueNumber(), $articleCopy->getSectionNumber());
        }

        Log::ArticleMessage($copyMe, $logtext, null, 155);
        if ($p_copyTranslations) {
            return $newArticles;
        } else {
          return array_pop($newArticles);
        }
    } // fn copy