Merge pull request #13915 from colemanw/shortCRM
[civicrm-core.git] / CRM / Contact / Form / Inline / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Form helper class for an Website object,
36 */
37 class CRM_Contact_Form_Inline_Website extends CRM_Contact_Form_Inline {
38
39 /**
40 * Websitess of the contact that is been viewed.
41 */
42 private $_websites = [];
43
44 /**
45 * No of website blocks for inline edit.
46 */
47 private $_blockCount = 6;
48
49 /**
50 * Call preprocess.
51 */
52 public function preProcess() {
53 parent::preProcess();
54
55 //get all the existing websites
56 $params = ['contact_id' => $this->_contactId];
57 $values = [];
58 $this->_websites = CRM_Core_BAO_Website::getValues($params, $values);
59 }
60
61 /**
62 * Build the form object elements for website object.
63 */
64 public function buildQuickForm() {
65 parent::buildQuickForm();
66
67 $totalBlocks = $this->_blockCount;
68 $actualBlockCount = 1;
69 if (count($this->_websites) > 1) {
70 $actualBlockCount = $totalBlocks = count($this->_websites);
71 if ($totalBlocks < $this->_blockCount) {
72 $additionalBlocks = $this->_blockCount - $totalBlocks;
73 $totalBlocks += $additionalBlocks;
74 }
75 else {
76 $actualBlockCount++;
77 $totalBlocks++;
78 }
79 }
80
81 $this->assign('actualBlockCount', $actualBlockCount);
82 $this->assign('totalBlocks', $totalBlocks);
83
84 $this->applyFilter('__ALL__', 'trim');
85
86 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
87 CRM_Contact_Form_Edit_Website::buildQuickForm($this, $blockId, TRUE);
88 }
89
90 $this->addFormRule(['CRM_Contact_Form_Inline_Website', 'formRule'], $this);
91
92 }
93
94 /**
95 * Set defaults for the form.
96 *
97 * @return array
98 */
99 public function setDefaultValues() {
100 $defaults = [];
101 if (!empty($this->_websites)) {
102 foreach ($this->_websites as $id => $value) {
103 $defaults['website'][$id] = $value;
104 }
105 }
106 else {
107 // set the default website type
108 $defaults['website'][1]['website_type_id'] = key(CRM_Core_OptionGroup::values('website_type',
109 FALSE, FALSE, FALSE, ' AND is_default = 1'
110 ));
111 }
112 return $defaults;
113 }
114
115 /**
116 * Process the form.
117 */
118 public function postProcess() {
119 $params = $this->exportValues();
120
121 foreach ($this->_websites as $count => $value) {
122 if (!empty($value['id']) && isset($params['website'][$count])) {
123 $params['website'][$count]['id'] = $value['id'];
124 }
125 }
126 // Process / save websites
127 CRM_Core_BAO_Website::process($params['website'], $this->_contactId, TRUE);
128
129 $this->log();
130 $this->response();
131 }
132
133 /**
134 * Global validation rules for the form.
135 *
136 * @param array $fields
137 * Posted values of the form.
138 * @param array $errors
139 * List of errors to be posted back to the form.
140 * @param CRM_Contact_Form_Inline_Website $form
141 *
142 * @return array
143 */
144 public static function formRule($fields, $errors, $form) {
145 $hasData = $errors = [];
146 if (!empty($fields['website']) && is_array($fields['website'])) {
147 $types = [];
148 foreach ($fields['website'] as $instance => $blockValues) {
149 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
150
151 if ($dataExists) {
152 $hasData[] = $instance;
153 if (!empty($blockValues['website_type_id'])) {
154 if (empty($types[$blockValues['website_type_id']])) {
155 $types[$blockValues['website_type_id']] = $blockValues['website_type_id'];
156 }
157 else {
158 $errors["website[" . $instance . "][website_type_id]"] = ts('Contacts may only have one website of each type at most.');
159 }
160 }
161 }
162 }
163 }
164 return $errors;
165 }
166
167 }