Merge pull request #21509 from JMAConsulting/core_2820
[civicrm-core.git] / CRM / Custom / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class generates form components for previewing custom data
20 *
21 * It delegates the work to lower level subclasses and integrates the changes
22 * back in. It also uses a lot of functionality with the CRM API's, so any change
23 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
24 *
25 */
26class CRM_Custom_Form_Preview extends CRM_Core_Form {
27
28 /**
fe482240 29 * The group tree data.
6a488035
TO
30 *
31 * @var array
32 */
33 protected $_groupTree;
34
35 /**
100fef9d 36 * Pre processing work done here.
6a488035
TO
37 *
38 * gets session variables for group or field id
39 *
6a488035 40 * @return void
8ef12e64 41 */
00be9182 42 public function preProcess() {
6a488035
TO
43 // get the controller vars
44 $this->_groupId = $this->get('groupId');
45 $this->_fieldId = $this->get('fieldId');
46 if ($this->_fieldId) {
47 // field preview
be2fb01f
CW
48 $defaults = [];
49 $params = ['id' => $this->_fieldId];
6a488035
TO
50 $fieldDAO = new CRM_Core_DAO_CustomField();
51 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
52
a7488080 53 if (!empty($defaults['is_view'])) {
6a488035
TO
54 CRM_Core_Error::statusBounce(ts('This field is view only so it will not display on edit form.'));
55 }
de6c59ca 56 elseif (empty($defaults['is_active'])) {
6a488035
TO
57 CRM_Core_Error::statusBounce(ts('This field is inactive so it will not display on edit form.'));
58 }
59
be2fb01f 60 $groupTree = [];
6a488035 61 $groupTree[$this->_groupId]['id'] = 0;
be2fb01f 62 $groupTree[$this->_groupId]['fields'] = [];
6a488035
TO
63 $groupTree[$this->_groupId]['fields'][$this->_fieldId] = $defaults;
64 $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this);
65 $this->assign('preview_type', 'field');
66 }
67 else {
68 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($this->_groupId);
69 $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, TRUE, $this);
70 $this->assign('preview_type', 'group');
71 }
72 }
73
74 /**
fe482240 75 * Set the default form values.
6a488035 76 *
a6c01b45
CW
77 * @return array
78 * the default array reference
6a488035 79 */
00be9182 80 public function setDefaultValues() {
be2fb01f 81 $defaults = [];
6a488035
TO
82
83 CRM_Core_BAO_CustomGroup::setDefaults($this->_groupTree, $defaults, FALSE, FALSE);
84
85 return $defaults;
86 }
87
88 /**
fe482240 89 * Build the form object.
6a488035 90 *
6a488035 91 * @return void
6a488035
TO
92 */
93 public function buildQuickForm() {
c8f8a3ad 94 if (is_array($this->_groupTree) && !empty($this->_groupTree[$this->_groupId])) {
6a488035
TO
95 foreach ($this->_groupTree[$this->_groupId]['fields'] as & $field) {
96 //add the form elements
3a7773be 97 CRM_Core_BAO_CustomField::addQuickFormElement($this, $field['element_name'], $field['id'], CRM_Utils_Array::value('is_required', $field));
6a488035
TO
98 }
99
100 $this->assign('groupTree', $this->_groupTree);
101 }
be2fb01f 102 $this->addButtons([
518fa0ee
SL
103 [
104 'type' => 'cancel',
105 'name' => ts('Done with Preview'),
106 'isDefault' => TRUE,
107 ],
108 ]);
6a488035 109 }
96025800 110
6a488035 111}