Submitting multiple fields to a joomla article

rknudsen 21 Dec, 2012
Hello,

I am in the process of trying to submit an article from my form containing multiple fields to be formatted and shown in the full text section of the article. The title field should also be used as the articles title. I've found the following article thats helped me start to understand the process, but I'm not sure what else needs to happen to get an article submitted.

http://www.chronoengine.com/forums.html?cont=posts&f=2&t=67956

The submit article action in chronoforms works, but it doesnt have the ability to use multiple fields for the full text field.

Heres the code that I've placed inside of a custom code action:

<?php
jimport( 'joomla.filesystem.file' );
$uri = JFactory::getURI();
$title = $form->data['title'];
$director = $form->data['director'];
$affiliation = $form->data['$affiliation'];
$nodeallocation = $form->data['$nodeallocation'];
$start = $form->data['start'];
$end = $form->data['end'];
$members = $form->data['members'];
$abstract = $form->data['abstract'];

$formated = "<p><strong>Director: </strong></p>".$director." <p>Affiliation: ".$desc."</p> </br> <p>Nodes to be allocated: ".$nodeallocation"</p> <p>Start Date: ".$start"</p> <p>End Date: ".$end"</p> </br> <p>List of involved members: ".$members"</p> </br> <p>Abstract: ".$abstract"</p>";

$form->data['fulltext'] = $formated;

?>


the order of my on submit actions is as follows:

check captcha
upload files
email
db save
custom article submit code (as written above)
show thanks
debugger

Thanks a bunch!
GreyHead 21 Dec, 2012
Hi rknudsen,

Great, thank you. Here's a slightly modified version that I will add to a FAQ later, the changes are only in style, not substance.
<?php
$d =& $form->data;
$form->data['fulltext'] = "<p><strong>Director: </strong>{$d['director']}</p>
<p>Affiliation: {$d['desc']}</p>
</br>
<p>Nodes to be allocated: {$d['nodeallocation']}</p>
<p>Start Date: {$d['start']}</p>
<p>End Date: {$d['end']}</p>
</br>
<p>List of involved members: {$d['members']}</p>
</br>
<p>Abstract: {$d['abstract']}</p>";
?>

Bob
rknudsen 21 Dec, 2012
Oh man this helps thanks a lot. I do have some more questions about it though.

It appears that it never submits an actual article to joomla. I was looking through the code in the submit article event action, but wasnt seeing how it was actually submitting the article and how I could adapt that to the custom code action.

I removed the debugger thinking maybe it was intervening in the article submission but no dice.

The other thing I would like to be able to do is default the articles category and set the article as published. Is this possible using the custom code?

Thanks bob!
rknudsen 22 Dec, 2012
I went back to the other thread that I linked to and read through it again.

Am I correct in making the assumption that I need to use a db save action to pipe the data into the joomla sql database?

If so How do I specify the articles category and published status?

Thanks!
GreyHead 22 Dec, 2012
Hi rknudsen,

You can use the Submit Article action - but that writes to 'introtext' not to 'fulltext'. It works OK but has some limited options that may not do exactly what you want.

You can have more control using the DB Save action because you can set any of the variables. If you are OK with PHP & MySQL then you can hand-code it all in a Custom Code action. Here's one that I wrote a few days ago.
$user =& JFactory::getUser();
// save article
$db =& JFactory::getDBO();
$query = "
  SELECT `id`
    FROM `#__content`
    WHERE `title` = '{$user->username}' ;
";
$db->setQuery($query);
$art_id = $db->loadResult();
if ( $art_id > 0 ) {
  $query = "
      UPDATE `#__content`
        SET `introtext` = {$db->quote($introtext)},
        `modified` = '".date("Y-m-d H:i:s")."',
        `modified_by` = '{$user->id}'
      WHERE `id` = '{$art_id}' ;
  ";
} else {
  $query = "
      INSERT INTO `#__content`
      SET `title` = '{$user->username}',
      `alias` = '".JFilterOutput::stringURLSafe($user->username)."',
      `introtext` = {$db->quote($introtext)},
      `state` = '1',
      `catid` = '1',
      `created` = '".date("Y-m-d H:i:s")."',
      `created_by` = '{$user->id}';
  ";
}
$db->setQuery($query);
$db->query();

This both updates and inserts - the INSERT INTO part is probably what you need. (Here the title is being set to the current username for particular purposes of this site.)

Bob
rknudsen 23 Dec, 2012
Thanks Bob,

I'm really new to php and so far been able to get the submission to stop crashing on submit.

Here is the code I've piece milled so far from your suggestions:
<?php
$d =& $form->data;
$form->data['fulltext'] = "
<p><strong>Project: </strong>{$d['title']}</p>
<p><strong>Director: </strong>{$d['director']}</p>
<p>Affiliation: {$d['affiliation']}</p>
</br>
<p>Nodes to be allocated: {$d['nodeallocation']}</p>
<p>Start Date: {$d['start']}</p>
<p>End Date: {$d['end']}</p>
</br>
<p>List of involved members: {$d['members']}</p>
</br>
<p>Abstract: {$d['abstract']}</p>";
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
  SELECT `id`
    FROM `#tableprefix_content`
    WHERE `title` = '{$title}' ;
";
$db->setQuery($query);
$query = "
      INSERT INTO `#tableprefix_content`
      SET `title` = '{$title}',
      `alias` = '".JFilterOutput::stringURLSafe($title)."',
      `fulltext` = {$db->quote(fulltext)},
      `state` = '1',
      `catid` = '1',
      `created` = '".date("Y-m-d H:i:s")."',
  ";
$db->setQuery($query);
$db->query();
?>


I am still not getting any articles submitted to joomla, but feel like its getting closer.
GreyHead 23 Dec, 2012
Hi rknudsen,

From a quick look this line looks wrong
INSERT INTO `#tableprefix_content`
It should be, as in the original:
INSERT INTO `#__content`
Joomla! will replace #_ with the prefix for the site.

Bob
rknudsen 27 Dec, 2012
Evening Bob!

So I've worked out the submission and am getting articles submitted with the correct data.

I do have one more question about setting the articles access value. the one that sets between public, registered, and special.

Do you happen to know what the value of this column can be set to? I'm looking in the content table to see if I can gleen what the value is set to for other articles, but its a bit indecipherable.

Thanks!
GreyHead 28 Dec, 2012
Hi rknudsen,

I've done a little digging and I think it works like this. Each article has an 'access level' defined and saved in the 'access' column in the #__content table.

These access levels are set up on the Site Admin | Users | Access Levels tab and each level can map across to one or more User groups.

The default access levels are:
1 => Public (anyone can view)
2 => Registered (user must be logged in to view)
3 => Special (user can view if they are a Manager, Author or Super User)

There's also a level 4 set up as an example of a custom level.

The default setting for Joomla! articles appears to be 1 - i.e. 'anyone can view this article'.

Bob
This topic is locked and no more replies can be posted.