Create Flexicontent item in custom code

More
10 years 5 months ago - 9 years 5 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: 9 years 5 months ago by JeBy.

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

More
10 years 5 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 review. Thanks!

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

More
10 years 5 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
10 years 5 months ago - 10 years 5 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 review. Thanks!
Last edit: 10 years 5 months ago by ggppdk.

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

More
9 years 8 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
9 years 6 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.

More
9 years 6 months ago - 9 years 6 months ago #63120 by ggppdk
Hello

i have re-tested it works with FLEXIcontent
v3.0.14-rc2a
v3.0.15-rc

(in v3.0.14-rc2a you will get 2 harmless notices if you have PHP 7)

Please note that the example code has:
- 6 PHP syntax errors , places with: ...


you are supposed to corrected the syntax errors and add appropriate values
- i have edited the FAQ article and made minor changes 2 comments


-- 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 review. Thanks!
Last edit: 9 years 6 months ago by ggppdk.

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

More
9 years 6 months ago - 9 years 6 months ago #63476 by JeBy
Hi again. This is still not working for me. I'm getting the error "Failed to create the new item" but no details of how to resolve the error are given (I had to change $item->getError() to $item_model->getError() btw. I did fill in all the blank spaces so it's not that.

I was wondering if the function 'store' does not expect to get an author or at least something to verify that the user CAN add a new item, even if it's from an external script?

Thanks again for all the help!

This is the log:

#Date: 2016-08-02 18:52:15 UTC
#Software: Joomla Platform 13.1.0 Stable [ Curiosity ] 24-Apr-2013 00:00 GMT

#Fields: datetime priority clientip category message
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SELECT extension_id AS id, element AS "option", params, enabled
FROM ddb_extensions
WHERE `type` = 'component'
2016-08-02T18:52:15+00:00 WARNING 78.240.140.75 deprecated JRequest is deprecated.
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SELECT lft,rgt FROM ddb_categories WHERE id=1
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SELECT b.id
FROM ddb_user_usergroup_map AS map
LEFT JOIN ddb_usergroups AS a ON a.id = map.group_id
LEFT JOIN ddb_usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt
WHERE map.user_id = 885
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SELECT id, rules
FROM `ddb_viewlevels`
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SELECT folder AS type, element AS name, params
FROM ddb_extensions
WHERE enabled = 1 AND type ='plugin' AND state IN (0,1) AND access IN (1,1,2,3,6)
ORDER BY ordering
2016-08-02T18:52:15+00:00 WARNING 78.240.140.75 deprecated JFactory::getAcl is deprecated. Use JAccess directly.
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SHOW FULL COLUMNS FROM `ddb_flexicontent_items_tmp`
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SHOW FULL COLUMNS FROM `ddb_content`
2016-08-02T18:52:15+00:00 DEBUG 78.240.140.75 databasequery SELECT author_basicparams FROM ddb_flexicontent_authors_ext WHERE user_id = 885
2016-08-02T18:52:16+00:00 DEBUG 78.240.140.75 databasequery SHOW FULL COLUMNS FROM `ddb_assets`
2016-08-02T18:52:16+00:00 DEBUG 78.240.140.75 databasequery SELECT *
FROM ddb_assets
WHERE `id` = '1'
2016-08-02T18:52:16+00:00 DEBUG 78.240.140.75 databasequery SELECT a.rules
FROM ddb_assets AS a
WHERE (a.id = 1 OR a.name = 'root.1')
2016-08-02T18:52:16+00:00 INFO 78.240.140.75 - Failed to create the new item
2016-08-02T18:52:16+00:00 DEBUG 78.240.140.75 databasequery SELECT *
FROM `ddb_users`
WHERE `id` = 885
2016-08-02T18:52:16+00:00 DEBUG 78.240.140.75 databasequery SELECT `g`.`id`,`g`.`title`
FROM `ddb_usergroups` AS g
INNER JOIN `ddb_user_usergroup_map` AS m ON m.group_id = g.id
WHERE `m`.`user_id` = 885
2016-08-02T18:52:16+00:00 DEBUG 78.240.140.75 databasequery UPDATE `ddb_session`
SET `data` = 'joomla|s:1972:\"TzoyNDoiSm9vbWxhXFJlZ2lzdHJ5XFJlZ2lzdHJ5IjoyOntzOjc6IgAqAGRhdGEiO086ODoic3RkQ2xhc3MiOjI6e3M6OToiX19kZWZhdWx0IjtPOjg6InN0ZENsYXNzIjo2OntzOjc6InNlc3Npb24iO086ODoic3RkQ2xhc3MiOjQ6e3M6NzoiY291bnRlciI7aTo5OTtzOjU6InRpbWVyIjtPOjg6InN0ZENsYXNzIjozOntzOjU6InN0YXJ0IjtpOjE0NzAxNDA4MzM7czo0OiJsYXN0IjtpOjE0NzAxNjI5NTc7czozOiJub3ciO2k6MTQ3MDE2MzkzNTt9czo2OiJjbGllbnQiO086ODoic3RkQ2xhc3MiOjE6e3M6OToiZm9yd2FyZGVkIjtzOjEzOiI3OC4yNDAuMTQwLjc1Ijt9czo1OiJ0b2tlbiI7czozMjoiRFNHb1o5YjdhaGZMR2xYYWxVY0laT2Z2cU9OZE5RaEsiO31zOjg6InJlZ2lzdHJ5IjtPOjI0OiJKb29tbGFcUmVnaXN0cnlcUmVnaXN0cnkiOjI6e3M6NzoiACoAZGF0YSI7Tzo4OiJzdGRDbGFzcyI6Mzp7czo5OiJjb21fdXNlcnMiO086ODoic3RkQ2xhc3MiOjE6e3M6NDoiZWRpdCI7Tzo4OiJzdGRDbGFzcyI6MTp7czo3OiJwcm9maWxlIjtPOjg6InN0ZENsYXNzIjoxOntzOjI6ImlkIjtpOjg4NTt9fX1zOjE0OiJjb21famZiY29ubmVjdCI7Tzo4OiJzdGRDbGFzcyI6MTp7czo4OiJmYWNlYm9vayI7Tzo4OiJzdGRDbGFzcyI6MTp7czoxNDoibWFwcGVkX3VzZ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iLiI7fQ==\";'
, `time` = '1470163936'
WHERE `session_id` = '8bht25rojtaa0sfl2ltk1e31k0'

Last edit: 9 years 6 months ago by JeBy. Reason: Added log

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

More
9 years 5 months ago #63529 by JeBy
Any idea how to solve this issue using the info in the log? I hope this gets resolved so I can get off your back ;) Thanks again for the great work!

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

More
9 years 5 months ago #63530 by ggppdk
Are you calling code
- from a frontend script ?
- from a backend script ?
- from a Joomla CLI script ?


-- 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 review. Thanks!

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

Moderators: vistamediajoomlacornerggppdk
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
Save