Merge pull request #16181 from civicrm/5.21
[civicrm-core.git] / CRM / UF / Form / Preview.php
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 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class generates form components
22 * for previewing Civicrm Profile Group
23 *
24 */
25 class CRM_UF_Form_Preview extends CRM_UF_Form_AbstractPreview {
26
27 /**
28 * Pre processing work done here.
29 *
30 * gets session variables for group or field id
31 *
32 * @param
33 *
34 * @return void
35 */
36 public function preProcess() {
37 $flag = FALSE;
38 $gid = $this->get('id');
39 $this->set('gid', $gid);
40 $field = CRM_Utils_Request::retrieve('field', 'Boolean', $this, TRUE, 0);
41
42 if ($field) {
43 $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, NULL, NULL, NULL, TRUE);
44 $fieldDAO = new CRM_Core_DAO_UFField();
45 $fieldDAO->id = $this->get('fieldId');
46 $fieldDAO->find(TRUE);
47
48 if ($fieldDAO->is_active == 0) {
49 CRM_Core_Error::statusBounce(ts('This field is inactive so it will not be displayed on profile form.'));
50 }
51 elseif ($fieldDAO->is_view == 1) {
52 CRM_Core_Error::statusBounce(ts('This field is view only so it will not be displayed on profile form.'));
53 }
54 $name = $fieldDAO->field_name;
55
56 if ($fieldDAO->field_name == 'phone_and_ext') {
57 $name = 'phone';
58 }
59
60 // preview for field
61 $specialFields = [
62 'address_name',
63 'street_address',
64 'supplemental_address_1',
65 'supplemental_address_2',
66 'supplemental_address_3',
67 'city',
68 'postal_code',
69 'postal_code_suffix',
70 'geo_code_1',
71 'geo_code_2',
72 'state_province',
73 'country',
74 'county',
75 'phone',
76 'email',
77 'im',
78 ];
79
80 if ($fieldDAO->location_type_id) {
81 $name .= '-' . $fieldDAO->location_type_id;
82 }
83 elseif (in_array($name, $specialFields)) {
84 $name .= '-Primary';
85 }
86
87 if (isset($fieldDAO->phone_type_id)) {
88 $name .= '-' . $fieldDAO->phone_type_id;
89 }
90
91 $fieldArray[$name] = $fields[$name];
92
93 if ($fieldDAO->field_name == 'phone_and_ext') {
94 $phoneExtField = str_replace('phone', 'phone_ext', $name);
95 $fieldArray[$phoneExtField] = $fields[$phoneExtField];
96 }
97
98 $fields = $fieldArray;
99 if (!is_array($fields[$name])) {
100 $flag = TRUE;
101 }
102 $this->setProfile($fields, TRUE, $flag);
103 }
104 else {
105 $fields = CRM_Core_BAO_UFGroup::getFields($gid);
106 $this->setProfile($fields);
107 }
108 }
109
110 /**
111 * Build the form object.
112 *
113 * @return void
114 */
115 public function buildQuickForm() {
116 parent::buildQuickForm();
117
118 $this->addButtons([
119 [
120 'type' => 'cancel',
121 'name' => ts('Done with Preview'),
122 'isDefault' => TRUE,
123 ],
124 ]);
125 }
126
127 }