Merge pull request #9599 from colemanw/CRM-19812
[civicrm-core.git] / CRM / Core / BAO / Website.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34/**
8eedd10a 35 * This class contain function for Website handling.
6a488035
TO
36 */
37class CRM_Core_BAO_Website extends CRM_Core_DAO_Website {
38
39 /**
fe482240 40 * Takes an associative array and adds im.
6a488035 41 *
6a0b768e
TO
42 * @param array $params
43 * (reference ) an assoc array of name/value pairs.
6a488035 44 *
a6c01b45
CW
45 * @return object
46 * CRM_Core_BAO_Website object on success, null otherwise
6a488035 47 */
00be9182 48 public static function add(&$params) {
6a488035
TO
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 /**
fe482240 61 * Process website.
6a488035 62 *
6a0b768e 63 * @param array $params
6a0b768e
TO
64 * @param int $contactID
65 * Contact id.
77b97be7 66 *
8eedd10a 67 * @param bool $skipDelete
60fd90d8 68 *
8eedd10a 69 * @return bool
6a488035 70 */
60fd90d8
DG
71 public static function create(&$params, $contactID, $skipDelete) {
72 if (empty($params)) {
6a488035
TO
73 return FALSE;
74 }
cbb7c7e0 75
60fd90d8
DG
76 $ids = self::allWebsites($contactID);
77 foreach ($params as $key => $values) {
139a60f3 78 if (empty($values['id']) && is_array($ids) && !empty($ids)) {
60fd90d8 79 foreach ($ids as $id => $value) {
139a60f3 80 if (($value['website_type_id'] == $values['website_type_id'])) {
60fd90d8 81 $values['id'] = $id;
6a488035
TO
82 }
83 }
84 }
60fd90d8 85 if (!empty($values['url'])) {
139a60f3 86 $values['contact_id'] = $contactID;
60fd90d8 87 self::add($values);
6a488035 88 }
139a60f3 89 elseif ($skipDelete && !empty($values['id'])) {
90 self::del(array($values['id']));
91 }
6a488035
TO
92 }
93 }
94
95 /**
fe482240 96 * Delete website.
6a488035 97 *
6a0b768e
TO
98 * @param array $ids
99 * Website ids.
6a488035 100 *
8eedd10a 101 * @return bool
6a488035 102 */
00be9182 103 public static function del($ids) {
6a488035
TO
104 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
105 CRM_Core_DAO::executeQuery($query);
a65e2e55
CW
106 // FIXME: we should return false if the del was unsuccessful
107 return TRUE;
6a488035
TO
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 *
c490a46a 114 * @param array $params
dd244018
EM
115 * @param $values
116 *
317fceb4 117 * @return bool
6a488035 118 */
00be9182 119 public static function &getValues(&$params, &$values) {
353ffa53
TO
120 $websites = array();
121 $website = new CRM_Core_DAO_Website();
6a488035
TO
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 /**
fe482240 138 * Get all the websites for a specified contact_id.
6a488035 139 *
6a0b768e
TO
140 * @param int $id
141 * The contact id.
6a488035 142 *
da6b46f4
EM
143 * @param bool $updateBlankLocInfo
144 *
a6c01b45
CW
145 * @return array
146 * the array of website details
6a488035 147 */
00be9182 148 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
149 if (!$id) {
150 return NULL;
151 }
152
153 $query = '
154SELECT 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();
353ffa53
TO
160 $dao = CRM_Core_DAO::executeQuery($query, $params);
161 $count = 1;
6a488035
TO
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 }
96025800 177
6a488035 178}