Create Flexicontent item in custom code

More
9 years 2 months ago - 8 years 2 months ago #56184 by JeBy
Hi everyone,

I want to be able to create an item through AJAX so my users don't have to go through the whole form when having to add a new item quickly and thanks to the article here I had an explanation of the Model:

www.flexicontent.org/documentation/faq/7...-in-custom-code.html

However, when I implemented this code I get the following errors:

Warning: Creating default object from empty value in /.../administrator/components/com_flexicontent/models/parentclassitem.php on line 1627

Fatal error: Class 'flexicontent_db' not found in /.../administrator/components/com_flexicontent/models/parentclassitem.php on line 1698


Here's the code:
Code:
<?php // 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(); $fieldid = $_REQUEST['fieldid']; $title = $_REQUEST['title']; // Create the item model object, WARNING !!! you may have to import more files HERE !! require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'models'.DS.'item.php'); $item_model = new FlexicontentModelItem(); if (isset($fieldid)) { $db = JFactory::getDbo(); // GET TYPE $query = $db->getQuery(true); $query->select('type_id'); $query->from('#__flexicontent_fields_type_relations'); $query->where('field_id = '.$fieldid.''); $db->setQuery($query); $type = $db->loadResult(); // GET CATEGORY $query = $db->getQuery(true); $query->select('attribs'); $query->from('#__flexicontent_types'); $query->where('id = '.$type.''); $db->setQuery($query); $attribs = json_decode($db->loadResult()); $cat = $attribs->catid_default; } // Create the new item data $data = array(); $data['id'] = 0; // indicate a new item will be created // Type and language $data['type_id'] = $type; // e.g. 1 for article $data['language']= 'en-GB'; // e.g. 'en-GB' // main category, secondary categories and tags $data['catid'] = $cat; // INTEGER ... the main category of the item $data['cid'] = ''; //e.g. array(55,117,56) an array of the secondary categories of the item $data['tag'] = ''; //e.g. 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'] = $title; $data['text'] = ''; // 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 item and log the result in file and on screen if ( !$item_model->store($data) ) { $msg = 'Failed to create the new item '. $item_model->getError(); //JLog::add($msg); echo $msg."<br/>"; } echo json_encode($item_model->getId()); ?>

Anyone know what I'm supposed to do?? Thanks ahead!
Last edit: 8 years 2 months ago by JeBy.

Please Log in or Create an account to join the conversation.

More
9 years 2 months ago #56229 by ggppdk
Hello

i will test / update the FAQ article, please post remind on Monday, if you don't get a responce by then


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

More
9 years 2 months ago #56281 by JeBy
Hi again! Did you get a chance to look at the code? I'm kinda stuck with my project without this feature :) Thanks ahead!

Please Log in or Create an account to join the conversation.

More
9 years 2 months ago - 9 years 2 months ago #56297 by ggppdk
Hello

i have update the article:
www.flexicontent.org/documentation/faq/7...-in-custom-code.html

so the start of your file should be the following and then add your code
-- also,
we assume that your custom script is located in your JOOMLA root folder,
e.g. if it is located in a subfolder: custom/myscripts/ then use:
Code:
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
Code:
<?php // ********************* // Load 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 define( 'JPATH_COMPONENT_SITE', JPATH_SITE.DS.'components'.DS.'com_flexicontent' ); define( 'JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent' ); // Create a log file $log_filename = 'item_creation_'.($user->id).'.php'; jimport('joomla.log.log'); JLog::addLogger(array('text_file' => $log_filename)); // ************************************** // Include the needed classes and helpers // ************************************** require_once (JPATH_COMPONENT_SITE.DS.'classes'.DS.'flexicontent.helper.php'); require_once (JPATH_COMPONENT_SITE.DS.'classes'.DS.'flexicontent.categories.php'); require_once (JPATH_COMPONENT_SITE.DS.'classes'.DS.'flexicontent.fields.php'); require_once (JPATH_COMPONENT_SITE.DS.'classes'.DS.'flexicontent.acl.php'); require_once (JPATH_COMPONENT_SITE.DS.'helpers'.DS.'permission.php'); require_once (JPATH_COMPONENT_SITE.DS.'helpers'.DS.'route.php'); // Add component's table directory to the include path JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.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 // **************************** require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'models'.DS.'item.php'); $item_model = new FlexicontentModelItem();


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...
Last edit: 9 years 2 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

More
8 years 5 months ago #62175 by JeBy
This is not working for me anymore. I get "Failed to create the new item" with no error message to debug. Can you confirm it working for you with the latest build?

www.flexicontent.org/documentation/faq/7...-in-custom-code.html

Thanks!

Please Log in or Create an account to join the conversation.

More
8 years 3 months ago #63117 by JeBy
Is there any progress on fixing this code so it works with the current version of Flexicontent?

Thank you so much!

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.589 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline