//+trace dis

// configuration variables
$moderate = 1;  // 0 = publish directly, 1 = leave unpublished

// declare globals used in this piece
global $Itemid;

// load the standard piece library
$this->execPieceByName('ff_InitLib');

// get values from submission
$name     = ff_getSubmit('name',     '');
$title    = ff_getSubmit('title',    '');
$text     = ff_getSubmit('text',     '');
$section  = ff_getSubmit('section',  0);
$category = ff_getSubmit('category', 0);

// get the userid of a superadministrator. the article will be created
// by the superadmin, but use as author alias the name of the person
// who submitted it.
$creator = ff_select("select id, email, sendEmail from #__users where gid=25");
$creator = $creator[0]; // take first row

// format text, date and state
$text = ff_markdown($text);
$date = date( "Y-m-d H:i:s" );
if ($moderate) $state = 0; else $state = 1;

// save new submission into #__content table
ff_query(
    "insert into #__content ".
        "(title, title_alias, introtext, state, sectionid, ".
         "catid, created, created_by, created_by_alias) ".
    "values ".
        "('$title', '$title', '$text', '$state', '$section', ".
         "'$category', '$date', '$creator->id', '$name')"
);

// get section and cat name
$section_name = ff_selectValue("select title from #__sections where id='$section'");
$cat_name = ff_selectValue("select title from #__categories where id='$category'");

// update #__messages table for backend notices
$subject = "New content submitted";
$message = "A new product was submitted by [$name] ".
           "titled [$title] in section [$section_name] and category [$cat_name]";
ff_query(
    "insert into #__messages (user_id_from, user_id_to, date_time, subject,message) ".
    "values ('".JFactory::getUser()->get('id',0)."', '$creator->id', '$date', '$subject','$message')"
);

// send a mail notice to superadmin
if ($creator->sendEmail) {
    // if sent by unregistred we dont know the email
    if (JFactory::getUser()->get('id',0) != 0) $from = JFactory::getUser()->get('email',''); else $from = "somebody@unknown.com";
    $this->sendMail(
        $from,              // senders email
        $name,              // senders name
        $creator->email,    // recipient email
        $subject,           // subject line
        $message            // the message
    );
} // if

ff_redirectSelf("&ff_page=3"); // show thank you page