| 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 | class CRM_Core_BAO_LocationType extends CRM_Core_DAO_LocationType { |
| 36 | |
| 37 | /** |
| 38 | * static holder for the default LT |
| 39 | */ |
| 40 | static $_defaultLocationType = NULL; |
| 41 | static $_billingLocationType = NULL; |
| 42 | |
| 43 | /** |
| 44 | * class constructor |
| 45 | */ |
| 46 | function __construct() { |
| 47 | parent::__construct(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Takes a bunch of params that are needed to match certain criteria and |
| 52 | * retrieves the relevant objects. Typically the valid params are only |
| 53 | * contact_id. We'll tweak this function to be more full featured over a period |
| 54 | * of time. This is the inverse function of create. It also stores all the retrieved |
| 55 | * values in the default array |
| 56 | * |
| 57 | * @param array $params (reference ) an assoc array of name/value pairs |
| 58 | * @param array $defaults (reference ) an assoc array to hold the flattened values |
| 59 | * |
| 60 | * @return object CRM_Core_BAO_LocaationType object on success, null otherwise |
| 61 | * @access public |
| 62 | * @static |
| 63 | */ |
| 64 | static function retrieve(&$params, &$defaults) { |
| 65 | $locationType = new CRM_Core_DAO_LocationType(); |
| 66 | $locationType->copyValues($params); |
| 67 | if ($locationType->find(TRUE)) { |
| 68 | CRM_Core_DAO::storeValues($locationType, $defaults); |
| 69 | return $locationType; |
| 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * update the is_active flag in the db |
| 76 | * |
| 77 | * @param int $id id of the database record |
| 78 | * @param boolean $is_active value we want to set the is_active field |
| 79 | * |
| 80 | * @return Object DAO object on sucess, null otherwise |
| 81 | * |
| 82 | * @access public |
| 83 | * @static |
| 84 | */ |
| 85 | static function setIsActive($id, $is_active) { |
| 86 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_LocationType', $id, 'is_active', $is_active); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * retrieve the default location_type |
| 91 | * |
| 92 | * @param NULL |
| 93 | * |
| 94 | * @return object The default location type object on success, |
| 95 | * null otherwise |
| 96 | * @static |
| 97 | * @access public |
| 98 | */ |
| 99 | static function &getDefault() { |
| 100 | if (self::$_defaultLocationType == NULL) { |
| 101 | $params = array('is_default' => 1); |
| 102 | $defaults = array(); |
| 103 | self::$_defaultLocationType = self::retrieve($params, $defaults); |
| 104 | } |
| 105 | return self::$_defaultLocationType; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Get ID of billing location type |
| 110 | * @return integer |
| 111 | */ |
| 112 | /** |
| 113 | * @return mixed|null |
| 114 | */ |
| 115 | static function getBilling() { |
| 116 | if (self::$_billingLocationType == NULL) { |
| 117 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
| 118 | self::$_billingLocationType = array_search('Billing', $locationTypes); |
| 119 | } |
| 120 | return self::$_billingLocationType; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Function to add a Location Type |
| 125 | * |
| 126 | * @param array $params reference array contains the values submitted by the form |
| 127 | * |
| 128 | * @internal param array $ids reference array contains the id |
| 129 | * |
| 130 | * @access public |
| 131 | * @static |
| 132 | * |
| 133 | * @return object |
| 134 | */ |
| 135 | static function create(&$params) { |
| 136 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 137 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); |
| 138 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); |
| 139 | |
| 140 | // action is taken depending upon the mode |
| 141 | $locationType = new CRM_Core_DAO_LocationType(); |
| 142 | $locationType->copyValues($params); |
| 143 | |
| 144 | if ($params['is_default']) { |
| 145 | $query = "UPDATE civicrm_location_type SET is_default = 0"; |
| 146 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 147 | } |
| 148 | |
| 149 | $locationType->save(); |
| 150 | return $locationType; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Function to delete location Types |
| 155 | * |
| 156 | * @param int $locationTypeId ID of the location type to be deleted. |
| 157 | * |
| 158 | * @access public |
| 159 | * @static |
| 160 | */ |
| 161 | static function del($locationTypeId) { |
| 162 | $entity = array('address', 'phone', 'email', 'im'); |
| 163 | //check dependencies |
| 164 | foreach ($entity as $key) { |
| 165 | if ($key == 'im') { |
| 166 | $name = strtoupper($key); |
| 167 | } |
| 168 | else { |
| 169 | $name = ucfirst($key); |
| 170 | } |
| 171 | $baoString = 'CRM_Core_BAO_' . $name; |
| 172 | $object = new $baoString(); |
| 173 | $object->location_type_id = $locationTypeId; |
| 174 | $object->delete(); |
| 175 | } |
| 176 | |
| 177 | $locationType = new CRM_Core_DAO_LocationType(); |
| 178 | $locationType->id = $locationTypeId; |
| 179 | $locationType->delete(); |
| 180 | } |
| 181 | } |
| 182 | |