PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/civicrm/CRM/Campaign/Form/SurveyType.php

https://github.com/nysenate/Bluebird-CRM
PHP | 145 lines | 74 code | 25 blank | 46 comment | 15 complexity | 61870d6dd2a1da4983f5b1e5ab85c3c6 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, MPL-2.0-no-copyleft-exception, AGPL-1.0, GPL-2.0, AGPL-3.0, Apache-2.0, MIT, GPL-3.0, CC-BY-4.0, LGPL-2.1, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | Copyright CiviCRM LLC. All rights reserved. |
  5. | |
  6. | This work is published under the GNU AGPLv3 license with some |
  7. | permitted exceptions and without any warranty. For full license |
  8. | and copyright information, see https://civicrm.org/licensing |
  9. +--------------------------------------------------------------------+
  10. */
  11. /**
  12. *
  13. * @package CRM
  14. * @copyright CiviCRM LLC https://civicrm.org/licensing
  15. */
  16. /**
  17. * This class generates form components for Option Group.
  18. */
  19. class CRM_Campaign_Form_SurveyType extends CRM_Admin_Form {
  20. protected $_gid;
  21. /**
  22. * The option group name
  23. *
  24. * @var string
  25. */
  26. protected $_gName;
  27. /**
  28. * Action
  29. *
  30. * @var int
  31. */
  32. public $_action;
  33. /**
  34. * Set variables up before form is built.
  35. *
  36. * @throws \CRM_Core_Exception
  37. */
  38. public function preProcess() {
  39. $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
  40. if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
  41. $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
  42. $this->assign('id', $this->_id);
  43. }
  44. $this->assign('action', $this->_action);
  45. $this->assign('id', $this->_id);
  46. $this->_BAOName = 'CRM_Core_BAO_OptionValue';
  47. $this->_gName = 'activity_type';
  48. $this->_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gName, 'id', 'name');
  49. $session = CRM_Core_Session::singleton();
  50. $url = CRM_Utils_System::url('civicrm/admin/campaign/surveyType', 'reset=1');
  51. $session->pushUserContext($url);
  52. if ($this->_id && in_array($this->_gName, CRM_Core_OptionGroup::$_domainIDGroups)) {
  53. $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'domain_id', 'id');
  54. if (CRM_Core_Config::domainID() != $domainID) {
  55. CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
  56. }
  57. }
  58. }
  59. /**
  60. * Set default values for the form.
  61. * the default values are retrieved from the database.
  62. *
  63. * @return array
  64. * array of default values
  65. */
  66. public function setDefaultValues() {
  67. $defaults = parent::setDefaultValues();
  68. if (!isset($defaults['weight']) || !$defaults['weight']) {
  69. $fieldValues = ['option_group_id' => $this->_gid];
  70. $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
  71. }
  72. return $defaults;
  73. }
  74. /**
  75. * Build the form object.
  76. */
  77. public function buildQuickForm() {
  78. parent::buildQuickForm();
  79. if ($this->_action & CRM_Core_Action::DELETE) {
  80. return;
  81. }
  82. $this->applyFilter('__ALL__', 'trim');
  83. $this->add('text', 'label', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
  84. $this->add('wysiwyg', 'description',
  85. ts('Description'),
  86. CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description')
  87. );
  88. $this->add('checkbox', 'is_active', ts('Enabled?'));
  89. if ($this->_action == CRM_Core_Action::UPDATE &&
  90. CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_id, 'is_reserved')
  91. ) {
  92. $this->freeze(['label', 'is_active']);
  93. }
  94. $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
  95. $this->assign('id', $this->_id);
  96. }
  97. /**
  98. * Process the form submission.
  99. */
  100. public function postProcess() {
  101. if ($this->_action & CRM_Core_Action::DELETE) {
  102. $fieldValues = ['option_group_id' => $this->_gid];
  103. $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues);
  104. if (CRM_Core_BAO_OptionValue::del($this->_id)) {
  105. CRM_Core_Session::setStatus(ts('Selected Survey type has been deleted.'), ts('Record Deleted'), 'success');
  106. }
  107. }
  108. else {
  109. $params = $ids = [];
  110. $params = $this->exportValues();
  111. // set db value of filter in params if filter is non editable
  112. if ($this->_id && !array_key_exists('filter', $params)) {
  113. $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id');
  114. }
  115. $params['component_id'] = CRM_Core_Component::getComponentID('CiviCampaign');
  116. $optionValue = CRM_Core_OptionValue::addOptionValue($params, $this->_gName, $this->_action, $this->_id);
  117. CRM_Core_Session::setStatus(ts('The Survey type \'%1\' has been saved.', [1 => $optionValue->label]), ts('Saved'), 'success');
  118. }
  119. }
  120. }