3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Redefine the upload action.
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Core_QuickForm_Action_Upload
extends CRM_Core_QuickForm_Action
{
22 * The array of uploaded file names.
25 protected $_uploadNames;
28 * The directory to store the uploaded files.
31 protected $_uploadDir;
36 * @param object $stateMachine
37 * Reference to state machine object.
38 * @param string $uploadDir
39 * Directory to store the uploaded files.
40 * @param array $uploadNames
41 * Element names of the various uploadable files.
43 * @return \CRM_Core_QuickForm_Action_Upload
45 public function __construct(&$stateMachine, $uploadDir, $uploadNames) {
46 parent
::__construct($stateMachine);
48 $this->_uploadDir
= $uploadDir;
49 $this->_uploadNames
= $uploadNames;
53 * Upload and move the file if valid to the uploaded directory.
55 * @param CRM_Core_Form $page
56 * The CRM_Core_Form object.
58 * The QFC data container.
59 * @param string $pageName
60 * The name of the page which index the data container with.
61 * @param string $uploadName
62 * The name of the uploaded file.
64 public function upload(&$page, &$data, $pageName, $uploadName) {
65 // make sure uploadName exists in the QF array
66 // else we skip, CRM-3427
67 if (empty($uploadName) ||
68 !isset($page->_elementIndex
[$uploadName])
73 // get the element containing the upload
74 $element = &$page->getElement($uploadName);
75 if ('file' == $element->getType()) {
76 if ($element->isUploadedFile()) {
77 // rename the uploaded file with a unique number at the end
78 $value = $element->getValue();
80 $newName = CRM_Utils_File
::makeFileName($value['name']);
81 $status = $element->moveUploadedFile($this->_uploadDir
, $newName);
83 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.', [
85 2 => $this->_uploadDir
,
88 if (!empty($data['values'][$pageName][$uploadName]['name'])) {
89 @unlink
($this->_uploadDir
. $data['values'][$pageName][$uploadName]);
93 'name' => $this->_uploadDir
. $newName,
94 'type' => $value['type'],
96 //CRM-19460 handle brackets if present in $uploadName, similar things we do it for all other inputs.
97 $value = $element->_prepareValue($value, TRUE);
98 $data['values'][$pageName] = HTML_QuickForm
::arrayMerge($data['values'][$pageName], $value);
104 * Processes the request.
106 * @param CRM_Core_Form $page
107 * CRM_Core_Form the current form-page.
108 * @param string $actionName
109 * Current action name, as one Action object can serve multiple actions.
111 public function perform(&$page, $actionName) {
112 // like in Action_Next
113 $page->isFormBuilt() or $page->buildForm();
115 // so this is a brain-seizure moment, so hang tight (real tight!)
116 // the above buildForm potentially changes the action function with different args
117 // so basically the rug might have been pulled from us, so we actually just check
118 // and potentially call the right one
119 // this allows standalone form uploads to work nicely
120 $page->controller
->_actions
['upload']->realPerform($page, $actionName);
126 * @todo document what I do.
128 * @param CRM_Core_Form $page
129 * @param string $actionName
133 public function realPerform(&$page, $actionName) {
134 $pageName = $page->getAttribute('name');
135 $data = &$page->controller
->container();
136 $data['values'][$pageName] = $page->exportValues();
137 $data['valid'][$pageName] = $page->validate();
139 if (!$data['valid'][$pageName]) {
140 return $page->handle('display');
143 foreach ($this->_uploadNames
as $name) {
144 $this->upload($page, $data, $pageName, $name);
147 $state = &$this->_stateMachine
->getState($pageName);
149 return $page->handle('display');
152 // the page is valid, process it before we jump to the next state
153 $page->mainProcess();
155 // check if destination is set, if so goto destination
156 $destination = $this->_stateMachine
->getDestination();
158 $destination = urldecode($destination);
159 CRM_Utils_System
::redirect($destination);
162 return $state->handleNextState($page);