Example #1
0
    /**
     * This is the Ajax function that is called with the results of the
     * form supplied by news_<user/admin>_new() to create a new draft item
     * The following parameters are received in an array 'story'!
     *
     * @param string 'title' the title of the news item
     *
     * @author Erik Spaan
     * @return array(output, etc) with output being a rendered template or a simple text and action the performed action
     */
    public function savedraft()
    {
        $this->checkAjaxToken();
        $story = $this->request->getPost()->get('story', null);

        // cannot process file inputs (pictures) via ajax
        // File inputs are skipped as they cannot be serialized and sent using only JavaScript.
        // @see http://api.prototypejs.org/dom/Form/serialize/
        // @see http://www.openjs.com/articles/ajax/ajax_file_upload/

        $output = '';
        $slug = '';
        $fullpermalink = '';
        $showslugedit = false;
        
        // Validate the input
        $validationerror = News_Util::validateArticle($story);
        // check hooked modules for validation
        $sid = isset($story['sid']) ? $story['sid'] : null;
        $hookvalidators = $this->notifyHooks('news.ui_hooks.articles.validate_edit', $story, $sid, array(), new Zikula_Hook_ValidationProviders())->getData();
        if ($hookvalidators->hasErrors()) {
            $validationerror .= $this->__('Error! Hooked content does not validate.') . "\n";
        }
        if ($validationerror) {
            return new Zikula_Response_Ajax_BadData($validationerror);
        }

        $newitem = array(
            'sid' => isset($story['sid']) ? $story['sid'] : null,
            'title' => $story['title'],
            'urltitle' => isset($story['urltitle']) ? $story['urltitle'] : '',
            '__CATEGORIES__' => isset($story['__CATEGORIES__']) ? $story['__CATEGORIES__'] : null,
            '__ATTRIBUTES__' => isset($story['attributes']) ? News_Util::reformatAttributes($story['attributes']) : null,
            'language' => isset($story['language']) ? $story['language'] : '',
            'hometext' => isset($story['hometext']) ? $story['hometext'] : '',
            'hometextcontenttype' => $story['hometextcontenttype'],
            'bodytext' => isset($story['bodytext']) ? $story['bodytext'] : '',
            'bodytextcontenttype' => $story['bodytextcontenttype'],
            'notes' => isset($story['notes']) ? $story['notes'] : '',
            'displayonindex' => isset($story['displayonindex']) ? $story['displayonindex'] : 0,
            'allowcomments' => isset($story['allowcomments']) ? $story['allowcomments'] : 0,
            'from' => isset($story['from']) ? $story['from'] : null,
            'tonolimit' => isset($story['tonolimit']) ? $story['tonolimit'] : null,
            'to' => isset($story['to']) ? $story['to'] : null,
            'unlimited' => isset($story['unlimited']) && $story['unlimited'] ? true : false,
            'weight' => isset($story['weight']) ? $story['weight'] : 0,
            'tempfiles' => isset($story['tempfiles']) ? $story['tempfiles'] : null,
            'pictures' => isset($story['pictures']) ? $story['pictures'] : 0,
            'del_pictures' => isset($story['del_pictures']) ? $story['del_pictures'] : null,
            'published_status' => isset($story['published_status']) ? $story['published_status'] : News_Api_User::STATUS_DRAFT,
            );

        // get all module vars
        $modvars = $this->getVars();

        // Check  if the article is already saved as draft
        if (isset($story['sid']) && $story['sid'] > 0) {
            // Get the current news article
            $item = ModUtil::apiFunc('News', 'User', 'get', array('sid' => $story['sid']));
            if ($item == false) {
                throw new Zikula_Exception_NotFound($this->__('Error! No such article found.'));
            }
            // Security check
            $this->throwForbiddenUnless(SecurityUtil::checkPermission('News::', "{$item['cr_uid']}::{$story['sid']}", ACCESS_EDIT));

            if (!ModUtil::apiFunc('News', 'admin', 'update', $newitem)) {
                $output = DataUtil::formatForDisplayHTML($this->__('Error! Could not save your changes.'));
            } else {
                $output = $this->__f('Draft updated at %s', DateUtil::getDatetime_Time('', '%H:%M'));
                // Return the permalink (domain shortened) and the slug of the permalink
                $slug = $item['urltitle'];
                $fullpermalink = DataUtil::formatForDisplayHTML(ModUtil::url('News', 'user', 'display', array('sid' => $story['sid'])));
                // Only show "edit the slug" if the shorturls are active
                $showslugedit = (System::getVar('shorturls', false));
            }
            $sid = $story['sid'];
        } else {
            // Create a first draft version of the story
            $sid = ModUtil::apiFunc('News', 'User', 'create', $newitem);
            if (!empty($sid)) {
                // Success and now reload the news story
                $item = ModUtil::apiFunc('News', 'User', 'get', array('sid' => $sid));
                if ($item == false) {
                    throw new Zikula_Exception_NotFound($this->__('Error! No such article found.'));
                } else {
                    // Return the Draft creation date
                    $output = $this->__f('Draft saved at %s', DateUtil::getDatetime_Time($item['cr_date'], '%H:%M'));
                    // Return the permalink (domain shortened) and the slug of the permalink
                    $slug = $item['urltitle'];
                    $fullpermalink = DataUtil::formatForDisplayHTML(ModUtil::url('News', 'user', 'display', array('sid' => $sid)));
                    // Only show "edit the slug" if the shorturls are active
                    $showslugedit = (System::getVar('shorturls', false));
                }
            } else {
                $output = DataUtil::formatForDisplayHTML($this->__('Error! Could not save your changes.'));
            }
        }

        //lock the page so others cannot edit it
        if (ModUtil::available('PageLock')) {
            $returnUrl = ModUtil::url('News', 'admin', 'view');
            ModUtil::apiFunc('PageLock', 'user', 'pageLock', array(
                'lockName' => "Newsnews{$sid}",
                'returnUrl' => $returnUrl));
        }

        return new Zikula_Response_Ajax(array(
            'result' => $output,
            'sid' => $sid,
            'slug' => $slug,
            'fullpermalink' => $fullpermalink,
            'showslugedit' => $showslugedit));
    }
