form.php
202 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.mvc |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | JLoader::import('adapter.mvc.model'); |
| 15 | JLoader::import('adapter.form.form'); |
| 16 | |
| 17 | /** |
| 18 | * The form model class used by the MVC framework. |
| 19 | * A form model can be used by a controller or a view to handle |
| 20 | * a FORM entity of the plugin. |
| 21 | * |
| 22 | * The model can be invoked when the value contained |
| 23 | * in $_REQUEST['task'] is equals to 'ComponentModel' + $_REQUEST['task']. |
| 24 | * |
| 25 | * e.g. $_REQUEST['task'] = 'groups.save' -> ComponentModelGroups |
| 26 | * |
| 27 | * @since 10.0 |
| 28 | */ |
| 29 | abstract class JModelForm extends JModel |
| 30 | { |
| 31 | /** |
| 32 | * Creates or updates the specified record. |
| 33 | * |
| 34 | * @param mixed $data The record to insert. |
| 35 | * |
| 36 | * @return mixed The ID of the inserted record on success, false otherwise. |
| 37 | */ |
| 38 | public function save(&$data) |
| 39 | { |
| 40 | /** |
| 41 | * Rely on JTable instance to complete the saving process. |
| 42 | * |
| 43 | * @since 10.1.35 |
| 44 | */ |
| 45 | $table = $this->getTable(); |
| 46 | |
| 47 | // attempt to save data |
| 48 | if ($table->save($data)) |
| 49 | { |
| 50 | // saved successfully, return PK |
| 51 | return $table->{$table->getKeyName()}; |
| 52 | } |
| 53 | |
| 54 | // something went wrong, try to obtain an error |
| 55 | $error = $table->getError($get_last = null, $string = true); |
| 56 | |
| 57 | if ($error) |
| 58 | { |
| 59 | // error found, register it within the model |
| 60 | $this->setError($error); |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Deletes the specified records. |
| 68 | * |
| 69 | * @param mixed $ids The PK value (or a list of values) of the record(s) to remove. |
| 70 | * |
| 71 | * @return boolean True if at least a record has been removed, otherwise false. |
| 72 | */ |
| 73 | public function delete($ids) |
| 74 | { |
| 75 | /** |
| 76 | * Rely on JTable instance to complete the deleting process. |
| 77 | * |
| 78 | * @since 10.1.35 |
| 79 | */ |
| 80 | $table = $this->getTable(); |
| 81 | |
| 82 | $return = false; |
| 83 | |
| 84 | foreach ((array) $ids as $id) |
| 85 | { |
| 86 | if ($table->delete($id)) |
| 87 | { |
| 88 | $return = true; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | // something went wrong, try to obtain an error |
| 93 | $error = $table->getError($get_last = null, $string = true); |
| 94 | |
| 95 | if ($error) |
| 96 | { |
| 97 | // error found, register it within the model |
| 98 | $this->setError($error); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return $return; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Basic item loading implementation. |
| 108 | * |
| 109 | * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match. |
| 110 | * If not set the instance property value is used. |
| 111 | * @param boolean $new True to return an empty object if missing. |
| 112 | * |
| 113 | * @return mixed The record object on success, null otherwise. |
| 114 | * |
| 115 | * @since 10.1.35 Added support for $new argument. |
| 116 | */ |
| 117 | public function getItem($pk, $new = false) |
| 118 | { |
| 119 | /** |
| 120 | * Rely on JTable instance to complete the fetching process. |
| 121 | * |
| 122 | * @since 10.1.35 |
| 123 | */ |
| 124 | $table = $this->getTable(); |
| 125 | |
| 126 | // reset table to make sure we obtain valid values |
| 127 | $table->reset(); |
| 128 | |
| 129 | // attempt to load record |
| 130 | $loaded = ($pk && $table->load($pk)); |
| 131 | |
| 132 | if ($loaded || $new === true) |
| 133 | { |
| 134 | // loaded successfully or requested an empty object |
| 135 | return (object) $table->getProperties(); |
| 136 | } |
| 137 | |
| 138 | // something went wrong, try to obtain an error |
| 139 | $error = $table->getError($get_last = null, $string = true); |
| 140 | |
| 141 | if ($error) |
| 142 | { |
| 143 | // error found, register it within the model |
| 144 | $this->setError($error); |
| 145 | } |
| 146 | |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Obtains the JForm object related to the model view. |
| 152 | * |
| 153 | * @return JForm The form object. |
| 154 | */ |
| 155 | public function getForm() |
| 156 | { |
| 157 | $comp = $this->getComponentName(); |
| 158 | $name = $this->getModelName(); |
| 159 | $client = $this->getClientFolder(); |
| 160 | |
| 161 | $id = serialize(array($comp, $name, $client)); |
| 162 | $path = implode(DIRECTORY_SEPARATOR, array(WP_PLUGIN_DIR, $comp, $client, 'views', $name, 'tmpl', 'default.xml')); |
| 163 | |
| 164 | try |
| 165 | { |
| 166 | $form = JForm::getInstance($id, $path); |
| 167 | } |
| 168 | catch (Exception $e) |
| 169 | { |
| 170 | $form = null; |
| 171 | } |
| 172 | |
| 173 | return $form; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * This method should be used to pre-load an item considering |
| 178 | * the data set in the request. |
| 179 | * |
| 180 | * For example, if the request owns an ID, this method may try |
| 181 | * to retrieve the item from the database. |
| 182 | * Otherwise it may return an empty object. |
| 183 | * |
| 184 | * @return array|object The object found. |
| 185 | */ |
| 186 | public function loadFormData() |
| 187 | { |
| 188 | return array(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * This method should be used to retrieve the posted data |
| 193 | * after the form submission. |
| 194 | * |
| 195 | * @return array|object The data object. |
| 196 | */ |
| 197 | public function getFormData() |
| 198 | { |
| 199 | return array(); |
| 200 | } |
| 201 | } |
| 202 |