PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/BAO/Location.php

https://github.com/michaelmcandrew/vaw
PHP | 387 lines | 204 code | 45 blank | 138 comment | 30 complexity | 2b765ef0bf4a3e736881e8d199a975d4 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. require_once 'CRM/Core/BAO/Phone.php';
  35. require_once 'CRM/Core/BAO/Email.php';
  36. require_once 'CRM/Core/BAO/IM.php';
  37. require_once 'CRM/Core/BAO/OpenID.php';
  38. require_once 'CRM/Core/BAO/Address.php';
  39. require_once 'CRM/Core/BAO/Block.php';
  40. /**
  41. * This class handle creation of location block elements
  42. */
  43. class CRM_Core_BAO_Location extends CRM_Core_DAO
  44. {
  45. /**
  46. * Location block element array
  47. */
  48. static $blocks = array( 'phone', 'email', 'im', 'openid', 'address' );
  49. /**
  50. * Function to create various elements of location block
  51. *
  52. * @param array $params (reference ) an assoc array of name/value pairs
  53. * @param boolean $fixAddress true if you need to fix (format) address values
  54. * before inserting in db
  55. *
  56. * @return array $location
  57. * @access public
  58. * @static
  59. */
  60. static function create( &$params, $fixAddress = true, $entity = null )
  61. {
  62. $location = array( );
  63. if ( ! self::dataExists( $params ) ) {
  64. return $location;
  65. }
  66. // create location blocks.
  67. foreach ( self::$blocks as $block ) {
  68. if ( $block != 'address' ) {
  69. eval( '$location[$block] = CRM_Core_BAO_Block::create( $block, $params, $entity );');
  70. } else {
  71. $location[$block] = CRM_Core_BAO_Address::create( $params, $fixAddress, $entity );
  72. }
  73. }
  74. if ( $entity ) {
  75. // this is a special case for adding values in location block table
  76. $entityElements = array( 'entity_table' => $params['entity_table'],
  77. 'entity_id' => $params['entity_id']);
  78. $location['id'] = self::createLocBlock ( $location, $entityElements );
  79. } else {
  80. // make sure contact should have only one primary block, CRM-5051
  81. self::checkPrimaryBlocks( CRM_Utils_Array::value( 'contact_id', $params ) );
  82. }
  83. return $location;
  84. }
  85. /**
  86. * Creates the entry in the civicrm_loc_block
  87. *
  88. */
  89. static function createLocBlock ( &$location, &$entityElements )
  90. {
  91. $locId = self::findExisting( $entityElements );
  92. $locBlock = array( );
  93. if ( $locId ) {
  94. $locBlock['id'] = $locId;
  95. }
  96. foreach( array( 'phone', 'email', 'im', 'address' ) as $loc ) {
  97. $locBlock["{$loc}_id"] = CRM_Utils_Array::value(0, $location["$loc"]) ? $location["$loc"][0]->id : null;
  98. $locBlock["{$loc}_2_id"] = CRM_Utils_Array::value(1, $location["$loc"]) ? $location["$loc"][1]->id : null;
  99. }
  100. $countNull = 0;
  101. foreach( $locBlock as $key => $block) {
  102. if ( empty($locBlock[$key] ) ) {
  103. $locBlock[$key] = 'null';
  104. $countNull++;
  105. }
  106. }
  107. if ( count($locBlock) == $countNull ) {
  108. // implies nothing is set.
  109. return null;
  110. }
  111. $locBlockInfo = self::addLocBlock( $locBlock );
  112. return $locBlockInfo->id;
  113. }
  114. /**
  115. * takes an entity array and finds the existing location block
  116. * @access public
  117. * @static
  118. */
  119. static function findExisting( $entityElements )
  120. {
  121. $eid = $entityElements['entity_id'];
  122. $etable = $entityElements['entity_table'];
  123. $query = "
  124. SELECT e.loc_block_id as locId
  125. FROM {$etable} e
  126. WHERE e.id = %1";
  127. $params = array( 1 => array( $eid, 'Integer' ) );
  128. $dao =& CRM_Core_DAO::executeQuery( $query, $params );
  129. while ( $dao->fetch( ) ) {
  130. $locBlockId = $dao->locId;
  131. }
  132. return $locBlockId;
  133. }
  134. /**
  135. * takes an associative array and adds location block
  136. *
  137. * @param array $params (reference ) an assoc array of name/value pairs
  138. *
  139. * @return object CRM_Core_BAO_locBlock object on success, null otherwise
  140. * @access public
  141. * @static
  142. */
  143. static function addLocBlock( &$params )
  144. {
  145. require_once 'CRM/Core/DAO/LocBlock.php';
  146. $locBlock = new CRM_Core_DAO_LocBlock();
  147. $locBlock->copyValues($params);
  148. return $locBlock->save( );
  149. }
  150. /**
  151. * This function deletes the Location Block
  152. *
  153. * @param int $locBlockId id of the Location Block
  154. *
  155. * @return void
  156. * @access public
  157. * @static
  158. */
  159. public static function deleteLocBlock( $locBlockId )
  160. {
  161. if ( !$locBlockId ) {
  162. return;
  163. }
  164. require_once 'CRM/Core/DAO/LocBlock.php';
  165. $locBlock = new CRM_Core_DAO_LocBlock( );
  166. $locBlock->id = $locBlockId;
  167. $locBlock->find( true );
  168. //resolve conflict of having same ids for multiple blocks
  169. $store = array(
  170. 'IM_1' => $locBlock->im_id,
  171. 'IM_2' => $locBlock->im_2_id,
  172. 'Email_1' => $locBlock->email_id,
  173. 'Email_2' => $locBlock->email_2_id,
  174. 'Phone_1' => $locBlock->phone_id,
  175. 'Phone_2' => $locBlock->phone_2_id,
  176. 'Address_1' => $locBlock->address_id,
  177. 'Address_2' => $locBlock->address_2_id
  178. );
  179. $locBlock->delete( );
  180. foreach ( $store as $daoName => $id ) {
  181. if ( $id ) {
  182. $daoName = substr( $daoName, 0, -2 );
  183. eval( '$dao = new CRM_Core_DAO_' . $daoName . '( );' );
  184. $dao->id = $id;
  185. $dao->find( true );
  186. $dao->delete( );
  187. $dao->free( );
  188. }
  189. }
  190. }
  191. /**
  192. * Check if there is data to create the object
  193. *
  194. * @param array $params (reference ) an assoc array of name/value pairs
  195. *
  196. * @return boolean
  197. * @access public
  198. * @static
  199. */
  200. static function dataExists( &$params )
  201. {
  202. // return if no data present
  203. $dataExists = false;
  204. foreach ( self::$blocks as $block ) {
  205. if ( array_key_exists( $block, $params ) ) {
  206. $dataExists = true;
  207. break;
  208. }
  209. }
  210. return $dataExists;
  211. }
  212. /**
  213. * Given the list of params in the params array, fetch the object
  214. * and store the values in the values array
  215. *
  216. * @param array $params input parameters to find object
  217. * @param array $values output values of the object
  218. *
  219. * @return array array of objects(CRM_Core_BAO_Location)
  220. * @access public
  221. * @static
  222. */
  223. static function &getValues( $entityBlock, $microformat = false )
  224. {
  225. if ( empty( $entityBlock ) ) {
  226. return null;
  227. }
  228. //get all the blocks for this contact
  229. foreach ( self::$blocks as $block ) {
  230. $name = ucfirst( $block );
  231. eval( '$blocks[$block] = CRM_Core_BAO_' . $name . '::getValues( $entityBlock, $microformat );');
  232. }
  233. return $blocks;
  234. }
  235. /**
  236. * Delete all the block associated with the location
  237. *
  238. * @param int $contactId contact id
  239. * @param int $locationTypeId id of the location to delete
  240. *
  241. * @return void
  242. * @access public
  243. * @static
  244. */
  245. static function deleteLocationBlocks( $contactId, $locationTypeId )
  246. {
  247. // ensure that contactId has a value
  248. if ( empty( $contactId ) ||
  249. ! CRM_Utils_Rule::positiveInteger( $contactId ) ) {
  250. CRM_Core_Error::fatal( );
  251. }
  252. if ( empty( $locationTypeId ) ||
  253. ! CRM_Utils_Rule::positiveInteger( $locationTypeId ) ) {
  254. // so we only delete the blocks which DO NOT have a location type Id
  255. // CRM-3581
  256. $locationTypeId = 'null';
  257. }
  258. static $blocks = array( 'Address', 'Phone', 'IM', 'OpenID', 'Email' );
  259. require_once "CRM/Core/BAO/Block.php";
  260. $params = array ( 'contact_id' => $contactId, 'location_type_id' => $locationTypeId );
  261. foreach ($blocks as $name) {
  262. CRM_Core_BAO_Block::blockDelete( $name, $params );
  263. }
  264. }
  265. /* Function to copy or update location block.
  266. *
  267. * @param int $locBlockId location block id.
  268. * @param int $updateLocBlockId update location block id
  269. * @return int newly created/updated location block id.
  270. */
  271. static function copyLocBlock( $locBlockId, $updateLocBlockId = null )
  272. {
  273. //get the location info.
  274. $defaults = $updateValues = array( );
  275. $locBlock = array( 'id' => $locBlockId );
  276. CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock', $locBlock, $defaults );
  277. if ( $updateLocBlockId ) {
  278. //get the location info for update.
  279. $copyLocationParams = array( 'id' => $updateLocBlockId );
  280. CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock', $copyLocationParams, $updateValues );
  281. foreach ( $updateValues as $key => $value) {
  282. if ( $key != 'id' ) {
  283. $copyLocationParams[$key] = 'null';
  284. }
  285. }
  286. }
  287. //copy all location blocks (email, phone, address, etc)
  288. foreach ( $defaults as $key => $value ) {
  289. if ( $key != 'id') {
  290. $tbl = explode("_", $key);
  291. $name = ucfirst( $tbl[0] );
  292. $updateParams = null;
  293. if ( $updateId = CRM_Utils_Array::value( $key, $updateValues ) ) {
  294. $updateParams = array( 'id' => $updateId );
  295. }
  296. $copy =& CRM_Core_DAO::copyGeneric( 'CRM_Core_DAO_' . $name, array( 'id' => $value ), $updateParams );
  297. $copyLocationParams[$key] = $copy->id;
  298. }
  299. }
  300. $copyLocation =& CRM_Core_DAO::copyGeneric( 'CRM_Core_DAO_LocBlock',
  301. array( 'id' => $locBlock['id'] ),
  302. $copyLocationParams );
  303. return $copyLocation->id;
  304. }
  305. /**
  306. * If contact has data for any location block, make sure
  307. * contact should have only one primary block, CRM-5051
  308. *
  309. * @param int $contactId - contact id
  310. *
  311. * @access public
  312. * @static
  313. */
  314. static function checkPrimaryBlocks( $contactId )
  315. {
  316. if ( !$contactId ) {
  317. return;
  318. }
  319. // get the loc block ids.
  320. require_once 'CRM/Contact/BAO/Contact.php';
  321. $primaryLocBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds( $contactId, array( 'is_primary' => 1 ) );
  322. $nonPrimaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds( $contactId, array( 'is_primary' => 0 ) );
  323. foreach ( array( 'Email', 'IM', 'Phone', 'Address', 'OpenID' ) as $block ) {
  324. $name = strtolower( $block );
  325. if ( array_key_exists( $name, $primaryLocBlockIds ) &&
  326. !CRM_Utils_System::isNull( $primaryLocBlockIds[$name] ) ) {
  327. if ( count( $primaryLocBlockIds[$name] ) > 1 ) {
  328. // keep only single block as primary.
  329. $primaryId = array_pop( $primaryLocBlockIds[$name] );
  330. $resetIds = "(" . implode( ',', $primaryLocBlockIds[$name] ) . ")";
  331. // reset all primary except one.
  332. CRM_Core_DAO::executeQuery( "UPDATE civicrm_$name SET is_primary = 0 WHERE id IN $resetIds" );
  333. }
  334. } else if ( array_key_exists( $name, $nonPrimaryBlockIds ) &&
  335. !CRM_Utils_System::isNull( $nonPrimaryBlockIds[$name] ) ) {
  336. // data exists and no primary block - make one primary.
  337. CRM_Core_DAO::setFieldValue( "CRM_Core_DAO_" . $block,
  338. array_pop( $nonPrimaryBlockIds[$name] ), 'is_primary', 1 );
  339. }
  340. }
  341. }
  342. }