NOTE:
- the following was test but it may be incomplete if you have a fix / suggestion for the following then post in our forums
// Create a log file
$user = JFactory::getUser(); $log_filename = 'item_creation_'.($user->id).'.php'; jimport('joomla.log.log'); JLog::addLogger(array('text_file' => $log_filename)); error_reporting(E_ALL & ~E_STRICT);
ini_set('display_errors',1);
// ************************************** // Include the needed classes and helpers // **************************************
if (!defined('DS')) define('DS',DIRECTORY_SEPARATOR);
require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'classes'.DS.'flexicontent.helper.php'); require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'classes'.DS.'flexicontent.categories.php'); require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'classes'.DS.'flexicontent.fields.php'); require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'helpers'.DS.'permission.php'); require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'helpers'.DS.'route.php'); // Add component's table directory to the include path JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'tables'); // ******************* // Load language files // ******************* // (BACKEND) Load english language file for 'com_content' component then override with current language file JFactory::getLanguage()->load('com_content', JPATH_ADMINISTRATOR, 'en-GB', true); JFactory::getLanguage()->load('com_content', JPATH_ADMINISTRATOR, null, true); // (BACKEND) Load english language file for 'com_flexicontent' component then override with current language file JFactory::getLanguage()->load('com_flexicontent', JPATH_ADMINISTRATOR, 'en-GB', true); JFactory::getLanguage()->load('com_flexicontent', JPATH_ADMINISTRATOR, null, true); // (FRONTEND) Load english language file for 'com_content' component then override with current language file JFactory::getLanguage()->load('com_content', JPATH_SITE, 'en-GB', true); JFactory::getLanguage()->load('com_content', JPATH_SITE, null, true); // (FRONTEND) Load english language file for 'com_flexicontent' component then override with current language file JFactory::getLanguage()->load('com_flexicontent', JPATH_SITE, 'en-GB', true); JFactory::getLanguage()->load('com_flexicontent', JPATH_SITE, null, true); // **************************** // Create the item model object // ****************************
JFactory::getApplication()->isAdmin()
? require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'models'.DS.'item.php') : require_once(JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'models'.DS.'item.php'); $item_model = new FlexicontentModelItem(); // Create the new item data $data = array(); // 444 is the id of the item that you want to load, or set it to zero for new item $item_ID = 0; // 444; if ($item_ID) { // EITHER Load existing item into the ITEM model
$item = $item_model->getItem($item_ID, $check_view_access=false, $no_cache=true, $force_version=0); // You can use $item to get existing values if (!$item) die('item '.$item_ID.' not found'); // IMPORTANT: Get existing field values for the item $items = array($item);
$items_custom_values = FlexicontentFields::getCustomFieldValues($items, 'item'); $data['custom'] = reset($items_custom_values); // Get data of first item
// Indicate which item to update
$data['id'] = $item_ID; } else { // OR indicate a new item will be created $data['id'] = 0; } // Type and language $data['type_id'] = ...; // e.g. 1 for article, !! DO NOT CHANGE for existing item $data['language']= ...; // e.g. 'en-GB' // main category, secondary categories and tags $data['catid'] = ...; // INTEGER ... the main category of the item $data['cid'] = ...; // e.g. array() or ... array(55,117,56) an array of the secondary categories of the item $data['tag'] = ...; // e.g. array() or ... array(13,43,34) an array of the tags of the item $data['vstate'] = 2; // item version is approved $data['state'] = 1; // 1 for published ... $data['title'] = 'this is the title of the item'; $data['text'] = 'this is the description of the item'; // Custom fields $data['custom']['field1'] = 'this value of field 1'; $data['custom']['field2'][0] = 'this value 0 of field 2'; $data['custom']['field2'][1] = 'this value 1 of field 2'; $data['custom']['field2'][2] = 'this value 2 of field 2'; // Store the new / existing item and log the result in file and on screen if ( !$item_model->store($data) ) { $msg = 'Failed to store item: '. $item_model->getError(); JLog::add($msg); echo $msg."<br/>"; }
Also if the above IS NOT inside a Joomla extension, then you will need to load the Joomla Framework before calling the above code, by using:
// Get Joomla! framework define( '_JEXEC', 1 ); define( '_VALID_MOS', 1 ); define( 'JPATH_BASE', realpath(dirname(__FILE__)) ); define( 'DS', DIRECTORY_SEPARATOR ); require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); $app = JFactory::getApplication('site'); $app->initialise(); // Define component paths IF FLEXIcontent v3.1.0 or older // You do not need them for v3.1.1 or later !! define( 'JPATH_COMPONENT_SITE', JPATH_SITE.DS.'components'.DS.'com_flexicontent' ); define( 'JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent' );
In the above we assume that you custom script is located in your site root, if it is located in a subfolder: custom/myscripts/ then use:
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));