Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 | 33 | * $Id$ |
6a488035 TO |
34 | */ |
35 | class CRM_Core_QuickForm_Action_Upload extends CRM_Core_QuickForm_Action { | |
36 | ||
37 | /** | |
100fef9d | 38 | * The array of uploaded file names |
6a488035 TO |
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 | /** | |
5c9ff055 | 50 | * Class constructor. |
6a488035 | 51 | * |
6a0b768e TO |
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. | |
6a488035 | 58 | * |
77b97be7 | 59 | * @return \CRM_Core_QuickForm_Action_Upload |
77b97be7 | 60 | */ |
00be9182 | 61 | public function __construct(&$stateMachine, $uploadDir, $uploadNames) { |
6a488035 TO |
62 | parent::__construct($stateMachine); |
63 | ||
64 | $this->_uploadDir = $uploadDir; | |
65 | $this->_uploadNames = $uploadNames; | |
66 | } | |
67 | ||
68 | /** | |
0965e988 | 69 | * Upload and move the file if valid to the uploaded directory. |
6a488035 | 70 | * |
6a0b768e TO |
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. | |
6a488035 | 79 | */ |
00be9182 | 80 | public function upload(&$page, &$data, $pageName, $uploadName) { |
6a488035 TO |
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) { | |
353ffa53 TO |
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'], | |
0965e988 | 101 | 2 => $this->_uploadDir, |
353ffa53 | 102 | ))); |
6a488035 TO |
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 | * | |
6a0b768e TO |
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. | |
6a488035 | 123 | */ |
00be9182 | 124 | public function perform(&$page, $actionName) { |
6a488035 TO |
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 | |
0965e988 | 132 | // this allows standalone form uploads to work nicely |
6a488035 TO |
133 | $page->controller->_actions['upload']->realPerform($page, $actionName); |
134 | } | |
135 | ||
a0ee3941 | 136 | /** |
5c9ff055 EM |
137 | * Real perform. |
138 | * | |
d8689418 | 139 | * @todo document what I do. |
0965e988 | 140 | * |
77fbc665 | 141 | * @param CRM_Core_Form $page |
100fef9d | 142 | * @param string $actionName |
a0ee3941 EM |
143 | * |
144 | * @return mixed | |
145 | */ | |
00be9182 | 146 | public function realPerform(&$page, $actionName) { |
6a488035 TO |
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 | } |