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

https://github.com/michaelmcandrew/citylink · PHP · 342 lines · 186 code · 37 blank · 119 comment · 40 complexity · 1ad197c956ad687be13dda726e337a3f MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 2.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. * add static functions to include some common functionality
  33. * used across location sub object BAO classes
  34. *
  35. */
  36. class CRM_Core_BAO_Block
  37. {
  38. /**
  39. * Fields that are required for a valid block
  40. */
  41. static $requiredBlockFields = array ( 'email' => array( 'email' ),
  42. 'phone' => array( 'phone' ),
  43. 'im' => array( 'name' ),
  44. 'openid' => array( 'openid' )
  45. );
  46. /**
  47. * Given the list of params in the params array, fetch the object
  48. * and store the values in the values array
  49. *
  50. * @param Object $block typically a Phone|Email|IM|OpenID object
  51. * @param string $blockName name of the above object
  52. * @param array $params input parameters to find object
  53. * @param array $values output values of the object
  54. *
  55. * @return array of $block objects.
  56. * @access public
  57. * @static
  58. */
  59. static function &getValues( $blockName, $params )
  60. {
  61. eval ('$block = & new CRM_Core_BAO_' . $blockName .'( );');
  62. $blocks = array( );
  63. if ( ! isset( $params['entity_table'] ) ) {
  64. $block->contact_id = $params['contact_id'];
  65. if ( ! $block->contact_id ) {
  66. CRM_Core_Error::fatal( );
  67. }
  68. $blocks = self::retrieveBlock( $block, $blockName );
  69. } else {
  70. $blockIds = self::getBlockIds( $blockName, null, $params );
  71. if ( empty($blockIds)) {
  72. return $blocks;
  73. }
  74. $count = 1;
  75. foreach( $blockIds[1] as $blockId ) {
  76. eval ('$block = & new CRM_Core_BAO_' . $blockName .'( );');
  77. $block->id = $blockId['id'];
  78. $getBlocks = self::retrieveBlock( $block, $blockName );
  79. $blocks[$block->location_type_id][$count] = $getBlocks[$block->location_type_id][1];
  80. $count++;
  81. }
  82. }
  83. return $blocks;
  84. }
  85. /**
  86. * Given the list of params in the params array, fetch the object
  87. * and store the values in the values array
  88. *
  89. * @param Object $block typically a Phone|Email|IM|OpenID object
  90. * @param string $blockName name of the above object
  91. * @param array $values output values of the object
  92. *
  93. * @return array of $block objects.
  94. * @access public
  95. * @static
  96. */
  97. static function retrieveBlock( &$block, $blockName )
  98. {
  99. // we first get the primary location due to the order by clause
  100. $block->orderBy( 'is_primary desc, location_type_id desc, id asc' );
  101. $block->find( );
  102. $locationTypes = array( );
  103. $blocks =array( );
  104. $count = 1;
  105. while ( $block->fetch( ) ) {
  106. $values = array( );
  107. CRM_Core_DAO::storeValues( $block, $values );
  108. //logic to check when we should increment counter
  109. if ( !empty( $locationTypes ) ) {
  110. if ( array_key_exists ( $block->location_type_id, $locationTypes ) ) {
  111. $count = $locationTypes[$block->location_type_id];
  112. $count++;
  113. $locationTypes[$block->location_type_id] = $count;
  114. } else {
  115. $locationTypes[$block->location_type_id] = 1;
  116. $count = 1;
  117. }
  118. } else {
  119. $locationTypes[$block->location_type_id] = 1;
  120. $count = 1;
  121. }
  122. $blocks[$block->location_type_id][$count] = $values;
  123. }
  124. return $blocks ;
  125. }
  126. /**
  127. * check if the current block object has any valid data
  128. *
  129. * @param array $blockFields array of fields that are of interest for this object
  130. * @param array $params associated array of submitted fields
  131. *
  132. * @return boolean true if the block has data, otherwise false
  133. * @access public
  134. * @static
  135. */
  136. static function dataExists( $blockFields, &$params )
  137. {
  138. foreach ( $blockFields as $field ) {
  139. if ( empty( $params[$field] ) ) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. /**
  146. * check if the current block exits
  147. *
  148. * @param string $blockName bloack name
  149. * @param array $params associated array of submitted fields
  150. *
  151. * @return boolean true if the block exits, otherwise false
  152. * @access public
  153. * @static
  154. */
  155. static function blockExists( $blockName, &$params )
  156. {
  157. // return if no data present
  158. if ( ! array_key_exists( $blockName, $params ) ) {
  159. return false;
  160. }
  161. return true;
  162. }
  163. /**
  164. * Function to get all block ids for a contact
  165. *
  166. * @param string $blockName block name
  167. * @param int $contactId contact id
  168. *
  169. * @return array $contactBlockIds formatted array of block ids
  170. *
  171. * @access public
  172. * @static
  173. */
  174. static function getBlockIds ( $blockName, $contactId = null, $entityElements = null )
  175. {
  176. $contactBlockIds = $allBlocks = array( );
  177. $name = ucfirst( $blockName );
  178. if ( $contactId ) {
  179. eval ( '$allBlocks = CRM_Core_BAO_' . $name . '::all' . $name . 's( $contactId );');
  180. } else if ( !empty($entityElements) && $blockName != 'openid' ) {
  181. eval ( '$allBlocks = CRM_Core_BAO_' . $name . '::allEntity' . $name . 's( $entityElements );');
  182. }
  183. $locationCount = 1;
  184. $blockCount = 1;
  185. $locationTypes = array( );
  186. $locationBlocks = array( );
  187. foreach ( $allBlocks as $blocks ) {
  188. //logic to check when we should increment counter
  189. $locationTypeId = $blocks['locationTypeId'];
  190. if ( !empty( $locationTypes ) ) {
  191. if ( in_array ( $locationTypeId, $locationTypes ) ) {
  192. $locationCount = array_search( $locationTypeId, $locationTypes );
  193. $blockCount = CRM_Utils_Array::value( $locationTypeId, $locationBlocks, 1 );
  194. $blockCount++;
  195. $locationBlocks[$locationTypeId] = $blockCount;
  196. } else {
  197. $locationCount++;
  198. $locationTypes[ $locationCount ] = $locationTypeId;
  199. $locationBlocks[$locationTypeId] = $blockCount = 1;
  200. }
  201. } else {
  202. $locationTypes[$locationCount] = $locationTypeId;
  203. $locationBlocks[$locationTypeId] = $blockCount;
  204. }
  205. $contactBlockIds[ $locationCount ][ $blockCount ] = array( 'id' => $blocks['id'],
  206. 'location_type_id' => $blocks['locationTypeId'] );
  207. }
  208. return $contactBlockIds;
  209. }
  210. /**
  211. * takes an associative array and creates a block
  212. *
  213. * @param string $blockName block name
  214. * @param array $params (reference ) an assoc array of name/value pairs
  215. * @param array $requiredFields fields that's are required in a block
  216. *
  217. * @return object CRM_Core_BAO_Block object on success, null otherwise
  218. * @access public
  219. * @static
  220. */
  221. static function create( $blockName, &$params, $entity = null )
  222. {
  223. if ( !self::blockExists( $blockName, $params ) ) {
  224. return null;
  225. }
  226. $name = ucfirst( $blockName );
  227. $entityElements = array( );
  228. $contactId = null;
  229. //get existing block ids if exist for this contact
  230. if ( !$entity ) {
  231. $contactId = $params[$blockName]['contact_id'];
  232. } else {
  233. $entityElements = array( 'entity_table' => $params['entity_table'],
  234. 'entity_id' => $params['entity_id']);
  235. }
  236. $blockIds = array( );
  237. $blockIds = self::getBlockIds( $blockName, $contactId, $entityElements );
  238. $isPrimary = true;
  239. $isBilling = true;
  240. $blocks = array( );
  241. foreach ( $params[$blockName] as $value ) {
  242. if ( !is_array( $value ) ) {
  243. continue;
  244. }
  245. $contactFields = array( );
  246. $locBlockCount = 1;
  247. $contactFields['contact_id' ] = $contactId;
  248. $contactFields['location_type_id'] = $value['location_type_id'];
  249. foreach ( $value as $k => $val ) {
  250. if ( !is_array( $val ) ) {
  251. continue;
  252. }
  253. if ( !empty( $blockIds ) ) {
  254. foreach( $blockIds as $locCount => $locIds ) {
  255. foreach ( $locIds as $locKey => $locVal ) {
  256. if ( $locKey == $locBlockCount &&
  257. $locVal['location_type_id'] == $value['location_type_id'] ) {
  258. $val['id'] = $locVal['id'];
  259. }
  260. }
  261. }
  262. }
  263. $dataExits = self::dataExists( self::$requiredBlockFields[$blockName], $val );
  264. $locBlockCount++;
  265. if ( isset( $val['id'] ) && !$dataExits ) {
  266. //delete the existing record
  267. self::blockDelete( $name, array( 'id' => $val['id'] ) );
  268. continue;
  269. } else if ( !$dataExits ) {
  270. continue;
  271. }
  272. if ( $isPrimary && $value['is_primary'] ) {
  273. $contactFields['is_primary'] = $value['is_primary'];
  274. $isPrimary = false;
  275. } else {
  276. $contactFields['is_primary'] = false;
  277. }
  278. if ( $isBilling && $value['is_billing'] ) {
  279. $contactFields['is_billing'] = $value['is_billing'];
  280. $isBilling = false;
  281. } else {
  282. $contactFields['is_billing'] = false;
  283. }
  284. $blockFields = array_merge( $val, $contactFields );
  285. eval ( '$blocks[] = CRM_Core_BAO_' . $name . '::add( $blockFields );' );
  286. }
  287. }
  288. return $blocks;
  289. }
  290. /**
  291. * Function to delete block
  292. *
  293. * @param string $blockName block name
  294. * @param int $params associates array
  295. *
  296. * @return void
  297. * @static
  298. */
  299. static function blockDelete ( $blockName, $params )
  300. {
  301. eval ( '$block =& new CRM_Core_DAO_' . $blockName . '( );' );
  302. $block->copyValues( $params );
  303. $block->delete();
  304. }
  305. }