コード例 #1
0
ファイル: items.php プロジェクト: jomsocial/JSVoice
    /**
     * Save item record
     */
    function save()
    {
        global $javconfig;
        // Check for request forgeries
        //JRequest::checkToken() or jexit( 'Invalid Token' );
        $Itemid = JRequest::getInt('Itemid');
        if (!$Itemid) {
            $Itemid = JAVoiceHelpers::get_Itemid(array('option' => 'com_javoice', 'view' => 'items'));
        }
        $cid = JRequest::getVar('cid', array(), '', 'array');
        $model = $this->getModel('items');
        $user = JFactory::getUser();
        if ($cid) {
            if (!JAVoiceHelpers::checkPermissionAdmin()) {
                $isAllowRegisterEdit = 0;
                if ($javconfig["systems"]->get("is_edit_delete_voice", 0)) {
                    $item = $model->getItem();
                    $userEId = $user->get('id');
                    if ($userEId == $item->user_id) {
                        $timeE = $javconfig["systems"]->get("time_for_edit_voice", 900);
                        if ($timeE != -1 || time() < $item->create_date + $timeE) {
                            $isAllowRegisterEdit = 1;
                        }
                    }
                }
                if (!$isAllowRegisterEdit) {
                    return;
                }
            }
        }
        JArrayHelper::toInteger($cid);
        $helper = new JAVoiceHelpers();
        $post = JRequest::get('request');
        if ($post['title']) {
            $post['title'] = trim($post['title']);
        }
        $lang = JFactory::getLanguage();
        $langName = $lang->getName();
        //		if(strpos("English", $langName) !== false){
        //			$post["title"] = $helper->addSpaceInLongTitle($post["title"]);
        //		}
        // allow name only to contain html
        if (JRequest::getVar("javNameOfTextarea", "newVoiceContent") == "newVoiceContentReply") {
            $post['content'] = JRequest::getString('newVoiceContentReply', '');
        } else {
            $post['content'] = JRequest::getString('newVoiceContent', '');
        }
        $post['content'] = $helper->removeEmptyBBCode($post['content']);
        $post['voice_types_id'] = JRequest::getInt('type');
        $post['forums_id'] = JRequest::getInt('forums_id');
        $model->setState('request', $post);
        $objects = array();
        if (!$cid) {
            $this->checkDataPhp($objects);
        }
        $row = $model->store();
        if (isset($row->id)) {
            if ($javconfig['plugin']->get("is_attach_image", 0)) {
                //delete file in store image if remove file
                jimport('joomla.filesystem.folder');
                jimport('joomla.filesystem.file');
                $listFile = JRequest::getVar('listfile', 0);
                $file_path = JPATH_ROOT . DS . "images" . DS . "stories" . DS . "ja_voice" . DS . $row->id;
                $listFileOfComments = JFolder::files($file_path);
                if ($listFileOfComments) {
                    foreach ($listFileOfComments as $listFileOfComment) {
                        if ($listFile) {
                            if (!in_array($listFileOfComment, $listFile)) {
                                JFile::delete($file_path . DS . $listFileOfComment);
                            }
                        } else {
                            JFile::delete($file_path . DS . $listFileOfComment);
                        }
                    }
                }
                if ($listFile) {
                    if (isset($_SESSION['javtemp'])) {
                        $listFileTemp = JFolder::files($_SESSION['javtemp']);
                        if ($listFileTemp) {
                            foreach ($listFileTemp as $file) {
                                if (!in_array($file, $listFile, true)) {
                                    JFile::delete($_SESSION['javtemp'] . DS . $file);
                                }
                            }
                        }
                        JRequest::setVar("listfile", implode(',', $listFile));
                        //move file
                        $target_path = JPATH_ROOT . DS . "images" . DS . "stories" . DS . "ja_voice" . DS . $row->id;
                        if (!is_dir($target_path)) {
                            JFolder::create($target_path);
                        }
                        if ($listFileTemp) {
                            JFolder::copy($_SESSION['javtemp'], $target_path, '', true);
                        }
                        JFolder::delete($_SESSION['javtemp']);
                        unset($_SESSION['javtemp']);
                        unset($_SESSION['javnameFolder']);
                    }
                }
            }
            if ($javconfig["systems"]->get("is_enable_tagging", 0)) {
                //Insert tag.
                $javtags = JRequest::getVar("javtag", "");
                $modelTags = $this->getModel('tags');
                $modelTags->addVoiceToTag($row->id, $javtags);
            }
            if (intval($javconfig['systems']->get("is_notify_admin", 0))) {
                // send mail to administrator or author
                $helper->sendMailWhenNewVoice($row, $Itemid);
            }
            if (!$cid) {
                $this->vote('', $post['votes']);
                $model->update_total_items($row->voice_types_id, $row->forums_id);
                $model->vote($row->id, $post['votes']);
                if ($row->voice_type_status_id) {
                    $status_is_spam = $this->getModel('voicetypesstatus')->is_spam($row->voice_type_status_id);
                }
                if ($row->published && (!$row->voice_type_status_id || !$status_is_spam)) {
                    $object = new stdClass();
                    $object->id = '';
                    $object->attr = 'reload';
                    if (trim($javconfig['systems']->get("page_redirect", 'item')) == 'item') {
                        $object->content = JRoute::_('index.php?option=com_javoice&view=items&layout=item&cid=' . $row->id . '&type=' . $row->voice_types_id . '&forums=' . $row->forums_id . '&Itemid=' . $Itemid . '&' . md5('save_successfull'));
                    } else {
                        $object->content = JURI::current();
                    }
                    $object->content = str_replace('&amp;', '&', $object->content);
                    $objects[] = $object;
                    $helper = new JAVoiceHelpers();
                    echo $helper->parse_JSON_new($objects);
                    exit;
                }
                if (isset($post['create_full']) && $post['create_full'] == 1) {
                    $url = str_replace('&amp;', '&', JRoute::_('index.php?option=com_javoice&type=' . $post['type'] . '&' . md5('save_successfull')));
                    return $this->setRedirect($url);
                }
                JRequest::setVar('layout', 'after_save');
            } else {
                $object = new stdClass();
                $object->id = '#jav-box-item-' . $row->id . ' .jav-item-title';
                $object->attr = 'html';
                $object->content = $row->title;
                $objects[] = $object;
                $row->content = str_replace("\n", '<br/>', $row->content);
                $object = new stdClass();
                $object->id = '#jav-box-item-' . $row->id . ' .jav-item-content';
                $object->attr = 'html';
                $object->content = html_entity_decode($helper->showItem($row->content));
                $objects[] = $object;
                $dir = JPATH_ROOT . DS . "images" . DS . "stories" . DS . "ja_voice" . DS . $row->id;
                $files = $model->formatFilesInDir($dir, 'download', $user->id, $row->id);
                $content = $files;
                $object = new stdClass();
                $object->id = '#jav_file_upload_' . $row->id;
                $object->attr = 'html';
                $object->content = $content;
                $objects[] = $object;
                $object = new stdClass();
                $object->id = '#jav-box-item-' . $row->id . ' .created-at';
                $object->attr = 'html';
                $object->content = JText::_('UPDATED_ON') . ' ' . JAVoiceHelpers::generatTimeStamp($row->update_date);
                $objects[] = $object;
                if ($javconfig["systems"]->get("is_enable_tagging", 0)) {
                    $object = new stdClass();
                    $object->id = '#jav_item_tags';
                    $object->attr = 'html';
                    $object->content = "";
                    $tagList = $modelTags->getTagByVoice($row->id);
                    $spaterTags = $javconfig["systems"]->get("characters_separating_tags", ",");
                    $tagContent = "<b>" . JText::_("TAG_LIST") . ':</b>';
                    foreach ($tagList as $tagItem => $tag) {
                        $tagContent .= ' <a title="' . $tag->name . '" href="' . JRoute::_('index.php?option=com_javoice&amp;view=items&amp;layout=item&amp;tagid=' . $tag->id . '&amp;Itemid=' . $Itemid) . '">' . $tag->name . '</a>';
                        if ($tagItem <= count($tagList) - 1) {
                            $tagContent .= $spaterTags;
                        }
                    }
                    $objects[] = $object;
                    $object->content = $tagContent;
                }
                if ($javconfig["systems"]->get("use_anonymous", 0)) {
                    $object = new stdClass();
                    $object->id = '#jav-box-item-' . $row->id . ' .jav-createdby';
                    $object->attr = 'html';
                    if ($row->use_anonymous) {
                        $object->content = '<small>' . JText::_('BY') . '</small> ' . JText::_("ANONYMOUS");
                    } else {
                        if (isset($row->user_id) && $row->user_id > 0) {
                            $user = JFactory::getUser($row->user_id);
                            if ($javconfig['plugin']->get('displayname', 'username') == "name") {
                                $creat_by = $user->name;
                            } else {
                                if ($javconfig['plugin']->get('displayname', 'username') == "username") {
                                    $creat_by = $user->username;
                                } else {
                                    $creat_by = JText::_("ANONYMOUS");
                                }
                            }
                        } elseif (isset($row->guest_name) && $row->guest_name != '') {
                            if ($javconfig['plugin']->get('displayname', 'username') == "anonymous") {
                                $creat_by = JText::_("ANONYMOUS");
                            } else {
                                $creat_by = $row->guest_name;
                            }
                        } else {
                            $creat_by = JText::_('ANONYMOUS');
                        }
                        $object->content = '<small>' . JText::_('BY') . '</small>
											<a href="' . JRoute::_('index.php?option=com_javoice&view=users&uid=' . $row->user_id . '&amp;Itemid=' . $Itemid) . '" class="user">' . $creat_by . '</a>';
                    }
                    $objects[] = $object;
                }
                $helper = new JAVoiceHelpers();
                echo $helper->parse_JSON_new($objects);
                exit;
            }
        } else {
            $object = new stdClass();
            $object->id = '#jav-popup-error-posting';
            $object->attr = 'html';
            $object->content = JText::_('ERROR_FOUND_FAIL_TO_SAVE_IDEA') . '<br/>' . $row;
            $objects[] = $object;
            $object = new stdClass();
            $object->id = '#jav-popup-error-posting';
            $object->attr = 'css';
            $object->content = 'display,block';
            $objects[] = $object;
            $helper = new JAVoiceHelpers();
            echo $helper->parse_JSON_new($objects);
            exit;
        }
        return parent::display();
    }