INFRA-132 - CRM/Custom - phpcbf
[civicrm-core.git] / CRM / Custom / Form / Preview.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
100fef9d 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 *
58 * @param null
59 *
60 * @return void
8ef12e64 61 */
00be9182 62 public function preProcess() {
6a488035
TO
63 // get the controller vars
64 $this->_groupId = $this->get('groupId');
65 $this->_fieldId = $this->get('fieldId');
66 if ($this->_fieldId) {
67 // field preview
68 $defaults = array();
69 $params = array('id' => $this->_fieldId);
70 $fieldDAO = new CRM_Core_DAO_CustomField();
71 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $defaults);
72
a7488080 73 if (!empty($defaults['is_view'])) {
6a488035
TO
74 CRM_Core_Error::statusBounce(ts('This field is view only so it will not display on edit form.'));
75 }
76 elseif (CRM_Utils_Array::value('is_active', $defaults) == 0) {
77 CRM_Core_Error::statusBounce(ts('This field is inactive so it will not display on edit form.'));
78 }
79
80 $groupTree = array();
81 $groupTree[$this->_groupId]['id'] = 0;
82 $groupTree[$this->_groupId]['fields'] = array();
83 $groupTree[$this->_groupId]['fields'][$this->_fieldId] = $defaults;
84 $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this);
85 $this->assign('preview_type', 'field');
86 }
87 else {
88 $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail($this->_groupId);
89 $this->_groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, TRUE, $this);
90 $this->assign('preview_type', 'group');
91 }
92 }
93
94 /**
95 * Set the default form values
96 *
97 * @param null
98 *
99 * @return array the default array reference
6a488035 100 */
00be9182 101 public function setDefaultValues() {
6a488035
TO
102 $defaults = array();
103
104 CRM_Core_BAO_CustomGroup::setDefaults($this->_groupTree, $defaults, FALSE, FALSE);
105
106 return $defaults;
107 }
108
109 /**
c490a46a 110 * Build the form object
6a488035
TO
111 *
112 * @param null
113 *
114 * @return void
6a488035
TO
115 */
116 public function buildQuickForm() {
117 if (is_array($this->_groupTree[$this->_groupId])) {
118 foreach ($this->_groupTree[$this->_groupId]['fields'] as & $field) {
119 //add the form elements
120 CRM_Core_BAO_CustomField::addQuickFormElement($this, $field['element_name'], $field['id'], FALSE, CRM_Utils_Array::value('is_required', $field));
121 }
122
123 $this->assign('groupTree', $this->_groupTree);
124 }
125 $this->addButtons(array(
126 array(
127 'type' => 'cancel',
128 'name' => ts('Done with Preview'),
129 'isDefault' => TRUE,
130 ),
131 )
132 );
133 }
134}