[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Administrator 5 * @subpackage com_mails 6 * 7 * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Mails\Administrator\Model; 12 13 use Joomla\CMS\Component\ComponentHelper; 14 use Joomla\CMS\Factory; 15 use Joomla\CMS\Filesystem\Path; 16 use Joomla\CMS\Form\Form; 17 use Joomla\CMS\Language\LanguageHelper; 18 use Joomla\CMS\Language\Text; 19 use Joomla\CMS\MVC\Model\AdminModel; 20 use Joomla\CMS\Object\CMSObject; 21 use Joomla\CMS\Table\Table; 22 use Joomla\Registry\Registry; 23 use Joomla\Utilities\ArrayHelper; 24 25 // phpcs:disable PSR1.Files.SideEffects 26 \defined('_JEXEC') or die; 27 // phpcs:enable PSR1.Files.SideEffects 28 29 /** 30 * Item Model for a Mail template. 31 * 32 * @since 4.0.0 33 */ 34 class TemplateModel extends AdminModel 35 { 36 /** 37 * The prefix to use with controller messages. 38 * 39 * @var string 40 * @since 4.0.0 41 */ 42 protected $text_prefix = 'COM_MAILS'; 43 44 /** 45 * The type alias for this content type (for example, 'com_content.article'). 46 * 47 * @var string 48 * @since 4.0.0 49 */ 50 public $typeAlias = 'com_mails.template'; 51 52 /** 53 * Method to test whether a record can be deleted. 54 * 55 * @param object $record A record object. 56 * 57 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 58 * 59 * @since 4.0.0 60 */ 61 protected function canDelete($record) 62 { 63 return false; 64 } 65 66 /** 67 * Method to get the record form. 68 * 69 * @param array $data An optional array of data for the form to interrogate. 70 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 71 * 72 * @return \Joomla\CMS\Form\Form|bool A JForm object on success, false on failure 73 * 74 * @since 4.0.0 75 */ 76 public function getForm($data = array(), $loadData = true) 77 { 78 // Get the form. 79 $form = $this->loadForm('com_mails.template', 'template', array('control' => 'jform', 'load_data' => $loadData)); 80 81 if (empty($form)) { 82 return false; 83 } 84 85 $params = ComponentHelper::getParams('com_mails'); 86 87 if ($params->get('mail_style', 'plaintext') == 'plaintext') { 88 $form->removeField('htmlbody'); 89 } 90 91 if ($params->get('mail_style', 'plaintext') == 'html') { 92 $form->removeField('body'); 93 } 94 95 if (!$params->get('alternative_mailconfig', '0')) { 96 $form->removeField('alternative_mailconfig', 'params'); 97 $form->removeField('mailfrom', 'params'); 98 $form->removeField('fromname', 'params'); 99 $form->removeField('replyto', 'params'); 100 $form->removeField('replytoname', 'params'); 101 $form->removeField('mailer', 'params'); 102 $form->removeField('sendmail', 'params'); 103 $form->removeField('smtphost', 'params'); 104 $form->removeField('smtpport', 'params'); 105 $form->removeField('smtpsecure', 'params'); 106 $form->removeField('smtpauth', 'params'); 107 $form->removeField('smtpuser', 'params'); 108 $form->removeField('smtppass', 'params'); 109 } 110 111 if (!$params->get('copy_mails')) { 112 $form->removeField('copyto', 'params'); 113 } 114 115 if (!trim($params->get('attachment_folder', ''))) { 116 $form->removeField('attachments'); 117 118 return $form; 119 } 120 121 try { 122 $attachmentPath = rtrim(Path::check(JPATH_ROOT . '/' . $params->get('attachment_folder')), \DIRECTORY_SEPARATOR); 123 } catch (\Exception $e) { 124 $attachmentPath = ''; 125 } 126 127 if (!$attachmentPath || $attachmentPath === Path::clean(JPATH_ROOT) || !is_dir($attachmentPath)) { 128 $form->removeField('attachments'); 129 130 return $form; 131 } 132 133 $field = $form->getField('attachments'); 134 $subform = new \SimpleXMLElement($field->formsource); 135 $files = $subform->xpath('field[@name="file"]'); 136 $files[0]->addAttribute('directory', $attachmentPath); 137 $form->load('<form><field name="attachments" type="subform" ' 138 . 'label="COM_MAILS_FIELD_ATTACHMENTS_LABEL" multiple="true" ' 139 . 'layout="joomla.form.field.subform.repeatable-table">' 140 . str_replace('<?xml version="1.0"?>', '', $subform->asXML()) 141 . '</field></form>'); 142 143 return $form; 144 } 145 146 /** 147 * Method to get a single record. 148 * 149 * @param integer $pk The id of the primary key. 150 * 151 * @return CMSObject|boolean Object on success, false on failure. 152 * 153 * @since 4.0.0 154 */ 155 public function getItem($pk = null) 156 { 157 $templateId = $this->getState($this->getName() . '.template_id'); 158 $language = $this->getState($this->getName() . '.language'); 159 $table = $this->getTable('Template', 'Table'); 160 161 if ($templateId != '' && $language != '') { 162 // Attempt to load the row. 163 $return = $table->load(array('template_id' => $templateId, 'language' => $language)); 164 165 // Check for a table object error. 166 if ($return === false && $table->getError()) { 167 $this->setError($table->getError()); 168 169 return false; 170 } 171 } 172 173 // Convert to the CMSObject before adding other data. 174 $properties = $table->getProperties(1); 175 $item = ArrayHelper::toObject($properties, CMSObject::class); 176 177 if (property_exists($item, 'params')) { 178 $registry = new Registry($item->params); 179 $item->params = $registry->toArray(); 180 } 181 182 if (!$item->template_id) { 183 $item->template_id = $templateId; 184 } 185 186 if (!$item->language) { 187 $item->language = $language; 188 } 189 190 return $item; 191 } 192 193 /** 194 * Get the master data for a mail template. 195 * 196 * @param integer $pk The id of the primary key. 197 * 198 * @return CMSObject|boolean Object on success, false on failure. 199 * 200 * @since 4.0.0 201 */ 202 public function getMaster($pk = null) 203 { 204 $template_id = $this->getState($this->getName() . '.template_id'); 205 $table = $this->getTable('Template', 'Table'); 206 207 if ($template_id != '') { 208 // Attempt to load the row. 209 $return = $table->load(array('template_id' => $template_id, 'language' => '')); 210 211 // Check for a table object error. 212 if ($return === false && $table->getError()) { 213 $this->setError($table->getError()); 214 215 return false; 216 } 217 } 218 219 // Convert to the CMSObject before adding other data. 220 $properties = $table->getProperties(1); 221 $item = ArrayHelper::toObject($properties, CMSObject::class); 222 223 if (property_exists($item, 'params')) { 224 $registry = new Registry($item->params); 225 $item->params = $registry->toArray(); 226 } 227 228 return $item; 229 } 230 231 /** 232 * Method to get a table object, load it if necessary. 233 * 234 * @param string $name The table name. Optional. 235 * @param string $prefix The class prefix. Optional. 236 * @param array $options Configuration array for model. Optional. 237 * 238 * @return Table A Table object 239 * 240 * @since 4.0.0 241 * @throws \Exception 242 */ 243 public function getTable($name = 'Template', $prefix = 'Administrator', $options = array()) 244 { 245 return parent::getTable($name, $prefix, $options); 246 } 247 248 /** 249 * Method to get the data that should be injected in the form. 250 * 251 * @return mixed The data for the form. 252 * 253 * @since 4.0.0 254 */ 255 protected function loadFormData() 256 { 257 // Check the session for previously entered form data. 258 $app = Factory::getApplication(); 259 $data = $app->getUserState('com_mails.edit.template.data', array()); 260 261 if (empty($data)) { 262 $data = $this->getItem(); 263 } 264 265 $this->preprocessData('com_mails.template', $data); 266 267 return $data; 268 } 269 270 /** 271 * Method to validate the form data. 272 * 273 * @param Form $form The form to validate against. 274 * @param array $data The data to validate. 275 * @param string $group The name of the field group to validate. 276 * 277 * @return array|boolean Array of filtered data if valid, false otherwise. 278 * 279 * @since 4.0.0 280 */ 281 public function validate($form, $data, $group = null) 282 { 283 $validLanguages = LanguageHelper::getContentLanguages(array(0, 1)); 284 285 if (!array_key_exists($data['language'], $validLanguages)) { 286 $this->setError(Text::_('COM_MAILS_FIELD_LANGUAGE_CODE_INVALID')); 287 288 return false; 289 } 290 291 return parent::validate($form, $data, $group); 292 } 293 294 /** 295 * Method to save the form data. 296 * 297 * @param array $data The form data. 298 * 299 * @return boolean True on success, False on error. 300 * 301 * @since 4.0.0 302 */ 303 public function save($data) 304 { 305 $table = $this->getTable(); 306 $context = $this->option . '.' . $this->name; 307 308 $key = $table->getKeyName(); 309 $template_id = (!empty($data['template_id'])) ? $data['template_id'] : $this->getState($this->getName() . '.template_id'); 310 $language = (!empty($data['language'])) ? $data['language'] : $this->getState($this->getName() . '.language'); 311 $isNew = true; 312 313 // Include the plugins for the save events. 314 \Joomla\CMS\Plugin\PluginHelper::importPlugin($this->events_map['save']); 315 316 // Allow an exception to be thrown. 317 try { 318 // Load the row if saving an existing record. 319 $table->load(array('template_id' => $template_id, 'language' => $language)); 320 321 if ($table->subject) { 322 $isNew = false; 323 } 324 325 // Load the default row 326 $table->load(array('template_id' => $template_id, 'language' => '')); 327 328 // Bind the data. 329 if (!$table->bind($data)) { 330 $this->setError($table->getError()); 331 332 return false; 333 } 334 335 // Prepare the row for saving 336 $this->prepareTable($table); 337 338 // Check the data. 339 if (!$table->check()) { 340 $this->setError($table->getError()); 341 342 return false; 343 } 344 345 // Trigger the before save event. 346 $result = Factory::getApplication()->triggerEvent($this->event_before_save, array($context, $table, $isNew, $data)); 347 348 if (in_array(false, $result, true)) { 349 $this->setError($table->getError()); 350 351 return false; 352 } 353 354 // Store the data. 355 if (!$table->store()) { 356 $this->setError($table->getError()); 357 358 return false; 359 } 360 361 // Clean the cache. 362 $this->cleanCache(); 363 364 // Trigger the after save event. 365 Factory::getApplication()->triggerEvent($this->event_after_save, array($context, $table, $isNew, $data)); 366 } catch (\Exception $e) { 367 $this->setError($e->getMessage()); 368 369 return false; 370 } 371 372 $this->setState($this->getName() . '.new', $isNew); 373 374 return true; 375 } 376 377 /** 378 * Prepare and sanitise the table data prior to saving. 379 * 380 * @param Table $table A reference to a Table object. 381 * 382 * @return void 383 * 384 * @since 4.0.0 385 */ 386 protected function prepareTable($table) 387 { 388 } 389 390 /** 391 * Stock method to auto-populate the model state. 392 * 393 * @return void 394 * 395 * @since 4.0.0 396 */ 397 protected function populateState() 398 { 399 parent::populateState(); 400 401 $template_id = Factory::getApplication()->input->getCmd('template_id'); 402 $this->setState($this->getName() . '.template_id', $template_id); 403 404 $language = Factory::getApplication()->input->getCmd('language'); 405 $this->setState($this->getName() . '.language', $language); 406 } 407 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |