CIVICRM-1391 Update Smart Group form unsets the existing Group Types for the Group
[civicrm-core.git] / CRM / Contact / Form / Inline / Website.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/**
5a409b50 19 * Form helper class for an Website object,
6a488035
TO
20 */
21class CRM_Contact_Form_Inline_Website extends CRM_Contact_Form_Inline {
22
23 /**
fe482240 24 * Websitess of the contact that is been viewed.
69078420 25 * @var array
6a488035 26 */
be2fb01f 27 private $_websites = [];
6a488035
TO
28
29 /**
fe482240 30 * No of website blocks for inline edit.
69078420 31 * @var int
6a488035
TO
32 */
33 private $_blockCount = 6;
34
35 /**
fe482240 36 * Call preprocess.
6a488035
TO
37 */
38 public function preProcess() {
39 parent::preProcess();
40
41 //get all the existing websites
be2fb01f
CW
42 $params = ['contact_id' => $this->_contactId];
43 $values = [];
6a488035
TO
44 $this->_websites = CRM_Core_BAO_Website::getValues($params, $values);
45 }
46
47 /**
fe482240 48 * Build the form object elements for website object.
6a488035
TO
49 */
50 public function buildQuickForm() {
51 parent::buildQuickForm();
52
53 $totalBlocks = $this->_blockCount;
54 $actualBlockCount = 1;
55 if (count($this->_websites) > 1) {
56 $actualBlockCount = $totalBlocks = count($this->_websites);
57 if ($totalBlocks < $this->_blockCount) {
58 $additionalBlocks = $this->_blockCount - $totalBlocks;
59 $totalBlocks += $additionalBlocks;
60 }
61 else {
62 $actualBlockCount++;
63 $totalBlocks++;
64 }
65 }
66
67 $this->assign('actualBlockCount', $actualBlockCount);
68 $this->assign('totalBlocks', $totalBlocks);
69
70 $this->applyFilter('__ALL__', 'trim');
71
72 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
73 CRM_Contact_Form_Edit_Website::buildQuickForm($this, $blockId, TRUE);
74 }
75
be2fb01f 76 $this->addFormRule(['CRM_Contact_Form_Inline_Website', 'formRule'], $this);
778fd763 77
6a488035
TO
78 }
79
80 /**
fe482240 81 * Set defaults for the form.
6a488035
TO
82 *
83 * @return array
6a488035
TO
84 */
85 public function setDefaultValues() {
be2fb01f 86 $defaults = [];
6a488035
TO
87 if (!empty($this->_websites)) {
88 foreach ($this->_websites as $id => $value) {
89 $defaults['website'][$id] = $value;
90 }
91 }
dcb07045
PN
92 else {
93 // set the default website type
94 $defaults['website'][1]['website_type_id'] = key(CRM_Core_OptionGroup::values('website_type',
95 FALSE, FALSE, FALSE, ' AND is_default = 1'
96 ));
97 }
6a488035
TO
98 return $defaults;
99 }
100
101 /**
fe482240 102 * Process the form.
6a488035
TO
103 */
104 public function postProcess() {
105 $params = $this->exportValues();
106
139a60f3 107 foreach ($this->_websites as $count => $value) {
108 if (!empty($value['id']) && isset($params['website'][$count])) {
109 $params['website'][$count]['id'] = $value['id'];
110 }
111 }
6a488035 112 // Process / save websites
ebdf0a2c 113 CRM_Core_BAO_Website::process($params['website'], $this->_contactId, TRUE);
6a488035
TO
114
115 $this->log();
116 $this->response();
117 }
96025800 118
778fd763
GB
119 /**
120 * Global validation rules for the form.
121 *
122 * @param array $fields
123 * Posted values of the form.
124 * @param array $errors
125 * List of errors to be posted back to the form.
126 * @param CRM_Contact_Form_Inline_Website $form
127 *
128 * @return array
129 */
130 public static function formRule($fields, $errors, $form) {
be2fb01f 131 $hasData = $errors = [];
778fd763 132 if (!empty($fields['website']) && is_array($fields['website'])) {
be2fb01f 133 $types = [];
778fd763
GB
134 foreach ($fields['website'] as $instance => $blockValues) {
135 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
136
137 if ($dataExists) {
138 $hasData[] = $instance;
139 if (!empty($blockValues['website_type_id'])) {
140 if (empty($types[$blockValues['website_type_id']])) {
141 $types[$blockValues['website_type_id']] = $blockValues['website_type_id'];
142 }
143 else {
144 $errors["website[" . $instance . "][website_type_id]"] = ts('Contacts may only have one website of each type at most.');
145 }
146 }
147 }
148 }
149 }
150 return $errors;
151 }
152
6a488035 153}