formaction.php 3.18 KB
Newer Older
mmn's avatar
mmn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
/**
 * Form action extendable class.
 *
 * PHP version 5
 *
 * @category Action
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://status.net/
 *
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2008, 2009, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

30
if (!defined('GNUSOCIAL')) { exit(1); }
mmn's avatar
mmn committed
31 32 33 34 35 36 37 38 39 40 41

/**
 * Form action extendable class
 *
 * @category Action
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @author   Mikael Nordfeldth <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://status.net/
 */
mmn's avatar
mmn committed
42
class FormAction extends ManagedAction
mmn's avatar
mmn committed
43 44
{
    protected $form = null;
45
    protected $formOpts = array();
mmn's avatar
mmn committed
46
    protected $type = null;
47
    protected $needLogin = true;
mmn's avatar
mmn committed
48
    protected $canPost = true;
mmn's avatar
mmn committed
49 50 51 52

    protected function prepare(array $args=array()) {
        parent::prepare($args);

53
        $this->form = $this->form ?: ucfirst($this->action);
mmn's avatar
mmn committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        $this->args['form'] = $this->form;

        $this->type = !is_null($this->type) ? $this->type : $this->trimmed('type');
        $this->args['context'] = $this->trimmed('action');  // reply for notice for example

        if (!$this->type) {
            $this->type = null;
        }

        return true;
    }

    public function isReadOnly($args) {
        return !$this->isPost();
    }

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    public function showPageNotice()
    {
        $this->showInstructions();
        if ($msg = $this->getError()) {
            $this->element('div', 'error', $msg);
        }
        if ($msg = $this->getInfo()) {
            $this->element('div', 'info', $msg);
        }
    }

    /**
     * Outputs the instructions for the form
     *
     * SHOULD overload
     */
mmn's avatar
mmn committed
86 87 88
    public function showInstructions()
    {
        // instructions are nice, so users know what to do
89 90 91 92 93 94
        $this->raw(common_markup_to_html($this->getInstructions()));
    }

    /**
     * @return string with instructions to pass into common_markup_to_html()
     */
95
    protected function getInstructions()
96 97
    {
        return null;
98 99
    }

100 101 102 103 104 105 106
    public function showForm($msg=null, $success=false)
    {
        $this->msg = $msg;
        $this->success = $success;
        $this->showPage();
    }

107
    protected function showContent()
108 109 110 111 112 113 114 115
    {
        $form = $this->getForm();
        $form->show();
    }

    protected function getForm()
    {
        $class = $this->form.'Form';
116
        $form = new $class($this, $this->formOpts);
117 118
        return $form;
    }
mmn's avatar
mmn committed
119
}