Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_dataBinding_GenerisFormDataBinder | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| bind | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| bindUploadFileDescription | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| removeFile | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getServiceLocator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2016 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * A specific Data Binder which binds data coming from a Generis Form Instance |
| 29 | * the Generis persistent memory. |
| 30 | * |
| 31 | * If the target instance was not set, a new instance of the target class will |
| 32 | * created to receive the data to be bound. |
| 33 | * |
| 34 | * @access public |
| 35 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 36 | * @package tao |
| 37 | |
| 38 | */ |
| 39 | |
| 40 | use oat\oatbox\service\ServiceManager; |
| 41 | use oat\generis\model\fileReference\FileReferenceSerializer; |
| 42 | use oat\tao\model\upload\UploadService; |
| 43 | |
| 44 | // phpcs:disable Generic.Files.LineLength |
| 45 | class tao_models_classes_dataBinding_GenerisFormDataBinder extends tao_models_classes_dataBinding_GenerisInstanceDataBinder |
| 46 | { |
| 47 | // phpcs:enable Generic.Files.LineLength |
| 48 | /** |
| 49 | * Simply bind data from a Generis Instance Form to a specific generis class |
| 50 | * |
| 51 | * The array of the data to be bound must contain keys that are property |
| 52 | * The repspective values can be either scalar or vector (array) values or |
| 53 | * values. |
| 54 | * |
| 55 | * - If the element of the $data array is scalar, it is simply bound using |
| 56 | * - If the element of the $data array is a vector, the property values are |
| 57 | * with the values of the vector. |
| 58 | * - If the element is an object, the binder will infer the best method to |
| 59 | * it in the persistent memory, depending on its nature. |
| 60 | * |
| 61 | * @access public |
| 62 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 63 | * @param array $data An array of values where keys are Property URIs and values are either scalar, vector or object |
| 64 | * values. |
| 65 | * @return mixed |
| 66 | */ |
| 67 | public function bind($data) |
| 68 | { |
| 69 | $returnValue = null; |
| 70 | |
| 71 | |
| 72 | try { |
| 73 | $instance = parent::bind($data); |
| 74 | |
| 75 | // Take care of what the generic data binding did not. |
| 76 | foreach ($data as $p => $d) { |
| 77 | $property = new core_kernel_classes_Property($p); |
| 78 | |
| 79 | if ($d instanceof tao_helpers_form_data_UploadFileDescription) { |
| 80 | $this->bindUploadFileDescription($property, $d); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $returnValue = $instance; |
| 85 | } catch (common_Exception $e) { |
| 86 | $msg = "An error occured while binding property values to instance '': " . $e->getMessage(); |
| 87 | throw new tao_models_classes_dataBinding_GenerisFormDataBindingException($msg); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | return $returnValue; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Binds an UploadFileDescription with the target instance. |
| 96 | * |
| 97 | * @access protected |
| 98 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 99 | * @param core_kernel_classes_Property $property The property to bind the data. |
| 100 | * @param tao_helpers_form_data_UploadFileDescription $desc the upload file description. |
| 101 | * @return void |
| 102 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 103 | * @throws \common_Exception |
| 104 | */ |
| 105 | protected function bindUploadFileDescription( |
| 106 | core_kernel_classes_Property $property, |
| 107 | tao_helpers_form_data_UploadFileDescription $desc |
| 108 | ) { |
| 109 | $instance = $this->getTargetInstance(); |
| 110 | |
| 111 | // If form has delete action, remove file |
| 112 | if ($desc->getAction() == tao_helpers_form_data_UploadFileDescription::FORM_ACTION_DELETE) { |
| 113 | $this->removeFile($property); |
| 114 | // If form has add action, remove file & replace by new |
| 115 | } elseif ($desc->getAction() == tao_helpers_form_data_UploadFileDescription::FORM_ACTION_ADD) { |
| 116 | $name = $desc->getName(); |
| 117 | $size = $desc->getSize(); |
| 118 | |
| 119 | if (! empty($name) && ! empty($size)) { |
| 120 | // Remove old |
| 121 | $this->removeFile($property); |
| 122 | |
| 123 | // Move the file at the right place. |
| 124 | $source = $desc->getTmpPath(); |
| 125 | $serial = tao_models_classes_TaoService::singleton()->storeUploadedFile($source, $name); |
| 126 | $this->getServiceLocator()->get(UploadService::SERVICE_ID)->remove($source); |
| 127 | |
| 128 | // Create association between item & file, database side |
| 129 | $instance->editPropertyValues($property, $serial); |
| 130 | |
| 131 | // Update the UploadFileDescription with the stored file. |
| 132 | $desc->setFile($serial); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Remove file stored into given $property from $targetInstance |
| 139 | * - Remove file properties |
| 140 | * - Remove physically file |
| 141 | * - Remove property link between item & file |
| 142 | * |
| 143 | * @param core_kernel_classes_Property $property |
| 144 | */ |
| 145 | protected function removeFile(core_kernel_classes_Property $property) |
| 146 | { |
| 147 | $instance = $this->getTargetInstance(); |
| 148 | $referencer = $this->getServiceLocator()->get(FileReferenceSerializer::SERVICE_ID); |
| 149 | |
| 150 | foreach ($instance->getPropertyValues($property) as $oldFileSerial) { |
| 151 | /** @var \oat\oatbox\filesystem\File $oldFile */ |
| 152 | $oldFile = $referencer->unserializeFile($oldFileSerial); |
| 153 | $oldFile->delete(); |
| 154 | $referencer->cleanup($oldFileSerial); |
| 155 | $instance->removePropertyValue($property, $oldFileSerial); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return ServiceManager |
| 161 | */ |
| 162 | public function getServiceLocator() |
| 163 | { |
| 164 | return ServiceManager::getServiceManager(); |
| 165 | } |
| 166 | } |