]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/formaction.php
FormAction updates, also fixing NoticeForm CSS
[quix0rs-gnu-social.git] / lib / formaction.php
1 <?php
2 /**
3  * Form action extendable class.
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2008, 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Form action extendable class
36  *
37  * @category Action
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @author   Mikael Nordfeldth <evan@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
42  * @link     http://status.net/
43  */
44 class FormAction extends ManagedAction
45 {
46     protected $form = null;
47     protected $type = null;
48     protected $needLogin = true;
49     protected $canPost = true;
50
51     protected function prepare(array $args=array()) {
52         parent::prepare($args);
53
54         $this->form = $this->form ?: $this->action;
55         $this->args['form'] = $this->form;
56
57         $this->type = !is_null($this->type) ? $this->type : $this->trimmed('type');
58         $this->args['context'] = $this->trimmed('action');  // reply for notice for example
59
60         if (!$this->type) {
61             $this->type = null;
62         }
63
64         return true;
65     }
66
67     public function isReadOnly($args) {
68         return !$this->isPost();
69     }
70
71     public function showPageNotice()
72     {
73         $this->showInstructions();
74         if ($msg = $this->getError()) {
75             $this->element('div', 'error', $msg);
76         }
77         if ($msg = $this->getInfo()) {
78             $this->element('div', 'info', $msg);
79         }
80     }
81
82     /**
83      * Outputs the instructions for the form
84      *
85      * SHOULD overload
86      */
87     public function showInstructions()
88     {
89         // instructions are nice, so users know what to do
90         $this->raw(common_markup_to_html($this->getInstructions()));
91     }
92
93     /**
94      * @return string with instructions to pass into common_markup_to_html()
95      */
96     public function getInstructions()
97     {
98         return null;
99     }
100
101     public function showForm($msg=null, $success=false)
102     {
103         $this->msg = $msg;
104         $this->success = $success;
105         $this->showPage();
106     }
107
108     public function showContent()
109     {
110         $form = $this->getForm();
111         $form->show();
112     }
113
114     protected function getForm()
115     {
116         $class = $this->form.'Form';
117         $form = new $class($this);
118         return $form;
119     }
120
121     /**
122      * Gets called from handle() if isPost() is true;
123      * @return void
124      */
125     protected function handlePost()
126     {
127         parent::handlePost();
128
129         // check for this before token since all POST and FILES data
130         // is losts when size is exceeded
131         if (empty($_POST) && $_SERVER['CONTENT_LENGTH']>0) {
132             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
133             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
134             $msg = _m('The server was unable to handle that much POST data (%s MiB) due to its current configuration.',
135                       'The server was unable to handle that much POST data (%s MiB) due to its current configuration.',
136                       round($_SERVER['CONTENT_LENGTH']/1024/1024,2));
137             throw new ClientException($msg);
138         }
139
140         $this->checkSessionToken();
141     }
142 }