Update copyright date for 2020
[civicrm-core.git] / CRM / Custom / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for previewing custom data
38 *
39 * It delegates the work to lower level subclasses and integrates the changes
40 * back in. It also uses a lot of functionality with the CRM API's, so any change
41 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
42 *
43 */
44class CRM_Custom_Form_Preview extends CRM_Core_Form {
45
46 /**
fe482240 47 * The group tree data.
6a488035
TO
48 *
49 * @var array
50 */
51 protected $_groupTree;
52
53 /**
100fef9d 54 * Pre processing work done here.
6a488035
TO
55 *
56 * gets session variables for group or field id
57 *
6a488035 58 * @return void
8ef12e64 59 */
00be9182 60 public function preProcess() {
6a488035
TO
61 // get the controller vars
62 $this->_groupId = $this->get('groupId');
63 $this->_fieldId = $this->get('fieldId');
64 if ($this->_fieldId) {
65 // field preview
be2fb01f
CW
66 $defaults = [];
67 $params = ['id' => $this->_fieldId];
6a488035
TO
68 $fieldDAO = new CRM_Core_DAO_CustomField();
69 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
70
a7488080 71 if (!empty($defaults['is_view'])) {
6a488035
TO
72 CRM_Core_Error::statusBounce(ts('This field is view only so it will not display on edit form.'));
73 }
de6c59ca 74 elseif (empty($defaults['is_active'])) {
6a488035
TO
75 CRM_Core_Error::statusBounce(ts('This field is inactive so it will not display on edit form.'));
76 }
77
be2fb01f 78 $groupTree = [];
6a488035 79 $groupTree[$this->_groupId]['id'] = 0;
be2fb01f 80 $groupTree[$this->_groupId]['fields'] = [];
6a488035
TO
81 $groupTree[$this->_groupId]['fields'][$this->_fieldId] = $defaults;
82 $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this);
83 $this->assign('preview_type', 'field');
84 }
85 else {
86 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($this->_groupId);
87 $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, TRUE, $this);
88 $this->assign('preview_type', 'group');
89 }
90 }
91
92 /**
fe482240 93 * Set the default form values.
6a488035 94 *
a6c01b45
CW
95 * @return array
96 * the default array reference
6a488035 97 */
00be9182 98 public function setDefaultValues() {
be2fb01f 99 $defaults = [];
6a488035
TO
100
101 CRM_Core_BAO_CustomGroup::setDefaults($this->_groupTree, $defaults, FALSE, FALSE);
102
103 return $defaults;
104 }
105
106 /**
fe482240 107 * Build the form object.
6a488035 108 *
6a488035 109 * @return void
6a488035
TO
110 */
111 public function buildQuickForm() {
c8f8a3ad 112 if (is_array($this->_groupTree) && !empty($this->_groupTree[$this->_groupId])) {
6a488035
TO
113 foreach ($this->_groupTree[$this->_groupId]['fields'] as & $field) {
114 //add the form elements
3a7773be 115 CRM_Core_BAO_CustomField::addQuickFormElement($this, $field['element_name'], $field['id'], CRM_Utils_Array::value('is_required', $field));
6a488035
TO
116 }
117
118 $this->assign('groupTree', $this->_groupTree);
119 }
be2fb01f 120 $this->addButtons([
518fa0ee
SL
121 [
122 'type' => 'cancel',
123 'name' => ts('Done with Preview'),
124 'isDefault' => TRUE,
125 ],
126 ]);
6a488035 127 }
96025800 128
6a488035 129}