Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle 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 Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Class account_gateway
  19   *
  20   * @package     core_payment
  21   * @copyright   2020 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_payment\form;
  26  
  27  use core\form\persistent;
  28  
  29  /**
  30   * Class account_gateway
  31   *
  32   * @package     core_payment
  33   * @copyright   2020 Marina Glancy
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class account_gateway extends persistent {
  37  
  38      /** @var string The persistent class. */
  39      protected static $persistentclass = \core_payment\account_gateway::class;
  40  
  41      protected static $fieldstoremove = ['accountname', 'gatewayname', 'submitbutton'];
  42  
  43      /**
  44       * Define the form - called by parent constructor
  45       */
  46      public function definition() {
  47          $mform = $this->_form;
  48  
  49          $mform->addElement('hidden', 'id');
  50          $mform->addElement('hidden', 'accountid');
  51          $mform->addElement('hidden', 'gateway');
  52  
  53          $mform->addElement('static', 'accountname', get_string('accountname', 'payment'),
  54              $this->get_gateway_persistent()->get_account()->get_formatted_name());
  55  
  56          $mform->addElement('static', 'gatewayname', get_string('type_paygw', 'plugin'),
  57              $this->get_gateway_persistent()->get_display_name());
  58  
  59          $mform->addElement('advcheckbox', 'enabled', get_string('enable'));
  60  
  61          /** @var \core_payment\gateway $classname */
  62          $classname = '\paygw_' . $this->get_gateway_persistent()->get('gateway') . '\gateway';
  63          if (class_exists($classname)) {
  64              $classname::add_configuration_to_gateway_form($this);
  65          }
  66  
  67          $this->add_action_buttons();
  68      }
  69  
  70      /**
  71       * Form validation
  72       *
  73       * @param \stdClass $data
  74       * @param array $files
  75       * @param array $errors
  76       */
  77      protected function extra_validation($data, $files, array &$errors) {
  78          /** @var \core_payment\gateway $classname */
  79          $classname = '\paygw_' . $this->get_gateway_persistent()->get('gateway') . '\gateway';
  80          if (class_exists($classname)) {
  81              $classname::validate_gateway_form($this, $data, $files, $errors);
  82          }
  83      }
  84  
  85      /**
  86       * Exposes the protected attribute to be accessed by the \core_payment\gateway callback
  87       *
  88       * @return \MoodleQuickForm
  89       */
  90      public function get_mform(): \MoodleQuickForm {
  91          return $this->_form;
  92      }
  93  
  94      /**
  95       * Exposes the protected attribute to be accessed by the \core_payment\gateway callback
  96       *
  97       * @return \core_payment\account_gateway
  98       */
  99      public function get_gateway_persistent(): \core_payment\account_gateway {
 100          return $this->get_persistent();
 101      }
 102  
 103      /**
 104       * Filter out the foreign fields of the persistent.
 105       *
 106       * This can be overridden to filter out more complex fields.
 107       *
 108       * @param \stdClass $data The data to filter the fields out of.
 109       * @return \stdClass.
 110       */
 111      protected function filter_data_for_persistent($data) {
 112          $data = parent::filter_data_for_persistent($data);
 113          return (object) array_intersect_key((array)$data, \core_payment\account_gateway::properties_definition());
 114      }
 115  
 116      /**
 117       * Overwrite parent method to json encode config
 118       *
 119       * @return object|\stdClass|null
 120       * @throws \coding_exception
 121       */
 122      public function get_data() {
 123          if (!$data = parent::get_data()) {
 124              return $data;
 125          }
 126          // Everything that is not a property of the account_gateway class is a gateway config.
 127          $data = (array)$data;
 128          $properties = \core_payment\account_gateway::properties_definition() + ['id' => 1];
 129          $config = array_diff_key($data, $properties, ['timemodified' => 1, 'timecreated' => 1]);
 130          $data = array_intersect_key($data, $properties);
 131          $data['config'] = json_encode($config);
 132          return (object)$data;
 133      }
 134  
 135      /**
 136       * Overwrite parent method to json decode config
 137       *
 138       * @param array|\stdClass $values
 139       */
 140      public function set_data($values) {
 141          if (($config = isset($values->config) ? @json_decode($values->config, true) : null) && is_array($config)) {
 142              $values = (object)((array)$values + $config);
 143          }
 144          unset($values->config);
 145          parent::set_data($values);
 146      }
 147  }