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