public function migrateK2Tags() { $db = $this->dbo; $query = "SELECT `name` from r2e0p_k2_tags ;"; $db->setQuery($query); $rows = $db->loadObjectList(); // Load the tags model. require_once JPATH_ADMINISTRATOR . '/components/com_tags/models/tag.php'; JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tags/tables'); $user = JFactory::getUser(); $userId = $user->id; // For writting permissions foreach ($rows as $key => $row) { error_log("Creaing tag " . $row->name); $tagName = $row->name; $tagAlias = $tagPath = preg_replace('/[\\s\\W\\.]+/', '-', $tagName); // Tags alias filter $tagMetadata = array("author" => "", "robots" => "", "tags" => null); // Get an instance of the table for insertion the new tags $tagsModel = TagsModelTag::getInstance('Tag', 'TagsModel'); // The data tag field row $data = array("parent_id" => 0, "path" => $tagPath, "title" => $tagName, "alias" => $tagAlias, "created_by_alias" => $user, "created_user_id" => $userId, "published" => 1, "checked_out" => 0, "metadata" => json_encode($tagMetadata)); // Finally, store the tag if the user is granted for that $table = $tagsModel->getTable(); $table->bind($data); $retVal = $table->store($data); error_log("Stored tag : . " . $table->id); $tags[] = $table->id; // And store the insert_id } }
/** * Overloaded bind function to pre-process the params. * * @param array $array Named array * @param mixed $ignore Optional array or list of parameters to ignore * * @return null|string null is operation was satisfactory, otherwise returns an error * * @see JTable:bind * @since 1.5 */ public function bind($array, $ignore = '') { if (isset($array['tags']) && !empty($array['tags'])) { // Load the tags helper. require_once JPATH_ADMINISTRATOR . '/components/com_tags/helpers/tags.php'; // Get the allowed actions for the user $canDo = TagsHelper::getActions('com_tags'); // The helper get the user and the component name itself // Load the tags model. require_once JPATH_ADMINISTRATOR . '/components/com_tags/models/tag.php'; JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tags/tables'); // Get an instance of the table for insertion the new tags $tagsModel = TagsModelTag::getInstance('Tag', 'TagsModel'); $tags = array(); // Initialization of the tag container must be processed // If tags is an array, store-mode if (is_array($array['tags'])) { // "Allow user creation" mode must be activated (default) in the component creation field // Save the tags does not exist into the table tags and get its id for save the entire Item with the proper data foreach ($array['tags'] as $singleTag) { // If there is any new tag... create it to get the id and save into the table #__COMPONENT_NAME_TABLE_NAME if (strpos($singleTag, "#new#") !== FALSE) { $user = JFactory::getUser(); $userId = $user->id; // For writting permissions $tagName = str_replace("#new#", "", $singleTag); $tagAlias = $tagPath = preg_replace('/[\\s\\W\\.]+/', '-', $tagName); // Tags alias filter $tagMetadata = array("author" => "", "robots" => "", "tags" => null); // The data tag field row $data = array("parent_id" => 0, "path" => $tagPath, "title" => $tagName, "alias" => $tagAlias, "created_by_alias" => $user, "created_user_id" => $userId, "published" => 1, "checked_out" => 0, "metadata" => json_encode($tagMetadata)); // Finally, store the tag if the user is granted for that if ($canDo->get('core.create')) { $table = $tagsModel->getTable(); $table->bind($data) ? $table->store($data) : exit; $tags[] = $table->id; // And store the insert_id } } else { $tags[] = intval($singleTag); } } // Overrride the tags array, because we should need to change the id before field saving // The field in database will look like "299,345,567,567" $array['tags'] = implode(',', $tags); } } else { $array['tags'] = ''; } // Support for multiple or not foreign key field: cuisines_id if (!empty($array['cuisines_id'])) { if (is_array($array['cuisines_id'])) { $array['cuisines_id'] = implode(',', $array['cuisines_id']); } else { if (strrpos($array['cuisines_id'], ',') != false) { $array['cuisines_id'] = explode(',', $array['cuisines_id']); } } } else { $array['cuisines_id'] = ''; } // Support for multiple or not foreign key field: meal_course_id if (!empty($array['meal_course_id'])) { if (is_array($array['meal_course_id'])) { $array['meal_course_id'] = implode(',', $array['meal_course_id']); } else { if (strrpos($array['meal_course_id'], ',') != false) { $array['meal_course_id'] = explode(',', $array['meal_course_id']); } } } else { $array['meal_course_id'] = ''; } $input = JFactory::getApplication()->input; $task = $input->getString('task', ''); if (($task == 'save' || $task == 'apply') && (!JFactory::getUser()->authorise('core.edit.state', 'com_akrecipes') && $array['state'] == 1)) { $array['state'] = 0; } if ($array['id'] == 0) { $array['created_by'] = JFactory::getUser()->id; } if ($array['id'] == 0) { $array['modified_by'] = JFactory::getUser()->id; } // Support for checkbox field: sponsored if (!isset($array['sponsored'])) { $array['sponsored'] = 0; } // Support for checkbox field: featured if (!isset($array['featured'])) { $array['featured'] = 0; } if (isset($array['params']) && is_array($array['params'])) { $registry = new JRegistry(); $registry->loadArray($array['params']); $array['params'] = (string) $registry; } if (isset($array['metadata']) && is_array($array['metadata'])) { $registry = new JRegistry(); $registry->loadArray($array['metadata']); $array['metadata'] = (string) $registry; } if (!JFactory::getUser()->authorise('core.admin', 'com_akrecipes.recipe.' . $array['id'])) { $actions = JAccess::getActionsFromFile(JPATH_ADMINISTRATOR . '/components/com_akrecipes/access.xml', "/access/section[@name='recipe']/"); $default_actions = JAccess::getAssetRules('com_akrecipes.recipe.' . $array['id'])->getData(); $array_jaccess = array(); foreach ($actions as $action) { $array_jaccess[$action->name] = $default_actions[$action->name]; } $array['rules'] = $this->JAccessRulestoArray($array_jaccess); } // Bind the rules for ACL where supported. if (isset($array['rules']) && is_array($array['rules'])) { $this->setRules($array['rules']); } return parent::bind($array, $ignore); }