Exemplo n.º 1
0
 /**
  * Method to save language associations
  *
  * @return  boolean True if successful
  */
 function saveAssociations(&$item, &$data)
 {
     $item = $item ? $item : $this->_category;
     $context = 'com_categories';
     return flexicontent_db::saveAssociations($item, $data, $context);
 }
Exemplo n.º 2
0
 /**
  * Method to copy items
  *
  * @access	public
  * @return	boolean	True on success
  * @since	1.5
  */
 function copyitems($cid, $keeptags = 1, $prefix, $suffix, $copynr = 1, $lang = null, $state = null, $method = 1, $maincat = null, $seccats = null, $type_id = null, $access = null)
 {
     $app = JFactory::getApplication();
     $jinput = $app->input;
     $dbprefix = $app->getCfg('dbprefix');
     $use_versioning = $this->cparams->get('use_versioning', 1);
     require_once "components/com_flexicontent/models/item.php";
     // ************************************************************************
     // Try to find Falang/Joomfish, to import translation data, if so requested
     // ************************************************************************
     $_FALANG = false;
     $this->_db->setQuery('SHOW TABLES LIKE "' . $dbprefix . 'falang_content"');
     $_FALANG = (bool) count($this->_db->loadObjectList());
     // Try to find old joomfish tables (with current DB prefix)
     $this->_db->setQuery('SHOW TABLES LIKE "' . $dbprefix . 'jf_content"');
     $_FISH = (bool) count($this->_db->loadObjectList());
     // Try to find old joomfish tables (with J1.5 jos prefix)
     if (!$_FISH) {
         $this->_db->setQuery('SHOW TABLES LIKE "jos_jf_content"');
         if (count($this->_db->loadObjectList())) {
             $_FISH = true;
             $dbprefix = 'jos_';
         }
     }
     // Detect version of joomfish tables
     $_FISH22GE = false;
     if ($_FISH) {
         $this->_db->setQuery('SHOW TABLES LIKE "' . $dbprefix . 'jf_languages_ext"');
         $_FISH22GE = (bool) count($this->_db->loadObjectList());
     }
     $_NEW_LANG_TBL = FLEXI_J16GE || $_FISH22GE;
     // Get if translation is to be performed, 1: FLEXI_DUPLICATEORIGINAL,  2: FLEXI_USE_JF_DATA,  3: FLEXI_AUTO_TRANSLATION,  4: FLEXI_FIRST_JF_THEN_AUTO
     if ($method == 99) {
         $translate_method = $jinput->get('translate_method', 1, 'int');
     } else {
         $translate_method = 0;
     }
     // If translation method import the translator class
     if ($translate_method == 3 || $translate_method == 4) {
         require_once JPATH_COMPONENT_SITE . DS . 'helpers' . DS . 'translator.php';
     }
     // If translation method load description field to allow some parsing according to parameters
     if ($translate_method == 3 || $translate_method == 4) {
         $this->_db->setQuery('SELECT id FROM #__flexicontent_fields WHERE name = "text" ');
         $desc_field_id = $this->_db->loadResult();
         $desc_field = JTable::getInstance('flexicontent_fields', '');
         $desc_field->load($desc_field_id);
     }
     // ************************************************************
     // Loop through the items, copying, moving, or translating them
     // ************************************************************
     foreach ($cid as $itemid) {
         for ($nr = 0; $nr < $copynr; $nr++) {
             // (a) Get existing item
             $item = JTable::getInstance('flexicontent_items', '');
             $item->load($itemid);
             // Some shortcuts
             $sourceid = (int) $item->id;
             $curversion = (int) $item->version;
             // (b) We create copy so that the original data are always available
             $row = clone $item;
             // (c) Force creation & assigning of new records by cleaning the primary keys
             $row->id = null;
             // force creation of new record in _content DB table
             $row->item_id = null;
             // force creation of new record in _flexicontent_ext DB table
             $row->asset_id = null;
             // force creation of new record in _assets DB table
             // (d) Start altering the properties of the cloned item
             $row->title = ($prefix ? $prefix . ' ' : '') . $item->title . ($suffix ? ' ' . $suffix : '');
             $row->hits = 0;
             if (!$translate_method) {
                 // cleared featured flag if not translating
                 $row->featured = 0;
             }
             $row->version = 1;
             $datenow = JFactory::getDate();
             $row->created = $datenow->toSql();
             $row->publish_up = $datenow->toSql();
             $row->modified = $nullDate = $this->_db->getNullDate();
             $lang_from = substr($row->language, 0, 2);
             $row->language = $lang ? $lang : $row->language;
             $lang_to = substr($row->language, 0, 2);
             $row->state = strlen($state) ? $state : $row->state;
             // keep original if: null, ''
             $row->type_id = $type_id ? $type_id : $row->type_id;
             // keep original if: null, zero, ''
             $row->access = $access ? $access : $row->access;
             // keep original if: null, zero, ''
             $doauto['title'] = $doauto['introtext'] = $doauto['fulltext'] = $doauto['metakey'] = $doauto['metadesc'] = true;
             // In case JF data is missing
             if ($translate_method == 2 || $translate_method == 4) {
                 // a. Try to get joomfish/falang translation from the item
                 $jfitemfields = false;
                 if ($_FALANG) {
                     $query = "SELECT c.* FROM `#__falang_content` AS c " . " LEFT JOIN #__languages AS lg ON c.language_id=lg.lang_id" . " WHERE c.reference_table = 'content' AND lg.lang_code='" . $row->language . "' AND c.reference_id = " . $sourceid;
                     $this->_db->setQuery($query);
                     $jfitemfields = $this->_db->loadObjectList();
                 }
                 if (!$jfitemfields && $_FISH) {
                     $query = "SELECT c.* FROM `" . $dbprefix . "jf_content` AS c " . " LEFT JOIN #__languages AS lg ON c.language_id=" . ($_NEW_LANG_TBL ? "lg.lang_id" : "lg.id") . " WHERE c.reference_table = 'content' AND " . ($_NEW_LANG_TBL ? "lg.lang_code" : "lg.code") . "='" . $row->language . "' AND c.reference_id = " . $sourceid;
                     $this->_db->setQuery($query);
                     $jfitemfields = $this->_db->loadObjectList();
                 }
                 // b. if joomfish translation found set for the new item
                 if ($jfitemfields) {
                     $jfitemdata = new stdClass();
                     foreach ($jfitemfields as $jfitemfield) {
                         $jfitemdata->{$jfitemfield->reference_field} = $jfitemfield->value;
                     }
                     if (isset($jfitemdata->title) && mb_strlen($jfitemdata->title) > 0) {
                         $row->title = $jfitemdata->title;
                         $doauto['title'] = false;
                     }
                     if (isset($jfitemdata->alias) && mb_strlen($jfitemdata->alias) > 0) {
                         $row->alias = $jfitemdata->alias;
                     }
                     if (isset($jfitemdata->introtext) && mb_strlen(strip_tags($jfitemdata->introtext)) > 0) {
                         $row->introtext = $jfitemdata->introtext;
                         $doauto['introtext'] = false;
                     }
                     if (isset($jfitemdata->fulltext) && mb_strlen(strip_tags($jfitemdata->fulltext)) > 0) {
                         $row->fulltext = $jfitemdata->fulltext;
                         $doauto['fulltext'] = false;
                     }
                     if (isset($jfitemdata->metakey) && mb_strlen($jfitemdata->metakey) > 0) {
                         $row->metakey = $jfitemdata->metakey;
                         $doauto['metakey'] = false;
                     }
                     if (isset($jfitemdata->metadesc) && mb_strlen($jfitemdata->metadesc) > 0) {
                         $row->metadesc = $jfitemdata->metadesc;
                         $doauto['metadesc'] = false;
                     }
                 }
             }
             // Try to do automatic translation from the item, if autotranslate is SET and --NOT found-- or --NOT using-- JoomFish Data
             if ($translate_method == 3 || $translate_method == 4) {
                 // Translate fulltext item property, using the function for which handles custom fields TYPES: text, textarea, ETC
                 if ($doauto['fulltext']) {
                     $desc_field->value = $row->fulltext;
                     $fields = array(&$desc_field);
                     $this->translateFieldValues($fields, $row, $lang_from, $lang_to);
                     $row->fulltext = $desc_field->value;
                 }
                 // TRANSLATE basic item properties (if not already imported via Joomfish)
                 $translatables = array('title', 'introtext', 'metakey', 'metadesc');
                 $fieldnames_arr = array();
                 $fieldvalues_arr = array();
                 foreach ($translatables as $translatable) {
                     if (!$doauto[$translatable]) {
                         continue;
                     }
                     $fieldnames_arr[] = $translatable;
                     $translatable_obj = new stdClass();
                     $translatable_obj->originalValue = $row->{$translatable};
                     $translatable_obj->noTranslate = false;
                     $fieldvalues_arr[] = $translatable_obj;
                 }
                 if (count($fieldvalues_arr)) {
                     $result = autoTranslator::translateItem($fieldnames_arr, $fieldvalues_arr, $lang_from, $lang_to);
                     if (intval($result)) {
                         $n = 0;
                         foreach ($fieldnames_arr as $fieldname) {
                             $row->{$fieldname} = $fieldvalues_arr[$n]->translationValue;
                             $n++;
                         }
                     }
                 }
             }
             //print_r($row->fulltext); exit;
             // Create a new item in the content fc_items_ext table
             $row->store();
             // Not doing a translation, we start a new language group for the new item
             if ($translate_method == 0) {
                 $row->lang_parent_id = 0;
                 //$row->id;
                 $row->store();
             }
             // ***********************************************************
             // Copy custom fields, translating the fields if so configured
             // ***********************************************************
             $doTranslation = $translate_method == 3 || $translate_method == 4;
             $query = 'SELECT fir.*, f.* ' . ' FROM #__flexicontent_fields_item_relations as fir' . ' LEFT JOIN #__flexicontent_fields as f ON f.id=fir.field_id' . ' WHERE item_id = ' . $sourceid;
             $this->_db->setQuery($query);
             $fields = $this->_db->loadObjectList();
             //echo "<pre>"; print_r($fields); exit;
             if ($doTranslation) {
                 $this->translateFieldValues($fields, $row, $lang_from, $lang_to);
             }
             //foreach ($fields as $field)  if ($field->field_type!='text' && $field->field_type!='textarea') { print_r($field->value); echo "<br><br>"; }
             foreach ($fields as $field) {
                 if (strlen($field->value)) {
                     $query = 'INSERT INTO #__flexicontent_fields_item_relations (`field_id`, `item_id`, `valueorder`, `suborder`, `value`)' . ' VALUES(' . $field->field_id . ', ' . $row->id . ', ' . $field->valueorder . ', ' . $field->suborder . ', ' . $this->_db->Quote($field->value) . ')';
                     $this->_db->setQuery($query);
                     $this->_db->execute();
                 }
             }
             if ($use_versioning) {
                 $v = new stdClass();
                 $v->item_id = (int) $item->id;
                 $v->version_id = 1;
                 $v->created = $item->created;
                 $v->created_by = $item->created_by;
                 //$v->comment		= 'copy version.';
                 $this->_db->insertObject('#__flexicontent_versions', $v);
             }
             // get the items versions
             $query = 'SELECT *' . ' FROM #__flexicontent_items_versions' . ' WHERE item_id = ' . $sourceid . ' AND version = ' . $curversion;
             $this->_db->setQuery($query);
             $curversions = $this->_db->loadObjectList();
             foreach ($curversions as $cv) {
                 $query = 'INSERT INTO #__flexicontent_items_versions (`version`, `field_id`, `item_id`, `valueorder`, `suborder`, `value`)' . ' VALUES(1 ,' . $cv->field_id . ', ' . $row->id . ', ' . $cv->valueorder . ', ' . $cv->suborder . ', ' . $this->_db->Quote($cv->value) . ')';
                 $this->_db->setQuery($query);
                 $this->_db->execute();
             }
             // get the item categories
             $query = 'SELECT catid' . ' FROM #__flexicontent_cats_item_relations' . ' WHERE itemid = ' . $sourceid;
             $this->_db->setQuery($query);
             $cats = $this->_db->loadColumn();
             foreach ($cats as $cat) {
                 $query = 'INSERT INTO #__flexicontent_cats_item_relations (`catid`, `itemid`)' . ' VALUES(' . $cat . ',' . $row->id . ')';
                 $this->_db->setQuery($query);
                 $this->_db->execute();
             }
             if ($keeptags) {
                 // get the item tags
                 $query = 'SELECT tid' . ' FROM #__flexicontent_tags_item_relations' . ' WHERE itemid = ' . $sourceid;
                 $this->_db->setQuery($query);
                 $tags = $this->_db->loadColumn();
                 foreach ($tags as $tag) {
                     $query = 'INSERT INTO #__flexicontent_tags_item_relations (`tid`, `itemid`)' . ' VALUES(' . $tag . ',' . $row->id . ')';
                     $this->_db->setQuery($query);
                     $this->_db->execute();
                 }
             }
             if ($method == 3) {
                 $this->moveitem($row->id, $maincat, $seccats);
             } else {
                 if ($method == 99 && ($maincat || $seccats)) {
                     $row->catid = $maincat ? $maincat : $row->catid;
                     $this->moveitem($row->id, $row->catid, $seccats);
                 }
             }
             // Load item model and save it once, e.g. updating Joomla featured FLAG data
             //$itemmodel = new FlexicontentModelItem();
             //$itemmodel->getItem($row->id);
             //$itemmodel->store((array)$row);
             // If new item is a tranlation, load the language associations of item
             // that was copied, and save the associations, adding the new item to them
             if ($method == 99 && $item->language != '*' && $row->language != '*' && flexicontent_db::useAssociations()) {
                 $associations = JLanguageAssociations::getAssociations('com_content', '#__content', 'com_content.item', $item->id);
                 // associations of item that was copied
                 $_data = array();
                 foreach ($associations as $tag => $association) {
                     $_data['associations'][$tag] = (int) $association->id;
                 }
                 $_data['associations'][$row->language] = $row->id;
                 // Add new item itself
                 $_data['associations'][$item->language] = $item->id;
                 // unneeded, done by saving ...
                 $context = 'com_content';
                 flexicontent_db::saveAssociations($row, $_data, $context);
                 // Save associations, adding the new item
                 //$app->enqueueMessage( print_r($_data, true), 'message' );
             }
         }
     }
     return true;
 }