]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/formaction.php
Improved type-hint for following methods:
[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 $formOpts = array();
48     protected $type = null;
49     protected $needLogin = true;
50     protected $canPost = true;
51
52     protected function prepare(array $args=array()) {
53         parent::prepare($args);
54
55         $this->form = $this->form ?: $this->action;
56         $this->args['form'] = $this->form;
57
58         $this->type = !is_null($this->type) ? $this->type : $this->trimmed('type');
59         $this->args['context'] = $this->trimmed('action');  // reply for notice for example
60
61         if (!$this->type) {
62             $this->type = null;
63         }
64
65         return true;
66     }
67
68     public function isReadOnly(array $args=array()) {
69         return !$this->isPost();
70     }
71
72     public function showPageNotice()
73     {
74         $this->showInstructions();
75         if ($msg = $this->getError()) {
76             $this->element('div', 'error', $msg);
77         }
78         if ($msg = $this->getInfo()) {
79             $this->element('div', 'info', $msg);
80         }
81     }
82
83     /**
84      * Outputs the instructions for the form
85      *
86      * SHOULD overload
87      */
88     public function showInstructions()
89     {
90         // instructions are nice, so users know what to do
91         $this->raw(common_markup_to_html($this->getInstructions()));
92     }
93
94     /**
95      * @return string with instructions to pass into common_markup_to_html()
96      */
97     public function getInstructions()
98     {
99         return null;
100     }
101
102     public function showForm($msg=null, $success=false)
103     {
104         $this->msg = $msg;
105         $this->success = $success;
106         $this->showPage();
107     }
108
109     protected function showContent()
110     {
111         $form = $this->getForm();
112         $form->show();
113     }
114
115     protected function getForm()
116     {
117         $class = $this->form.'Form';
118         $form = new $class($this, $this->formOpts);
119         return $form;
120     }
121
122     /**
123      * Gets called from handle() if isPost() is true;
124      * @return void
125      */
126     protected function handlePost()
127     {
128         parent::handlePost();
129
130         // check for this before token since all POST and FILES data
131         // is losts when size is exceeded
132         if (empty($_POST) && $_SERVER['CONTENT_LENGTH']>0) {
133             // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
134             // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
135             $msg = _m('The server was unable to handle that much POST data (%s MiB) due to its current configuration.',
136                       'The server was unable to handle that much POST data (%s MiB) due to its current configuration.',
137                       round($_SERVER['CONTENT_LENGTH']/1024/1024,2));
138             throw new ClientException($msg);
139         }
140
141         $this->checkSessionToken();
142     }
143 }