PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/CRM/Contact/BAO/Individual.php

https://github.com/ksecor/civicrm
PHP | 347 lines | 207 code | 46 blank | 94 comment | 59 complexity | eb834559ed1c83af0ba5ad708ff807ef MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  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. */
  33. /**
  34. * Class contains functions for individual contact type
  35. */
  36. require_once 'CRM/Contact/DAO/Contact.php';
  37. class CRM_Contact_BAO_Individual extends CRM_Contact_DAO_Contact
  38. {
  39. /**
  40. * This is a contructor of the class.
  41. */
  42. function __construct()
  43. {
  44. }
  45. /**
  46. * Function is used to format the individual contact values
  47. *
  48. * @param array $params (reference ) an assoc array of name/value pairs
  49. * @param array $contact contact object
  50. *
  51. * @return object CRM_Contact_BAO_Contact object
  52. * @access public
  53. * @static
  54. */
  55. static function format( &$params, &$contact )
  56. {
  57. if ( ! self::dataExists($params ) ) {
  58. return;
  59. }
  60. $sortName = "";
  61. $firstName = CRM_Utils_Array::value('first_name' , $params, '');
  62. $middleName = CRM_Utils_Array::value('middle_name' , $params, '');
  63. $lastName = CRM_Utils_Array::value('last_name' , $params, '');
  64. $prefix_id = CRM_Utils_Array::value('prefix_id' , $params, '');
  65. $suffix_id = CRM_Utils_Array::value('suffix_id' , $params, '');
  66. // get prefix and suffix names
  67. $prefixes = CRM_Core_PseudoConstant::individualPrefix();
  68. $suffixes = CRM_Core_PseudoConstant::individualSuffix();
  69. $prefix = $suffix = null;
  70. if ( $prefix_id ) {
  71. $prefix = $prefixes[$prefix_id];
  72. }
  73. if ( $suffix_id ) {
  74. $suffix = $suffixes[$suffix_id];
  75. }
  76. $params['is_deceased'] = CRM_Utils_Array::value( 'is_deceased', $params, false );
  77. if ( $contact->id ) {
  78. $individual =& new CRM_Contact_BAO_Contact();
  79. $individual->id = $contact->id;
  80. if ( $individual->find( true ) ) {
  81. //lets allow to update single name field though preserveDBName
  82. //but if db having null value and params contain value, CRM-4330.
  83. $useDBNames = array( );
  84. foreach ( array( 'last', 'middle', 'first' ) as $name ) {
  85. $dbName = "{$name}_name";
  86. $value = $individual->$dbName;
  87. // the db has name values
  88. if ( $value && CRM_Utils_Array::value( 'preserveDBName', $params ) ) {
  89. $useDBNames[] = $name;
  90. }
  91. }
  92. foreach ( array( 'prefix', 'suffix' ) as $name ) {
  93. $dbName = "{$name}_id";
  94. $value = $individual->$dbName;
  95. if ( $value && CRM_Utils_Array::value( 'preserveDBName', $params ) ) {
  96. $useDBNames[] = $name;
  97. }
  98. }
  99. // CRM-4430
  100. //1. preserve db name if want
  101. //2. lets get value from param if exists.
  102. //3. if not in params, lets get from db.
  103. foreach ( array( 'last', 'middle', 'first' ) as $name ) {
  104. $phpName = "{$name}Name";
  105. $dbName = "{$name}_name";
  106. $value = $individual->$dbName;
  107. if ( in_array( $name, $useDBNames ) ) {
  108. $params[$dbName] = $value;
  109. $contact->$dbName = $value;
  110. $$phpName = $value;
  111. } else if ( array_key_exists( $dbName, $params ) ) {
  112. $$phpName = $params[$dbName];
  113. } else if ( $value ) {
  114. $$phpName = $value;
  115. }
  116. }
  117. foreach ( array( 'prefix', 'suffix' ) as $name ) {
  118. $phpName = $name;
  119. $dbName = "{$name}_id";
  120. $vals = "{$name}es";
  121. $value = $individual->$dbName;
  122. if ( in_array( $name, $useDBNames ) ) {
  123. $params[$dbName] = $value;
  124. $contact->$dbName = $value;
  125. if ( $value ) {
  126. $temp = $$vals;
  127. $$phpName = $temp[$value];
  128. } else {
  129. $$phpName = null;
  130. }
  131. } else if ( array_key_exists( $dbName, $params ) ) {
  132. $temp = $$vals;
  133. // CRM-5278
  134. if ( ! empty( $params[$dbName] ) ) {
  135. $$phpName = CRM_Utils_Array::value( $params[$dbName], $temp );
  136. }
  137. } else if ( $value ) {
  138. $temp = $$vals;
  139. $$phpName = $temp[$value];
  140. }
  141. }
  142. }
  143. }
  144. if ( $lastName || $firstName || $middleName ) {
  145. if ( $lastName && $firstName ) {
  146. $contact->sort_name = trim( "$lastName, $firstName" );
  147. } else {
  148. $contact->sort_name = trim( "$lastName $firstName" );
  149. }
  150. $display_name =
  151. trim( "$prefix $firstName $middleName $lastName $suffix" );
  152. $display_name = str_replace( ' ', ' ', $display_name );
  153. }
  154. if (isset( $display_name ) &&
  155. trim( $display_name ) ) {
  156. $contact->display_name = trim( $display_name );
  157. }
  158. if ( CRM_Utils_Array::value( 'email', $params ) && is_array( $params['email'] ) ) {
  159. foreach ($params['email'] as $emailBlock) {
  160. if ( isset( $emailBlock['is_primary'] ) ) {
  161. $email = $emailBlock['email'];
  162. break;
  163. }
  164. }
  165. }
  166. $uniqId = CRM_Utils_Array::value( 'user_unique_id', $params );
  167. if (empty($contact->display_name)) {
  168. if (isset($email)) {
  169. $contact->display_name = $email;
  170. } else if (isset($uniqId)) {
  171. $contact->display_name = $uniqId;
  172. }
  173. }
  174. if (empty($contact->sort_name)) {
  175. if (isset($email)) {
  176. $contact->sort_name = $email;
  177. } else if (isset($uniqId)) {
  178. $contact->sort_name = $uniqId;
  179. }
  180. }
  181. $format = CRM_Core_Dao::getFieldValue('CRM_Core_DAO_PreferencesDate',
  182. 'birth', 'date_format', 'name' );
  183. if ( $date = CRM_Utils_Array::value('birth_date', $params) ) {
  184. if ( in_array( $format, array('dd/mm', 'mm/dd' ) ) ) {
  185. $date = "{$date}/1902";
  186. }
  187. $contact->birth_date = CRM_Utils_Date::processDate($date) ;
  188. //$contact->birth_date = preg_replace('/[^0-9]/', '', $date);
  189. } else if ( $contact->birth_date ) {
  190. $contact->birth_date = CRM_Utils_Date::isoToMysql( $contact->birth_date );
  191. }
  192. if ( $date = CRM_Utils_Array::value('deceased_date', $params) ) {
  193. if ( in_array( $format, array('dd/mm', 'mm/dd' ) ) ) {
  194. $date = "{$date}/1902";
  195. }
  196. $contact->deceased_date = CRM_Utils_Date::processDate($date) ;
  197. } else if ( $contact->deceased_date ) {
  198. $contact->deceased_date = CRM_Utils_Date::isoToMysql( $contact->deceased_date );
  199. }
  200. if ( $middle_name = CRM_Utils_Array::value('middle_name', $params)) {
  201. $contact->middle_name = $middle_name;
  202. }
  203. return $contact;
  204. }
  205. /**
  206. * Given the list of params in the params array, fetch the object
  207. * and store the values in the values array
  208. *
  209. * @param array $params input parameters to find object
  210. * @param array $values output values of the object
  211. * @param array $ids the array that holds all the db ids
  212. *
  213. * @return CRM_Contact_BAO_Contact|null the found object or null
  214. * @access public
  215. * @static
  216. */
  217. static function getValues( &$params, &$values, &$ids )
  218. {
  219. $individual =& new CRM_Contact_BAO_Individual( );
  220. $individual->copyValues( $params );
  221. if ( $individual->find(true) ) {
  222. $ids['individual'] = $individual->id;
  223. CRM_Core_DAO::storeValues( $individual, $values );
  224. if ( isset( $individual->birth_date ) ) {
  225. $values['birth_date'] = CRM_Utils_Date::unformat( $individual->birth_date );
  226. }
  227. return $individual;
  228. }
  229. return null;
  230. }
  231. /**
  232. * regenerates display_name for contacts with given prefixes/suffixes
  233. *
  234. * @param array $ids the array with the prefix/suffix id governing which contacts to regenerate
  235. * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED
  236. *
  237. * @return void
  238. */
  239. static function updateDisplayNames( &$ids, $action )
  240. {
  241. // get the proper field name (prefix_id or suffix_id) and its value
  242. $fieldName = '';
  243. foreach ($ids as $key => $value) {
  244. switch ($key) {
  245. case 'individualPrefix':
  246. $fieldName = 'prefix_id';
  247. $fieldValue = $value;
  248. break 2;
  249. case 'individualSuffix':
  250. $fieldName = 'suffix_id';
  251. $fieldValue = $value;
  252. break 2;
  253. }
  254. }
  255. if ($fieldName == '') return;
  256. // query for the affected individuals
  257. $fieldValue = CRM_Utils_Type::escape($fieldValue, 'Integer');
  258. $contact =& new CRM_Contact_BAO_Contact( );
  259. $contact->$fieldName = $fieldValue;
  260. $contact->find();
  261. // iterate through the affected individuals and rebuild their display_names
  262. require_once 'CRM/Contact/BAO/Contact.php';
  263. while ($contact->fetch()) {
  264. $contact =& new CRM_Contact_BAO_Contact();
  265. $contact->id = $contact->contact_id;
  266. if ($action == CRM_Core_Action::DELETE) {
  267. $contact->$fieldName = 'NULL';
  268. $contact->save();
  269. }
  270. $contact->display_name = $contact->displayName();
  271. $contact->save();
  272. }
  273. }
  274. /**
  275. * creates display name
  276. *
  277. * @return string the constructed display name
  278. */
  279. function displayName()
  280. {
  281. $prefix =& CRM_Core_PseudoConstant::individualPrefix();
  282. $suffix =& CRM_Core_PseudoConstant::individualSuffix();
  283. return str_replace(' ', ' ', trim($prefix[$this->prefix_id] . ' ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name . ' ' . $suffix[$this->suffix_id]));
  284. }
  285. /**
  286. * Check if there is data to create the object
  287. *
  288. * @param array $params (reference ) an assoc array of name/value pairs
  289. *
  290. * @return boolean
  291. * @access public
  292. * @static
  293. */
  294. static function dataExists( &$params )
  295. {
  296. if ( $params['contact_type'] == 'Individual' ) {
  297. return true;
  298. }
  299. return false;
  300. }
  301. }