Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-11-23-22-46-27
[civicrm-core.git] / CRM / Core / BAO / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class contain function for Website handling.
36 */
37 class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
38
39 /**
40 * Takes an associative array and adds im.
41 *
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
44 *
45 * @return object
46 * CRM_Core_BAO_Website object on success, null otherwise
47 */
48 public static function add(&$params) {
49 $hook = empty($params['id']) ? 'create' : 'edit';
50 CRM_Utils_Hook::pre($hook, 'Website', CRM_Utils_Array::value('id', $params), $params);
51
52 $website = new CRM_Core_DAO_Website();
53 $website->copyValues($params);
54 $website->save();
55
56 CRM_Utils_Hook::post($hook, 'Website', $website->id, $website);
57 return $website;
58 }
59
60 /**
61 * Process website.
62 *
63 * @param array $params
64 * @param int $contactID
65 * Contact id.
66 *
67 * @param bool $skipDelete
68 *
69 * @return bool
70 */
71 public static function create(&$params, $contactID, $skipDelete) {
72 if (empty($params)) {
73 return FALSE;
74 }
75
76 $ids = self::allWebsites($contactID);
77 foreach ($params as $key => $values) {
78 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
79 foreach ($ids as $id => $value) {
80 if (($value['website_type_id'] == $values['website_type_id'])) {
81 $values['id'] = $id;
82 }
83 }
84 }
85 if (!empty($values['url'])) {
86 $values['contact_id'] = $contactID;
87 self::add($values);
88 }
89 elseif ($skipDelete && !empty($values['id'])) {
90 self::del(array($values['id']));
91 }
92 }
93 }
94
95 /**
96 * Delete website.
97 *
98 * @param array $ids
99 * Website ids.
100 *
101 * @return bool
102 */
103 public static function del($ids) {
104 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
105 CRM_Core_DAO::executeQuery($query);
106 // FIXME: we should return false if the del was unsuccessful
107 return TRUE;
108 }
109
110 /**
111 * Given the list of params in the params array, fetch the object
112 * and store the values in the values array
113 *
114 * @param array $params
115 * @param $values
116 *
117 * @return bool
118 */
119 public static function &getValues(&$params, &$values) {
120 $websites = array();
121 $website = new CRM_Core_DAO_Website();
122 $website->contact_id = $params['contact_id'];
123 $website->find();
124
125 $count = 1;
126 while ($website->fetch()) {
127 $values['website'][$count] = array();
128 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
129
130 $websites[$count] = $values['website'][$count];
131 $count++;
132 }
133
134 return $websites;
135 }
136
137 /**
138 * Get all the websites for a specified contact_id.
139 *
140 * @param int $id
141 * The contact id.
142 *
143 * @param bool $updateBlankLocInfo
144 *
145 * @return array
146 * the array of website details
147 */
148 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
149 if (!$id) {
150 return NULL;
151 }
152
153 $query = '
154 SELECT id, website_type_id
155 FROM civicrm_website
156 WHERE civicrm_website.contact_id = %1';
157 $params = array(1 => array($id, 'Integer'));
158
159 $websites = $values = array();
160 $dao = CRM_Core_DAO::executeQuery($query, $params);
161 $count = 1;
162 while ($dao->fetch()) {
163 $values = array(
164 'id' => $dao->id,
165 'website_type_id' => $dao->website_type_id,
166 );
167
168 if ($updateBlankLocInfo) {
169 $websites[$count++] = $values;
170 }
171 else {
172 $websites[$dao->id] = $values;
173 }
174 }
175 return $websites;
176 }
177
178 }