CRM-14106 - Regex targeting the first part of if statements
[civicrm-core.git] / CRM / Contact / Form / Inline / OpenID.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * form helper class for an OpenID object
38 */
39class CRM_Contact_Form_Inline_OpenID extends CRM_Contact_Form_Inline {
40
41 /**
42 * ims of the contact that is been viewed
43 */
44 private $_openids = array();
45
46 /**
47 * No of openid blocks for inline edit
48 */
49 private $_blockCount = 6;
50
51 /**
52 * call preprocess
53 */
54 public function preProcess() {
55 parent::preProcess();
56
57 //get all the existing openids
58 $openid = new CRM_Core_BAO_OpenID();
59 $openid->contact_id = $this->_contactId;
60
61 $this->_openids = CRM_Core_BAO_Block::retrieveBlock($openid, NULL);
62 }
63
64 /**
65 * build the form elements for openID object
66 *
67 * @return void
68 * @access public
69 */
70 public function buildQuickForm() {
71 parent::buildQuickForm();
72
73 $totalBlocks = $this->_blockCount;
74 $actualBlockCount = 1;
75 if (count($this->_openids) > 1) {
76 $actualBlockCount = $totalBlocks = count($this->_openids);
77 if ($totalBlocks < $this->_blockCount) {
78 $additionalBlocks = $this->_blockCount - $totalBlocks;
79 $totalBlocks += $additionalBlocks;
80 }
81 else {
82 $actualBlockCount++;
83 $totalBlocks++;
84 }
85 }
86
87 $this->assign('actualBlockCount', $actualBlockCount);
88 $this->assign('totalBlocks', $totalBlocks);
89
90 $this->applyFilter('__ALL__', 'trim');
91
92 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
93 CRM_Contact_Form_Edit_OpenID::buildQuickForm($this, $blockId, TRUE);
94 }
95
96 $this->addFormRule(array('CRM_Contact_Form_Inline_OpenID', 'formRule'));
97 }
98
99 /**
100 * global validation rules for the form
101 *
102 * @param array $fields posted values of the form
103 * @param array $errors list of errors to be posted back to the form
104 *
105 * @return $errors
106 * @static
107 * @access public
108 */
109 static function formRule($fields, $errors) {
110 $hasData = $hasPrimary = $errors = array();
a7488080 111 if (!empty($fields['openid']) && is_array($fields['openid'])) {
6a488035
TO
112 foreach ($fields['openid'] as $instance => $blockValues) {
113 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
114
115 if ($dataExists) {
116 $hasData[] = $instance;
a7488080 117 if (!empty($blockValues['is_primary'])) {
6a488035
TO
118 $hasPrimary[] = $instance;
119 if (!$primaryID &&
120 CRM_Utils_Array::value('openid', $blockValues)) {
121 $primaryID = $blockValues['openid'];
122 }
123 }
124 }
125 }
126
127 if (empty($hasPrimary) && !empty($hasData)) {
128 $errors["openid[1][is_primary]"] = ts('One OpenID should be marked as primary.');
129 }
130
131 if (count($hasPrimary) > 1) {
132 $errors["openid[".array_pop($hasPrimary)."][is_primary]"] = ts('Only one OpenID can be marked as primary.');
133 }
134 }
135 return $errors;
136 }
137
138 /**
139 * set defaults for the form
140 *
141 * @return array
142 * @access public
143 */
144 public function setDefaultValues() {
145 $defaults = array();
146 if (!empty($this->_openids)) {
147 foreach ($this->_openids as $id => $value) {
148 $defaults['openid'][$id] = $value;
149 }
150 }
151 else {
152 // get the default location type
153 $locationType = CRM_Core_BAO_LocationType::getDefault();
154 $defaults['openid'][1]['location_type_id'] = $locationType->id;
155 }
156 return $defaults;
157 }
158
159 /**
160 * process the form
161 *
162 * @return void
163 * @access public
164 */
165 public function postProcess() {
166 $params = $this->exportValues();
167
168 // Process / save openID
169 $params['contact_id'] = $this->_contactId;
170 $params['updateBlankLocInfo'] = TRUE;
171 CRM_Core_BAO_Block::create('openid', $params);
172
173 $this->log();
174 $this->response();
175 }
176}