Example #2
0
    /**
     * This is a standard function that is called with the results of the
     * form supplied by News_admin_newitem() or News_user_newitem to create
     * a new item.
     *
     * @author Mark West
     * @param string 'title' the title of the news item
     * @param string 'language' the language of the news item
     * @param string 'hometext' the summary text of the news item
     * @param int 'hometextcontenttype' the content type of the summary text
     * @param string 'bodytext' the body text of the news item
     * @param int 'bodytextcontenttype' the content type of the body text
     * @param string 'notes' any administrator notes
     * @param int 'published_status' the published status of the item
     * @param int 'displayonindex' display the article on the index page
     * @return bool true
     */
    public function create($args)
    {
        // Get parameters from whatever input we need
        $story = FormUtil::getPassedValue('story', isset($args['story']) ? $args['story'] : null, 'POST');
        $files = News_ImageUtil::reArrayFiles(FormUtil::getPassedValue('news_files', null, 'FILES'));

        // Create the item array for processing
        $item = array(
            'title' => $story['title'],
            'urltitle' => isset($story['urltitle']) ? $story['urltitle'] : '',
            '__CATEGORIES__' => isset($story['__CATEGORIES__']) ? $story['__CATEGORIES__'] : null,
            '__ATTRIBUTES__' => isset($story['attributes']) ? News_Util::reformatAttributes($story['attributes']) : null,
            'language' => isset($story['language']) ? $story['language'] : '',
            'hometext' => isset($story['hometext']) ? $story['hometext'] : '',
            'hometextcontenttype' => $story['hometextcontenttype'],
            'bodytext' => isset($story['bodytext']) ? $story['bodytext'] : '',
            'bodytextcontenttype' => $story['bodytextcontenttype'],
            'notes' => $story['notes'],
            'displayonindex' => isset($story['displayonindex']) ? $story['displayonindex'] : 0,
            'allowcomments' => isset($story['allowcomments']) ? $story['allowcomments'] : 0,
            'from' => isset($story['from']) ? $story['from'] : null,
            'tonolimit' => isset($story['tonolimit']) ? $story['tonolimit'] : null,
            'to' => isset($story['to']) ? $story['to'] : null,
            'unlimited' => isset($story['unlimited']) && $story['unlimited'] ? true : false,
            'weight' => isset($story['weight']) ? $story['weight'] : 0,
            'action' => isset($story['action']) ? $story['action'] : self::ACTION_PREVIEW,
            'sid' => isset($story['sid']) ? $story['sid'] : null,
            'tempfiles' => isset($story['tempfiles']) ? $story['tempfiles'] : null,
            'del_pictures' => isset($story['del_pictures']) ? $story['del_pictures'] : null,
        );

        // convert user times to server times (TZ compensation) refs #181
        //  can't do the below because values are YYYY-MM-DD HH:MM:SS and DateUtil value is in seconds.
        // $item['from'] = $item['from'] + DateUtil::getTimezoneUserDiff();
        // $item['to'] = $item['to'] + DateUtil::getTimezoneUserDiff();
        // Disable the non accessible fields for non editors
        if (!SecurityUtil::checkPermission('News::', '::', ACCESS_ADD)) {
            $item['notes'] = '';
            $item['displayonindex'] = 1;
            $item['allowcomments'] = 1;
            $item['from'] = null;
            $item['tonolimit'] = true;
            $item['to'] = null;
            $item['unlimited'] = true;
            $item['weight'] = 0;
            if ($item['action'] > self::ACTION_SUBMIT) {
                $item['action'] = self::ACTION_PREVIEW;
            }
        }

        // Validate the input
        $validationerror = News_Util::validateArticle($item);
        // check hooked modules for validation
        $sid = isset($item['sid']) ? $item['sid'] : null;
        $hookvalidators = $this->notifyHooks(new Zikula_ValidationHook('news.ui_hooks.articles.validate_edit', new Zikula_Hook_ValidationProviders()))->getValidators();
        if ($hookvalidators->hasErrors()) {
            $validationerror .= $this->__('Error! Hooked content does not validate.') . "\n";
        }

        // get all module vars
        $modvars = $this->getVars();

        if (isset($files) && $modvars['picupload_enabled']) {
            list($files, $item) = News_ImageUtil::validateImages($files, $item);
        } else {
            $item['pictures'] = 0;
        }

        // story was previewed with uploaded pics
        if (isset($item['tempfiles'])) {
            $tempfiles = unserialize($item['tempfiles']);
            // delete files if requested
            if (isset($item['del_pictures'])) {
                foreach ($tempfiles as $key => $file) {
                    if (in_array($file['name'], $item['del_pictures'])) {
                        unset($tempfiles[$key]);
                        News_ImageUtil::removePreviewImages(array($file));
                    }
                }
            }
            $files = array_merge($files, $tempfiles);
            $item['pictures'] += count($tempfiles);
        }

        // if the user has selected to preview the article we then route them back
        // to the new function with the arguments passed here
        if ($item['action'] == self::ACTION_PREVIEW || $validationerror !== false) {
            // log the error found if any
            if ($validationerror !== false) {
                LogUtil::registerError(nl2br($validationerror));
            }
            if ($item['pictures'] > 0) {
                $tempfiles = News_ImageUtil::tempStore($files);
                $item['tempfiles'] = serialize($tempfiles);
            }
            // back to the referer form
            SessionUtil::setVar('newsitem', $item);
            $this->redirect(ModUtil::url('News', 'user', 'newitem'));
        } else {
            // As we're not previewing the item let's remove it from the session
            SessionUtil::delVar('newsitem');
        }

        // Confirm authorization code.
        $this->checkCsrfToken();

        if (!isset($item['sid']) || empty($item['sid'])) {
            // Create the news story
            $sid = ModUtil::apiFunc('News', 'user', 'create', $item);
            if ($sid != false) {
                // Success
                LogUtil::registerStatus($this->__('Done! Created new article.'));
                // Let any hooks know that we have created a new item
                $this->notifyHooks(new Zikula_ProcessHook('news.ui_hooks.articles.process_edit', $sid, new Zikula_ModUrl('News', 'User', 'display', ZLanguage::getLanguageCode(), array('sid' => $sid))));
                $this->notify($item); // send notification email
            } else {
                // fail! story not created
                throw new Zikula_Exception_Fatal($this->__('Story not created for unknown reason (Api failure).'));
                return false;
            }
        } else {
            // update the draft
            $result = ModUtil::apiFunc('News', 'admin', 'update', $item);
            if ($result) {
                LogUtil::registerStatus($this->__('Story Updated.'));
            } else {
                // fail! story not updated
                throw new Zikula_Exception_Fatal($this->__('Story not updated for unknown reason (Api failure).'));
                return false;
            }
        }

        // clear respective cache
        ModUtil::apiFunc('News', 'user', 'clearItemCache', $item);

        if (isset($files) && $modvars['picupload_enabled']) {
            $resized = News_ImageUtil::resizeImages($sid, $files); // resize and move the uploaded pics
            if (isset($item['tempfiles'])) {
                News_ImageUtil::removePreviewImages($tempfiles); // remove any preview images
            }
            LogUtil::registerStatus($this->_fn('%1$s out of %2$s picture was uploaded and resized.', '%1$s out of %2$s pictures were uploaded and resized.', $item['pictures'], array($resized, $item['pictures'])));
            if (($item['action'] >= self::ACTION_SAVEDRAFT) && ($resized <> $item['pictures'])) {
                LogUtil::registerStatus($this->_fn('Article now has draft status, since the picture was not uploaded.', 'Article now has draft status, since not all pictures were uploaded.', $item['pictures'], array($resized, $item['pictures'])));
            }
        }

        // release pagelock
        if (ModUtil::available('PageLock')) {
            ModUtil::apiFunc('PageLock', 'user', 'releaseLock', array('lockName' => "Newsnews{$item['sid']}"));
        }

        if ($item['action'] == self::ACTION_SAVEDRAFT_RETURN) {
            SessionUtil::setVar('newsitem', $item);
            $this->redirect(ModUtil::url('News', 'user', 'newitem'));
        }
        $this->redirect(ModUtil::url('News', 'user', 'view'));
    }
