commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Core / QuickForm / Action / Upload.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Redefine the upload action.
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2015
33 * $Id$
34 */
35 class CRM_Core_QuickForm_Action_Upload extends CRM_Core_QuickForm_Action {
36
37 /**
38 * The array of uploaded file names.
39 * @var array
40 */
41 protected $_uploadNames;
42
43 /**
44 * The directory to store the uploaded files.
45 * @var string
46 */
47 protected $_uploadDir;
48
49 /**
50 * Class constructor.
51 *
52 * @param object $stateMachine
53 * Reference to state machine object.
54 * @param string $uploadDir
55 * Directory to store the uploaded files.
56 * @param array $uploadNames
57 * Element names of the various uploadable files.
58 *
59 * @return \CRM_Core_QuickForm_Action_Upload
60 */
61 public function __construct(&$stateMachine, $uploadDir, $uploadNames) {
62 parent::__construct($stateMachine);
63
64 $this->_uploadDir = $uploadDir;
65 $this->_uploadNames = $uploadNames;
66 }
67
68 /**
69 * Upload and move the file if valid to the uploaded directory.
70 *
71 * @param CRM_Core_Form $page
72 * The CRM_Core_Form object.
73 * @param object $data
74 * The QFC data container.
75 * @param string $pageName
76 * The name of the page which index the data container with.
77 * @param string $uploadName
78 * The name of the uploaded file.
79 */
80 public function upload(&$page, &$data, $pageName, $uploadName) {
81 // make sure uploadName exists in the QF array
82 // else we skip, CRM-3427
83 if (empty($uploadName) ||
84 !isset($page->_elementIndex[$uploadName])
85 ) {
86 return;
87 }
88
89 // get the element containing the upload
90 $element = &$page->getElement($uploadName);
91 if ('file' == $element->getType()) {
92 if ($element->isUploadedFile()) {
93 // rename the uploaded file with a unique number at the end
94 $value = $element->getValue();
95
96 $newName = CRM_Utils_File::makeFileName($value['name']);
97 $status = $element->moveUploadedFile($this->_uploadDir, $newName);
98 if (!$status) {
99 CRM_Core_Error::statusBounce(ts('We could not move the uploaded file %1 to the upload directory %2. Please verify that the \'Temporary Files\' setting points to a valid path which is writable by your web server.', array(
100 1 => $value['name'],
101 2 => $this->_uploadDir,
102 )));
103 }
104 if (!empty($data['values'][$pageName][$uploadName]['name'])) {
105 @unlink($this->_uploadDir . $data['values'][$pageName][$uploadName]);
106 }
107
108 $data['values'][$pageName][$uploadName] = array(
109 'name' => $this->_uploadDir . $newName,
110 'type' => $value['type'],
111 );
112 }
113 }
114 }
115
116 /**
117 * Processes the request.
118 *
119 * @param CRM_Core_Form $page
120 * CRM_Core_Form the current form-page.
121 * @param string $actionName
122 * Current action name, as one Action object can serve multiple actions.
123 */
124 public function perform(&$page, $actionName) {
125 // like in Action_Next
126 $page->isFormBuilt() or $page->buildForm();
127
128 // so this is a brain-seizure moment, so hang tight (real tight!)
129 // the above buildForm potentially changes the action function with different args
130 // so basically the rug might have been pulled from us, so we actually just check
131 // and potentially call the right one
132 // this allows standalone form uploads to work nicely
133 $page->controller->_actions['upload']->realPerform($page, $actionName);
134 }
135
136 /**
137 * Real perform.
138 *
139 * @todo document what I do.
140 *
141 * @param CRM_Core_Form $page
142 * @param string $actionName
143 *
144 * @return mixed
145 */
146 public function realPerform(&$page, $actionName) {
147 $pageName = $page->getAttribute('name');
148 $data = &$page->controller->container();
149 $data['values'][$pageName] = $page->exportValues();
150 $data['valid'][$pageName] = $page->validate();
151
152 if (!$data['valid'][$pageName]) {
153 return $page->handle('display');
154 }
155
156 foreach ($this->_uploadNames as $name) {
157 $this->upload($page, $data, $pageName, $name);
158 }
159
160 $state = &$this->_stateMachine->getState($pageName);
161 if (empty($state)) {
162 return $page->handle('display');
163 }
164
165 // the page is valid, process it before we jump to the next state
166 $page->mainProcess();
167
168 // check if destination is set, if so goto destination
169 $destination = $this->_stateMachine->getDestination();
170 if ($destination) {
171 $destination = urldecode($destination);
172 CRM_Utils_System::redirect($destination);
173 }
174 else {
175 return $state->handleNextState($page);
176 }
177 }
178
179 }