Exemplo n.º 1
0
 function Save()
 {
     $db = JFactory::getDBO();
     $this->item = array();
     $this->item['id'] = JRequest::getVar('id', 0);
     $user = JFactory::getUser();
     $userid = $user->get('id');
     $this->errors = array();
     $ok = true;
     foreach ($this->edit as $edit) {
         $field = $this->GetField($edit);
         $this->item[$field->field] = JRequest::getVar($field->input_name, '');
         if ($field->type == "text") {
             $this->item[$field->field] = JRequest::getVar($field->input_name, '', 'post', 'string', JREQUEST_ALLOWRAW);
         }
         if ($field->more) {
             if (strpos($this->item[$field->field], "system-readmore") > 0) {
                 $pos = strpos($this->item[$field->field], "system-readmore");
                 $top = substr($this->item[$field->field], 0, $pos);
                 $top = substr($top, 0, strrpos($top, "<"));
                 $bottom = substr($this->item[$field->field], $pos);
                 $bottom = substr($bottom, strpos($bottom, ">") + 1);
                 $this->item[$field->field] = $top;
                 $this->item[$field->more] = $bottom;
             } else {
                 $this->item[$field->more] = '';
             }
         }
         if ($field->required) {
             if ($this->item[$field->field] == "") {
                 $ok = false;
                 $this->errors[$field->field] = $field->required;
             }
         }
     }
     $now = FSF_Helper::CurDate();
     // if errors
     if ($ok) {
         if ($this->item['id']) {
             $qry = "UPDATE " . $this->table . " SET ";
             $sets = array();
             foreach ($this->edit as $edit) {
                 $field = $this->GetField($edit);
                 if ($field->type != "related" && $field->type != "tags") {
                     $sets[] = $field->field . " = '" . FSFJ3Helper::getEscaped($db, $this->item[$field->field]) . "'";
                 }
                 if ($field->more) {
                     $sets[] = "`" . $field->more . "` = '" . FSFJ3Helper::getEscaped($db, $this->item[$field->more]) . "'";
                 }
             }
             if ($this->permission['artperm'] > 2) {
                 $sets[] = "published = " . JRequest::getInt('published', 0);
                 $sets[] = "author = " . JRequest::getInt('author', 0);
             }
             if ($this->has_modified) {
                 $sets[] = "modified = '{$now}'";
             }
             $qry .= implode(", ", $sets);
             $qry .= " WHERE id = '" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "'";
         } else {
             $fieldlist = array();
             $fieldlist[] = "author";
             if ($this->has_added) {
                 $fieldlist[] = "added";
             }
             $setlist = array();
             foreach ($this->edit as $edit) {
                 $field = $this->GetField($edit);
                 if ($field->type == "related" || $field->type == "tags") {
                     continue;
                 }
                 $fieldlist[] = $field->field;
                 $setlist[] = "'" . FSFJ3Helper::getEscaped($db, $this->item[$field->field]) . "'";
                 if ($field->more) {
                     $fieldlist[] = "`" . $field->more . "`";
                     $setlist[] = "'" . FSFJ3Helper::getEscaped($db, $this->item[$field->more]) . "'";
                 }
             }
             if ($this->has_modified) {
                 $fieldlist[] = "modified";
                 $setlist[] = "'{$now}'";
                 $fieldlist[] = "created";
                 $setlist[] = "'{$now}'";
             }
             $fieldlist[] = "published";
             if ($this->permission['artperm'] > 2) {
                 $setlist[] = JRequest::getInt('published', 0);
             } else {
                 $setlist[] = "0";
             }
             if ($this->has_ordering) {
                 // need to get ordering value
                 $order = $this->GetOrderValue();
                 if ($order < 1) {
                     $order = 1;
                 }
                 $fieldlist[] = "ordering";
                 $setlist[] = $order;
             }
             $qry = "INSERT INTO " . $this->table . " (" . implode(", ", $fieldlist) . ") VALUES ('{$userid}', ";
             if ($this->has_added) {
                 $qry .= "'{$now}', ";
             }
             $qry .= implode(", ", $setlist) . ")";
         }
         $db->setQuery($qry);
         $db->query($qry);
         if (!$this->item['id']) {
             $this->item['id'] = $db->insertid();
         }
         $this->articleid = $this->item['id'];
         foreach ($this->edit as $edit) {
             $field = $this->GetField($edit);
             // save any products fields
             if ($field->type == "products") {
                 $this->GetProducts();
                 $qry = "DELETE FROM {$field->prod_table} WHERE {$field->prod_artid} = '" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "'";
                 $db->setQuery($qry);
                 //echo $qry."<br>";
                 $db->query($qry);
                 if (!$this->item[$field->field]) {
                     foreach ($this->products as &$product) {
                         $pid = $product->id;
                         $name = $field->field . "_prod_" . $pid;
                         $val = JRequest::getVar($name);
                         if ($val == "on") {
                             $qry = "INSERT INTO {$field->prod_table} ({$field->prod_prodid}, {$field->prod_artid}) VALUES\r\n\t\t\t\t\t\t\t\t\t({$pid}, '" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "')";
                             $db->setQuery($qry);
                             //echo $qry."<br>";
                             $db->query($qry);
                         }
                     }
                     //echo "Saving products<br>";
                 }
                 //echo "Prod Field";
             } elseif ($field->type == "related") {
                 // save related field
                 $relids = explode(":", $this->item[$field->field]);
                 $qry1 = "DELETE FROM {$field->rel_table} WHERE {$field->rel_id} = '" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "'";
                 $db->setQuery($qry1);
                 //echo $qry1."<br>";
                 $db->query();
                 foreach ($relids as $id) {
                     $id = FSFJ3Helper::getEscaped($db, $id);
                     $qry1 = "REPLACE INTO {$field->rel_table} ({$field->rel_id}, {$field->rel_relid}) VALUES ('" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "', '{$id}')";
                     $db->setQuery($qry1);
                     //echo $qry1."<br>";
                     $db->query();
                 }
             } else {
                 if ($field->type == "tags") {
                     //print_p($field);
                     //print_p($this->item);
                     $qry1 = "DELETE FROM {$field->tags_table} WHERE {$field->tags_key} = '" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "'";
                     //echo $qry1 . "<br>";
                     $db->setQuery($qry1);
                     $db->query();
                     $tags = explode("\n", $this->item[$field->field]);
                     foreach ($tags as $tag) {
                         $tag = trim($tag);
                         if (!$tag) {
                             continue;
                         }
                         $qry1 = "REPLACE INTO {$field->tags_table} ({$field->tags_key}, tag, language) VALUES (\t'" . FSFJ3Helper::getEscaped($db, $this->item['id']) . "', ";
                         $qry1 .= "'" . FSFJ3Helper::getEscaped($db, $tag) . "', '" . FSFJ3Helper::getEscaped($db, $this->item['language']) . "')";
                         //echo $qry1 . "<br>";
                         $db->setQuery($qry1);
                         $db->query();
                     }
                     //exit;
                 }
             }
         }
         // need to check for a redirect field here
         $mainframe = JFactory::getApplication();
         if ($this->what == "apply") {
             $link = FSFRoute::x("index.php?option=com_fsf&view=admin&layout=content&type={$this->id}&what=edit&id={$this->articleid}", false);
         } elseif ($this->what == "savenew") {
             $link = FSFRoute::x("index.php?option=com_fsf&view=admin&layout=content&type={$this->id}&what=new", false);
         } else {
             $link = FSFRoute::x('index.php?option=com_fsf&view=admin&layout=content&type=' . $this->id, false);
             $return = JRequest::getVar('return', '');
             if ($return && $return != 1) {
                 $link = $return;
             }
         }
         $mainframe->redirect($link, JText::_('ARTICLE_SAVED'));
         return;
     } else {
         // need to put onto the form the field stuff for related and products fields
         foreach ($this->edit as $edit) {
             $field = $this->GetField($edit);
             if ($field->type == "related") {
                 $field->rel_ids = array();
                 $relids = JRequest::getVar($field->field);
                 $relateds = explode(":", $relids);
                 foreach ($relateds as $related) {
                     if ($related == 0) {
                         continue;
                     }
                     $field->rel_ids[$related] = $related;
                 }
                 $field->rel_id_list = implode(":", $field->rel_ids);
                 if (count($field->rel_ids) > 0) {
                     $ids = array();
                     foreach ($field->rel_ids as $id) {
                         $ids[] = FSFJ3Helper::getEscaped($db, $id);
                     }
                     $qry = "SELECT {$field->rel_lookup_id}, {$field->rel_display} FROM {$field->rel_lookup_table} WHERE {$field->rel_lookup_id} IN (" . implode(", ", $ids) . ")";
                     ///					$qry = "SELECT {$field->rel_lookup_id}, {$field->rel_lookup_display} FROM {$field->rel_lookup_table} WHERE {$field->rel_lookup_id} IN (" . implode(", ", $field->rel_ids) . ")";
                     $db->setQuery($qry);
                     $relateds = $db->loadAssocList($field->rel_lookup_id);
                     foreach ($relateds as $id => &$related) {
                         $field->rel_ids[$id] = $related[$field->rel_lookup_display];
                     }
                 }
                 if (!$this->added_related_js) {
                     $this->AddRelatedJS();
                 }
             } else {
                 if ($field->type == "products") {
                     $this->GetProducts();
                     $field->products = array();
                     $prodcheck = "";
                     foreach ($this->products as $product) {
                         $prodform = JRequest::getVar($field->field . "_prod_" . $product->id);
                         if ($prodform == "on") {
                             $prodcheck .= "<input type='checkbox' name='{$field->field}_prod_" . $product->id . "' checked />" . $product->title . "<br>";
                         } else {
                             $prodcheck .= "<input type='checkbox' name='{$field->field}_prod_" . $product->id . "' />" . $product->title . "<br>";
                         }
                     }
                     $field->products_check = $prodcheck;
                     $field->products_yesno = JHTML::_('select.booleanlist', $field->field, array('class' => "inputbox", 'size' => "1", 'onclick' => "DoAllProdChange('{$field->field}');"), $this->item[$field->field]);
                 }
             }
         }
         if ($this->permission['artperm'] > 2) {
             $this->item['published'] = JRequest::getVar('published', 0);
             $this->item['author'] = JRequest::getVar('author', 0);
             $this->authorselect = $this->AuthorSelect($this->item);
         }
         $this->Output("form");
     }
     // if no errors, forward to list
     return;
 }