Merge pull request #11618 from jitendrapurohit/CRM-21730
[civicrm-core.git] / CRM / Core / BAO / Website.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
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 76 foreach ($params as $key => $values) {
60fd90d8 77 if (!empty($values['url'])) {
139a60f3 78 $values['contact_id'] = $contactID;
60fd90d8 79 self::add($values);
6a488035 80 }
139a60f3 81 elseif ($skipDelete && !empty($values['id'])) {
82 self::del(array($values['id']));
83 }
6a488035
TO
84 }
85 }
86
87 /**
fe482240 88 * Delete website.
6a488035 89 *
6a0b768e
TO
90 * @param array $ids
91 * Website ids.
6a488035 92 *
8eedd10a 93 * @return bool
6a488035 94 */
00be9182 95 public static function del($ids) {
6a488035
TO
96 $query = 'DELETE FROM civicrm_website WHERE id IN ( ' . implode(',', $ids) . ')';
97 CRM_Core_DAO::executeQuery($query);
a65e2e55
CW
98 // FIXME: we should return false if the del was unsuccessful
99 return TRUE;
6a488035
TO
100 }
101
102 /**
103 * Given the list of params in the params array, fetch the object
104 * and store the values in the values array
105 *
c490a46a 106 * @param array $params
dd244018
EM
107 * @param $values
108 *
317fceb4 109 * @return bool
6a488035 110 */
00be9182 111 public static function &getValues(&$params, &$values) {
353ffa53
TO
112 $websites = array();
113 $website = new CRM_Core_DAO_Website();
6a488035
TO
114 $website->contact_id = $params['contact_id'];
115 $website->find();
116
117 $count = 1;
118 while ($website->fetch()) {
119 $values['website'][$count] = array();
120 CRM_Core_DAO::storeValues($website, $values['website'][$count]);
121
122 $websites[$count] = $values['website'][$count];
123 $count++;
124 }
125
126 return $websites;
127 }
128
129 /**
fe482240 130 * Get all the websites for a specified contact_id.
6a488035 131 *
6a0b768e
TO
132 * @param int $id
133 * The contact id.
6a488035 134 *
da6b46f4
EM
135 * @param bool $updateBlankLocInfo
136 *
a6c01b45
CW
137 * @return array
138 * the array of website details
6a488035 139 */
00be9182 140 public static function allWebsites($id, $updateBlankLocInfo = FALSE) {
6a488035
TO
141 if (!$id) {
142 return NULL;
143 }
144
145 $query = '
146SELECT id, website_type_id
147 FROM civicrm_website
148 WHERE civicrm_website.contact_id = %1';
149 $params = array(1 => array($id, 'Integer'));
150
151 $websites = $values = array();
353ffa53
TO
152 $dao = CRM_Core_DAO::executeQuery($query, $params);
153 $count = 1;
6a488035
TO
154 while ($dao->fetch()) {
155 $values = array(
156 'id' => $dao->id,
157 'website_type_id' => $dao->website_type_id,
158 );
159
160 if ($updateBlankLocInfo) {
161 $websites[$count++] = $values;
162 }
163 else {
164 $websites[$dao->id] = $values;
165 }
166 }
167 return $websites;
168 }
96025800 169
6a488035 170}