예제 #1
0
 /**
  * Method to save a record.
  *
  * @param	string	$key	The name of the primary key of the URL variable.
  * @param	string	$urlVar	The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
  *
  * @return	Boolean	True if successful, false otherwise.
  * @since	1.6
  */
 public function save($key = null, $urlVar = 'a_id')
 {
     // Load the backend helper for filtering.
     require_once JPATH_ADMINISTRATOR . '/components/com_content/helpers/content.php';
     //var_dump($_POST['jform']);die;
     $app = JFactory::getApplication();
     $error = false;
     $userId = JFactory::getUser()->get('id');
     $isFirstIdeari = false;
     $jform['title'] = $_POST['jform']['title'];
     $jform['new'] = $_POST['jform']['new'];
     $jform['articletext'] = $_POST['jform']['articletext'];
     $jform['catid'] = 33;
     $app->setUserState('jformulario', $jform);
     if ($error) {
         session_start();
         $_SESSION['flash']['msg'] = "Hubo un error al guardar tu Ideary ):";
         $_SESSION['flash']['class'] = "error";
         $idQueryString = $_POST['jform']['id'] != -1 ? '&a_id=' . $_POST['jform']['id'] : '';
         $this->setRedirect(JRoute::_('index.php?option=com_content&view=form&layout=edit' . $idQueryString, false));
     } else {
         $idearyCount = ideary::getCountTextsByUserId($userId);
         $hasFirst = ideary::getHasFirstByUserId($userId);
         $isFirstIdeari = !$hasFirst && $idearyCount < 1;
         $result = parent::save($key, $urlVar);
         $db =& JFactory::getDBO();
         $model = $this->getModel();
         if ($_POST['jform']['id'] == -1) {
             $textId = $model->getLastInsertedTextId();
         } else {
             $textId = $_POST['jform']['id'];
             $isFirstIdeari = null;
         }
         $save_author_write_not = false;
         $actual_date = '';
         if ($_POST['jform']['id'] == -1) {
             //...si el texto es nuevo.
             if ($_POST['jform']['state']) {
                 //...si lo estamos publicando.
                 $save_author_write_not = true;
             }
             $actual_date = 'created=NOW(), ';
         } else {
             $sql = 'SELECT COUNT(*) FROM text_notifications n ' . 'JOIN text_notification_types nt ON (nt.id=n.notification_type_id) ' . 'WHERE text_id=' . $textId . ' AND nt.type="followed_write"';
             $db->setQuery($sql);
             $save_author_write_not = $db->loadResult() ? false : true;
         }
         // text was published, notify followers
         if ($save_author_write_not && $_POST['jform']['state']) {
             $sql = "SELECT DISTINCT follower_id FROM text_follows WHERE followed_id=" . $userId;
             $db->setQuery($sql);
             $authorFollowers = $db->loadObjectList();
             foreach ($authorFollowers as $authorFollower) {
                 $query = 'INSERT INTO text_notifications (user_id, notified_id, text_id, notification_type_id, created_at, saw) ' . 'VALUES ' . '(' . $userId . ', ' . $authorFollower->follower_id . ', ' . $textId . ', (SELECT id FROM text_notification_types WHERE TYPE="followed_write"), NOW(), FALSE)';
                 $db->setQuery($query);
                 $db->query();
             }
         }
         if (!$_POST['jform']['state']) {
             // text was unpublished, delete all notifications of it's publication
             $query = "\n            \t \tDELETE FROM text_notifications \n            \t \tWHERE text_id={$textId}\n\t\t\t\t\tAND notification_type_id in (\n\t\t\t\t\t\tselect id from text_notification_types\n\t\t\t\t\t\twhere type='followed_write'\n\t\t\t\t\t)";
             $db->setQuery($query);
             $db->query();
         }
         // Strip slashes added by magic quotes by dummy servers
         if (get_magic_quotes_gpc()) {
             $_POST['jform']['articletext'] = stripslashes($_POST['jform']['articletext']);
         }
         $query = 'UPDATE #__content SET ' . 'introtext="' . mysql_real_escape_string($_POST['jform']['articletext']) . '", ' . 'allow_comments=' . $_POST['jform']['comments'] . ', ' . 'state=' . $_POST['jform']['state'] . ', ' . 'catid=33, ' . 'modified_by=created_by, ' . 'checked_out=created_by, ' . $actual_date . 'editor_font_scale=' . $_POST['jform']['editor-font-scale'] . ', ' . 'editor_font_size="' . $_POST['jform']['editor-font-size'] . '" ' . ($isFirstIdeari === null ? '' : ', first=' . ($isFirstIdeari ? 1 : 0)) . ' ' . 'WHERE id=' . $textId;
         $db->setQuery($query);
         $db->query();
         $app->setUserState('jformulario', null);
         ideary::clearCache();
         switch ($_POST["accionpost"]) {
             case 'home':
                 $this->setRedirect(JRoute::_('index.php?option=com_users&view=login'));
                 break;
             case 'mytexts':
                 $this->setRedirect(JRoute::_('index.php?option=com_users&mytexts=1&view=profile&user_id=' . $userId));
                 break;
             case 'draft':
                 $this->setRedirect(JRoute::_('index.php?option=com_users&draft=1&view=profile&user_id=' . $userId));
                 break;
             case 'same':
                 session_start();
                 $_SESSION['flash']['msg'] = "Tu Ideary fue guardado exitosamente!";
                 $_SESSION['flash']['class'] = "success";
                 $this->setRedirect(JRoute::_("index.php?option=com_content&Itemid=101&a_id=" . $textId . "&task=article.edit"));
                 break;
             case 'view':
                 session_start();
                 $this->setRedirect(JRoute::_("index.php?option=com_content&view=article&id=" . $textId));
                 break;
         }
         return $result;
     }
 }