Example #3
0
    /**
     * This is a standard function that is called with the results of the
     * form supplied by News_admin_modify() to update a current item
     *
     * @param int 'sid' the id of the item to be updated
     * @param int 'objectid' generic object id maps to sid if present
     * @param string 'title' the title of the news item
     * @param string 'urltitle' the title of the news item formatted for the url
     * @param string 'language' the language of the news item
     * @param string 'bodytext' the summary text of the news item
     * @param int 'bodytextcontenttype' the content type of the summary text
     * @param string 'extendedtext' the body text of the news item
     * @param int 'extendedtextcontenttype' the content type of the body text
     * @param string 'notes' any administrator notes
     * @param int 'published_status' the published status of the item
     * @param int 'displayonindex' display the article on the index page
     * @author Mark West
     * @return bool true
     */
    public function update($args)
    {
        $this->checkCsrfToken();
        $story = FormUtil::getPassedValue('story', isset($args['story']) ? $args['story'] : null, 'POST');
        $files = News_ImageUtil::reArrayFiles(FormUtil::getPassedValue('news_files', null, 'FILES'));

        if (!empty($story['objectid'])) {
            $story['sid'] = $story['objectid'];
        }

        // Validate the essential parameters
        if (empty($story['sid'])) {
            return LogUtil::registerArgsError();
        }

        // Get the unedited news article for the permissions check
        $item = ModUtil::apiFunc('News', 'user', 'get', array('sid' => $story['sid']));
        if ($item === false) {
            return LogUtil::registerError($this->__('Error! No such article found.'), 404);
        }

        $this->throwForbiddenUnless(SecurityUtil::checkPermission('News::', $item['cr_uid'] . '::' . $item['sid'], ACCESS_EDIT), LogUtil::getErrorMsgPermission());

        // Reformat the attributes array
        if (isset($story['attributes'])) {
            $story['__ATTRIBUTES__'] = News_Util::reformatAttributes($story['attributes']);
            unset($story['attributes']);
        }

        // Validate the input
        $validationerror = News_Util::validateArticle($story);
        $hookvalidators = $this->notifyHooks(new Zikula_ValidationHook('news.ui_hooks.articles.validate_edit', new Zikula_Hook_ValidationProviders()))->getValidators();
        if ($hookvalidators->hasErrors()) {
            $validationerror .= $this->__('Error! Hooked content does not validate.') . "\n";
        }

        // if the user has selected to preview the article we then route them back
        // to the new function with the arguments passed here
        if ($story['action'] == 0 || $validationerror !== false) {
            // log the error found if any
            if ($validationerror !== false) {
                LogUtil::registerError(nl2br($validationerror));
            }
            // back to the referer form
            SessionUtil::setVar('newsitem', $story);
            return $this->redirect(ModUtil::url('News', 'admin', 'modify'));
        } else {
            // As we're not previewing the item let's remove it from the session
            SessionUtil::delVar('newsitem');
        }

        // Check if the article goes from pending to published
        if ($item['published_status'] == News_Api_User::STATUS_PENDING && $story['published_status'] == News_Api_User::STATUS_PUBLISHED) {
            $story['approver'] = UserUtil::getVar('uid');
        }

        $modvars = $this->getVars();

        // Handle Images
        if ($modvars['picupload_enabled']) {
            if (isset($story['del_pictures']) && !empty($story['del_pictures'])) {
                $deletedPics = News_ImageUtil::deleteImagesByName($modvars['picupload_uploaddir'], $story['del_pictures']);
                $story['pictures'] = $story['pictures'] - $deletedPics;
            }
            if (isset($deletedPics) && ($deletedPics > 0)) {
                $nextImageId = News_ImageUtil::renumberImages($item['pictures'], $story['sid']);
            } else {
                $nextImageId = isset($story['pictures']) ? $story['pictures'] : 0;
            }
            if (isset($files) && !empty($files)) {
                list($files, $story) = News_ImageUtil::validateImages($files, $story);
                $story['pictures'] = News_ImageUtil::resizeImages($story['sid'], $files, $nextImageId); // resize and move the uploaded pics
            }
        }


        // Update the story
        if (ModUtil::apiFunc('News', 'admin', 'update', array(
                    'sid' => $story['sid'],
                    'cr_uid' => $story['cr_uid'],
                    'contributor' => UserUtil::getVar('uname', $story['cr_uid'], $item['contributor']),
                    'title' => $story['title'],
                    'urltitle' => $story['urltitle'],
                    '__CATEGORIES__' => isset($story['__CATEGORIES__']) ? $story['__CATEGORIES__'] : null,
                    '__ATTRIBUTES__' => isset($story['__ATTRIBUTES__']) ? $story['__ATTRIBUTES__'] : null,
                    'language' => isset($story['language']) ? $story['language'] : '',
                    'hometext' => isset($story['hometext']) ? $story['hometext'] : '',
                    'hometextcontenttype' => $story['hometextcontenttype'],
                    'bodytext' => isset($story['bodytext']) ? $story['bodytext'] : '',
                    'bodytextcontenttype' => $story['bodytextcontenttype'],
                    'notes' => isset($story['notes']) ? $story['notes'] : '',
                    'displayonindex' => isset($story['displayonindex']) ? $story['displayonindex'] : 0,
                    'allowcomments' => isset($story['allowcomments']) ? $story['allowcomments'] : 0,
                    'unlimited' => isset($story['unlimited']) ? $story['unlimited'] : null,
                    'from' => $story['from'],
                    'tonolimit' => isset($story['tonolimit']) ? $story['tonolimit'] : null,
                    'to' => $story['to'],
                    'approver' => $story['approver'],
                    'weight' => isset($story['weight']) ? $story['weight'] : 0,
                    'pictures' => $story['pictures'],
                    'action' => $story['action']))) {
            // Success
            LogUtil::registerStatus($this->__('Done! Saved your changes.'));
        }

        // Let any hooks know that we have edited an item.
        $url = new Zikula_ModUrl('News', 'user', 'display', ZLanguage::getLanguageCode(), array('sid' => $story['sid']));
        $this->notifyHooks(new Zikula_ProcessHook('news.ui_hooks.articles.process_edit', $story['sid'], $url));
        // release pagelock
        if (ModUtil::available('PageLock')) {
            ModUtil::apiFunc('PageLock', 'user', 'releaseLock', array('lockName' => "Newsnews{$story['sid']}"));
        }

        // clear article and view caches
        ModUtil::apiFunc('News', 'user', 'clearItemCache', $story);

        return $this->redirect(ModUtil::url('News', 'admin', 'view'));
    }