SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/Admin/EmailTextAdminController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers\Admin;
  15.  
  16. use Sprout\Helpers\EmailText;
  17. use Sprout\Helpers\Pdb;
  18. use Sprout\Helpers\Validator;
  19.  
  20.  
  21. /**
  22. * Handles most processing for Email text
  23. **/
  24. class EmailTextAdminController extends ManagedAdminController
  25. {
  26. protected $controller_name = 'email_text';
  27. protected $friendly_name = 'Email text';
  28. protected $add_defaults = array(
  29. 'active' => 1,
  30. );
  31. protected $main_delete = false;
  32.  
  33.  
  34. public function _getNavigation()
  35. {
  36. return null;
  37. }
  38.  
  39. public function _getTools()
  40. {
  41. return null;
  42. }
  43.  
  44. public function _getContents()
  45. {
  46. return null;
  47. }
  48.  
  49. public function _getAddForm()
  50. {
  51. return '<p><i>You cannot add these manually.</i></p>';
  52. }
  53.  
  54. public function _addSave(&$item_id)
  55. {
  56. return false;
  57. }
  58.  
  59.  
  60. /**
  61.   * Pre-render hook for editing
  62.   **/
  63. protected function _editPreRender($view, $item_id)
  64. {
  65. parent::_editPreRender($view, $item_id);
  66.  
  67. $view->field_defs = EmailText::getFieldDefs($view->data['name']);
  68. }
  69.  
  70.  
  71. /**
  72.   * Return the sub-actions for editing; for spec {@see AdminController::renderSubActions}
  73.   * @return array
  74.   */
  75. public function _getEditSubActions($item_id)
  76. {
  77. $actions = parent::_getEditSubActions($item_id);
  78. // Add your actions here, like this: $actions[] = [ ... ];
  79. return $actions;
  80. }
  81.  
  82.  
  83. /**
  84.   * Saves the provided POST data the specified record
  85.   *
  86.   * @param int $code The record to update
  87.   * @param bool True on success, false on failure
  88.   **/
  89. public function _editSave($id)
  90. {
  91. $id = (int) $id;
  92.  
  93. $_SESSION['admin']['field_values'] = Validator::trim($_POST);
  94.  
  95. // Validate form fields
  96. $valid = new Validator($_POST);
  97. $valid->required(['text']);
  98. $valid->check('text', 'Validity::length', 0, 5000);
  99. if ($valid->hasErrors()) {
  100. $_SESSION['admin']['field_errors'] = $valid->getFieldErrors();
  101. $valid->createNotifications();
  102. return false;
  103. }
  104.  
  105. // Prep changes
  106. $update_fields = array();
  107. $update_fields['text'] = $_POST['text'];
  108. $update_fields['date_modified'] = Pdb::now();
  109.  
  110. Pdb::transact();
  111.  
  112. $logdata = $this->loadRecord($this->table_name, $id);
  113.  
  114. Pdb::update($this->table_name, $update_fields, ['id' => $id]);
  115.  
  116. $this->logEdit($this->table_name, $id, $logdata);
  117.  
  118. Pdb::commit();
  119.  
  120. return true;
  121. }
  122.  
  123. }
  124.  
  125.  
  126.