3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Redefine the upload action.
32 * @copyright CiviCRM LLC (c) 2004-2017
35 class CRM_Core_QuickForm_Action_Upload
extends CRM_Core_QuickForm_Action
{
38 * The array of uploaded file names.
41 protected $_uploadNames;
44 * The directory to store the uploaded files.
47 protected $_uploadDir;
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.
59 * @return \CRM_Core_QuickForm_Action_Upload
61 public function __construct(&$stateMachine, $uploadDir, $uploadNames) {
62 parent
::__construct($stateMachine);
64 $this->_uploadDir
= $uploadDir;
65 $this->_uploadNames
= $uploadNames;
69 * Upload and move the file if valid to the uploaded directory.
71 * @param CRM_Core_Form $page
72 * The CRM_Core_Form object.
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.
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])
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();
96 $newName = CRM_Utils_File
::makeFileName($value['name']);
97 $status = $element->moveUploadedFile($this->_uploadDir
, $newName);
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(
101 2 => $this->_uploadDir
,
104 if (!empty($data['values'][$pageName][$uploadName]['name'])) {
105 @unlink
($this->_uploadDir
. $data['values'][$pageName][$uploadName]);
109 'name' => $this->_uploadDir
. $newName,
110 'type' => $value['type'],
112 //CRM-19460 handle brackets if present in $uploadName, similar things we do it for all other inputs.
113 $value = $element->_prepareValue($value, TRUE);
114 $data['values'][$pageName] = HTML_QuickForm
::arrayMerge($data['values'][$pageName], $value);
120 * Processes the request.
122 * @param CRM_Core_Form $page
123 * CRM_Core_Form the current form-page.
124 * @param string $actionName
125 * Current action name, as one Action object can serve multiple actions.
127 public function perform(&$page, $actionName) {
128 // like in Action_Next
129 $page->isFormBuilt() or $page->buildForm();
131 // so this is a brain-seizure moment, so hang tight (real tight!)
132 // the above buildForm potentially changes the action function with different args
133 // so basically the rug might have been pulled from us, so we actually just check
134 // and potentially call the right one
135 // this allows standalone form uploads to work nicely
136 $page->controller
->_actions
['upload']->realPerform($page, $actionName);
142 * @todo document what I do.
144 * @param CRM_Core_Form $page
145 * @param string $actionName
149 public function realPerform(&$page, $actionName) {
150 $pageName = $page->getAttribute('name');
151 $data = &$page->controller
->container();
152 $data['values'][$pageName] = $page->exportValues();
153 $data['valid'][$pageName] = $page->validate();
155 if (!$data['valid'][$pageName]) {
156 return $page->handle('display');
159 foreach ($this->_uploadNames
as $name) {
160 $this->upload($page, $data, $pageName, $name);
163 $state = &$this->_stateMachine
->getState($pageName);
165 return $page->handle('display');
168 // the page is valid, process it before we jump to the next state
169 $page->mainProcess();
171 // check if destination is set, if so goto destination
172 $destination = $this->_stateMachine
->getDestination();
174 $destination = urldecode($destination);
175 CRM_Utils_System
::redirect($destination);
178 return $state->handleNextState($page);