Coderman5000
Total posts: 7
10 Nov 2014 23:28

Hi there,

I want to create a Restful API that creates a new Cobalt Section. I think I have figured out how to make a restful API call to Joomla but I am not sure how to interact with cobalt to create a new section, type, etc... Any help?

Thanks

Last Modified: 04 Dec 2014


Sergey
Total posts: 13,748
11 Nov 2014 04:16

Cobalt is a native Joomla extension. SO it use the same technique as in all other Joomla core extensions. If you know how, you can use modules. Or you can simple create SQL query and work with DB directly.

Try to create modele instance

JTable::addIncludePath(JPATH_ROOT . '/administrator/components/com_cobalt/tables');
JModelLegacy::addIncludePath(JPATH_ROOT . '/components/com_cobalt/models');
$model = JModelLegacy::getInstance('Section', 'CobaltBModel');

$model->save($data);

where $data is an array.


Coderman5000
Total posts: 7
12 Nov 2014 18:26

Wow, Thank you for the quick response!

I think I understand this a little better now. I know now that I really need to create a new record not a new section. Knowing how to create a new section would be a good step to learn also. Do you know of any good resources for putting me on the right track if my end goal is to create new records, sections, types, etc.. using php in the backend?

Thank you.


Sergey
Total posts: 13,748
14 Nov 2014 02:55

All that is a very complex things. I can give you important hint. Whatever you do make sure that you do not dublicate creation process but utilize Cobalt models.

What is in my mind is that you can send POST request to real save url

/index.php?option=com_cobalt&task=record.save&id=123

ID is optional. Null for new records.

And as POST variales you can set anything you what by looking in to article form HTML source you can understand what should be the names of those post values.

Or you can create small wrapper. Your own URL that will check POST variables and then execute cobalt controller with task just like it is done in cobalt.php file. Or you even can include cobalt.php to your file for rest of processing.


Coderman5000
Total posts: 7
14 Nov 2014 22:48

Sergey,

Thank you once more for your quick response. I am trying to use the exisiting creation process in Cobalt to create a new record.

Is the only way to implement a new record in cobalt through a HTML form submission?

I was hoping there was a method on a class I could call.

Im interested in using components\com_cobalt\cobalt.php for processing.

$controller = JControllerLegacy::getInstance('Cobalt');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();

Calls the defualt method "display" in components\com_cobalt\controller.php

are you suggesting I replace the defualt 'task' in the above code with:

/index.php?option=com_cobalt&task=record.save&id=123

That doesn't make sense to me since there is no record method in components\com_cobalt\controller.php

Thank you for your patience and wisdom.

I would also love to Skype if you have time. Whatever time zone you are in is good. My Skype ID is dyami.delia

Thank you


Sergey
Total posts: 13,748
17 Nov 2014 04:36
$_POST['jform']['title'] = 'My New record';
$_POST['jform']['category'][] = 12;
$_POST['jform']['fields'][24] = 'Single field';
$_POST['jform']['fields'][10][] = 'Multi field 1';
$_POST['jform']['fields'][10][] = 'Multi field 2';


$controller = JControllerLegacy::getInstance('Cobalt');
$controller->execute('record.save');
$controller->redirect();

This is a concept. You create POST acording to record form. And then you just call a controller.

But look cobalt.php and all files it includes to be executed and do the same before executing controller. Or instead of executing cobalt controller just include cobalt.php where is it executed.


Coderman5000
Total posts: 7
18 Nov 2014 21:50

Hi there,

Thanks for all the help I definatly owe you!

So I have formatted all of my data into $_POST requests like you mentioned above.

I don't understand the bottom three lines. I tried to do:

require_once JPATH_ROOT . '\components\com_cobalt\library\php\helpers\helper.php';

$controller = JControllerLegacy::getInstance('Cobalt');
$controller->execute('record.save');
$controller->redirect();

But all I get is an Invalid Controller error.

I also tried to just access the save function directly

require_once JPATH_ROOT . '\components\com_cobalt\controllers\form.php';

$controller = new CobaltControllerForm();
$controller->save($key, $urlVar);   

But I am getting an invalid Token error.

Any Ideas?

Thank you.


Sergey
Total posts: 13,748
20 Nov 2014 10:54

Because cobalt depend on so much more things. As I said, open cobalt.php and see what else is loaded thete. Or simple use

require_once JPATH_ROOT . '/administrator/components/com_cobalt/cobalt.php';

Coderman5000
Total posts: 7
21 Nov 2014 23:10

Hey Sergey.

I ended up getting My method to work WITH EDITS

my code looks like so

//imports
jimport('joomla.application.component.controllerform');
jimport('joomla.application.component.controller');
require_once JPATH_ROOT . '\components\com_cobalt\library\php\helpers\helper.php';
require_once JPATH_ROOT . '\components\com_cobalt\controllers\form.php';
require_once JPATH_ROOT . '\libraries\joomla\session\session.php';

//make the post data look like my record
 $_POST['jform']['title'] = 'TestCase8';
 $_POST['jform']['ucatid'] = '0';
 //etc...


//INstance Joomla Save controller and Token and Pass it Data

$controller = new CobaltControllerForm();
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "save", 'STR' );
$controller->execute($task);

Now this works IF i disable line:

685: $form = $model->getForm($data, false);

And replace it with $form = true; and

699: $validData = $model->validate($form, $data); `` with $validData = $data;

in Joomla\libraries\legacy\controller\form.php

So it seems like there is something I am missing with the model... do I need to create one or what? I would like to leave all the core joomla code intact. Any Ideas?


Sergey
Total posts: 13,748
25 Nov 2014 12:31

I am little bit got lost.

Your API files are in backend of frontend? You create section or article?


Coderman5000
Total posts: 7
02 Dec 2014 01:58

I am accessing the cobalt files from a plugin.

I am creating a record for a particular Section

I am still having the same problem with line 699


Sergey
Total posts: 13,748
02 Dec 2014 03:32

Try to add this to form controller.

public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
{
    return JModelLegacy::getInstance('Form', 'CobaltModel', $config);
}

Coderman5000
Total posts: 7
04 Dec 2014 19:21

Ahhh Sergey,

Thank you so much for all your time and help I have it mostly working.

It came down to changing components\com_cobalt\models\form.php

to this:

$form = $this->loadForm('com_cobalt.record', JPATH_ROOT . '\components\com_cobalt\models\forms\record.xml', array('control' => 'jform', 'load_data' => $loadData));

Becuase the XML data wasn't being validated.

Powered by Cobalt