PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/CRM/Profile/Page/Listings.php

https://github.com/ksecor/civicrm
PHP | 359 lines | 204 code | 54 blank | 101 comment | 60 complexity | 753d7de5daccc3d162a8b5b9c5833a0f 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. require_once 'CRM/Profile/Selector/Listings.php';
  34. require_once 'CRM/Core/Selector/Controller.php';
  35. require_once 'CRM/Core/Page.php';
  36. /**
  37. * This implements the profile page for all contacts. It uses a selector
  38. * object to do the actual dispay. The fields displayd are controlled by
  39. * the admin
  40. */
  41. class CRM_Profile_Page_Listings extends CRM_Core_Page {
  42. /**
  43. * all the fields that are listings related
  44. *
  45. * @var array
  46. * @access protected
  47. */
  48. protected $_fields;
  49. /**
  50. * the custom fields for this domain
  51. *
  52. * @var array
  53. * @access protected
  54. */
  55. protected $_customFields;
  56. /**
  57. * The input params from the request
  58. *
  59. * @var array
  60. * @access protected
  61. */
  62. protected $_params;
  63. /**
  64. * The group id that we are editing
  65. *
  66. * @var int
  67. */
  68. protected $_gid;
  69. /**
  70. * state wether to display serch form or not
  71. *
  72. * @var int
  73. */
  74. protected $_search;
  75. /**
  76. * Should we display a map
  77. *
  78. * @var int
  79. */
  80. protected $_map;
  81. /**
  82. * extracts the parameters from the request and constructs information for
  83. * the selector object to do a query
  84. *
  85. * @return void
  86. * @access public
  87. *
  88. */
  89. function preProcess( ) {
  90. $this->_search = true;
  91. $search = CRM_Utils_Request::retrieve( 'search', 'Boolean',
  92. $this, false, 0, 'GET' );
  93. if( isset( $search ) && $search == 0) {
  94. $this->_search = false;
  95. }
  96. $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
  97. $this, true, 0, 'GET' );
  98. require_once 'CRM/Core/BAO/UFGroup.php';
  99. $this->_fields =
  100. CRM_Core_BAO_UFGroup::getListingFields( CRM_Core_Action::UPDATE,
  101. CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY,
  102. false, $this->_gid, false, 'Profile',
  103. CRM_Core_Permission::SEARCH );
  104. $this->_customFields = CRM_Core_BAO_CustomField::getFieldsForImport( null );
  105. $this->_params = array( );
  106. $resetArray = array( 'group', 'tag', 'preferred_communication_method', 'do_not_phone',
  107. 'do_not_email', 'do_not_mail', 'do_not_sms', 'do_not_trade', 'gender' );
  108. foreach ( $this->_fields as $name => $field ) {
  109. if ( (substr($name, 0, 6) == 'custom') && CRM_Utils_Array::value( 'is_search_range', $field ) ) {
  110. $from = CRM_Utils_Request::retrieve( $name.'_from', 'String',
  111. $this, false, null, 'REQUEST' );
  112. $to = CRM_Utils_Request::retrieve( $name.'_to', 'String',
  113. $this, false, null, 'REQUEST' );
  114. $value = array();
  115. if ( $from && $to ) {
  116. $value['from'] = $from;
  117. $value['to'] = $to;
  118. } else if ( $from ) {
  119. $value['from'] = $from;
  120. } else if ( $to ) {
  121. $value['to'] = $to;
  122. }
  123. } else if ( ( substr($name, 0, 7) == 'custom_' ) &&
  124. ( CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_CustomField',
  125. substr($name, 7), 'html_type' ) == 'TextArea' ) ) {
  126. $value = trim( CRM_Utils_Request::retrieve( $name, 'String',
  127. $this, false, null, 'REQUEST' ) );
  128. if ( ! empty($value) &&
  129. ! ( ( substr( $value, 0, 1 ) == '%' ) &&
  130. ( substr( $value, -1, 1 ) == '%' ) ) ) {
  131. $value = '%' . $value . '%';
  132. }
  133. } else if ( CRM_Utils_Array::value( 'html_type', $field ) == 'Multi-Select State/Province'
  134. || CRM_Utils_Array::value( 'html_type', $field ) == 'Multi-Select Country') {
  135. $value = CRM_Utils_Request::retrieve( $name, 'String', $this, false, null, 'REQUEST' );
  136. if ( ! is_array($value) ) $value = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, substr($value,1,-1));
  137. } else {
  138. $value = CRM_Utils_Request::retrieve( $name, 'String',
  139. $this, false, null, 'REQUEST' );
  140. }
  141. if ( ( $name == 'group' || $name == 'tag' ) && ! empty( $value ) && ! is_array( $value ) ) {
  142. $v = explode( ',', $value );
  143. $value = array( );
  144. foreach ( $v as $item ) {
  145. $value[$item] = 1;
  146. }
  147. }
  148. $customField = CRM_Utils_Array::value( $name, $this->_customFields );
  149. if ( ! empty( $_POST ) && ! CRM_Utils_Array::value( $name, $_POST ) ) {
  150. if ( $customField ) {
  151. // reset checkbox/radio because a form does not send null checkbox values
  152. if ( in_array( $customField['html_type'],
  153. array( 'Multi-Select', 'CheckBox', 'Multi-Select State/Province', 'Multi-Select Country', 'Radio' ) ) ) {
  154. // only reset on a POST submission if we dont see any value
  155. $value = null;
  156. $this->set( $name, $value );
  157. }
  158. } else if ( in_array( $name, $resetArray ) ) {
  159. $value = null;
  160. $this->set( $name, $value );
  161. }
  162. }
  163. if ( isset( $value ) && $value != null ) {
  164. if ( !is_array( $value) ) {
  165. $value = trim( $value );
  166. }
  167. $this->_params[$name] = $this->_fields[$name]['value'] = $value;
  168. }
  169. }
  170. // set the params in session
  171. $session =& CRM_Core_Session::singleton();
  172. $session->set('profileParams', $this->_params);
  173. }
  174. /**
  175. * run this page (figure out the action needed and perform it).
  176. *
  177. * @return void
  178. */
  179. function run( ) {
  180. $this->preProcess( );
  181. $this->assign( 'recentlyViewed', false );
  182. if ( $this->_gid ) {
  183. $ufgroupDAO = new CRM_Core_DAO_UFGroup( );
  184. $ufgroupDAO->id = $this->_gid;
  185. if ( ! $ufgroupDAO->find( true ) ) {
  186. CRM_Core_Error::fatal( );
  187. }
  188. }
  189. if ( $this->_gid ) {
  190. // set the title of the page
  191. if ( $ufgroupDAO->title ) {
  192. CRM_Utils_System::setTitle( $ufgroupDAO->title );
  193. }
  194. }
  195. // do not do any work if we are in reset mode
  196. if ( ! CRM_Utils_Array::value( 'reset', $_GET ) ||
  197. CRM_Utils_Array::value( 'force', $_GET ) ) {
  198. $this->assign( 'isReset', false );
  199. $map = 0;
  200. $linkToUF = 0;
  201. $editLink = false;
  202. if ( $this->_gid ) {
  203. $map = $ufgroupDAO->is_map;
  204. $linkToUF = $ufgroupDAO->is_uf_link;
  205. $editLink = $ufgroupDAO->is_edit_link;
  206. }
  207. if ( $map ) {
  208. $this->assign( 'mapURL',
  209. CRM_Utils_System::url( 'civicrm/profile/map',
  210. "map=1&gid={$this->_gid}&reset=1" ) );
  211. }
  212. if ( CRM_Utils_Array::value( 'group', $this->_params ) ) {
  213. foreach( $this->_params['group'] as $key => $val ) {
  214. if ( !$val ) {
  215. unset( $this->_params['group'][$key] );
  216. }
  217. }
  218. }
  219. // the selector will override this if the user does have
  220. // edit permissions as determined by the mask, CRM-4341
  221. // do not allow edit for anon users in joomla frontend, CRM-4668
  222. $config =& CRM_Core_Config::singleton( );
  223. if ( ! CRM_Core_Permission::check( 'access CiviCRM' ) ||
  224. $config->userFrameworkFrontend == 1 ) {
  225. $editLink = false;
  226. }
  227. $selector =& new CRM_Profile_Selector_Listings( $this->_params, $this->_customFields, $this->_gid,
  228. $map, $editLink, $linkToUF );
  229. $controller =& new CRM_Core_Selector_Controller($selector ,
  230. $this->get( CRM_Utils_Pager::PAGE_ID ),
  231. $this->get( CRM_Utils_Sort::SORT_ID ),
  232. CRM_Core_Action::VIEW,
  233. $this,
  234. CRM_Core_Selector_Controller::TEMPLATE );
  235. $controller->setEmbedded( true );
  236. $controller->run( );
  237. } else {
  238. $this->assign( 'isReset', true );
  239. }
  240. // we have to build form since pager needs it.
  241. $formController =& new CRM_Core_Controller_Simple( 'CRM_Profile_Form_Search',
  242. ts('Search Profile'),
  243. CRM_Core_Action::ADD );
  244. $formController->setEmbedded( true );
  245. $formController->process( );
  246. $formController->run( );
  247. // also get the search tpl name
  248. $this->assign( 'searchTPL', $formController->getTemplateFileName( ) );
  249. $this->assign( 'search', $this->_search );
  250. return parent::run( );
  251. }
  252. /**
  253. * Function to get the list of contacts for a profile
  254. *
  255. * @param $form object
  256. *
  257. * @access public
  258. */
  259. function getProfileContact( $gid )
  260. {
  261. $session =& CRM_Core_Session::singleton();
  262. $params = $session->get('profileParams');
  263. $details = array( );
  264. $ufGroupParam = array('id' => $gid );
  265. require_once "CRM/Core/BAO/UFGroup.php";
  266. CRM_Core_BAO_UFGroup::retrieve($ufGroupParam, $details);
  267. // make sure this group can be mapped
  268. if ( ! $details['is_map'] ) {
  269. CRM_Core_Error::statusBounce( ts('This profile does not have the map feature turned on.') );
  270. }
  271. $groupId = CRM_Utils_Array::value('limit_listings_group_id', $details);
  272. // add group id to params if a uf group belong to a any group
  273. if ($groupId) {
  274. if ( CRM_Utils_Array::value('group', $params ) ) {
  275. $params['group'][$groupId] = 1;
  276. } else {
  277. $params['group'] = array($groupId => 1);
  278. }
  279. }
  280. $fields = CRM_Core_BAO_UFGroup::getListingFields( CRM_Core_Action::VIEW,
  281. CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY |
  282. CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY,
  283. false, $gid );
  284. $returnProperties =& CRM_Contact_BAO_Contact::makeHierReturnProperties( $fields );
  285. $returnProperties['contact_type'] = 1;
  286. $returnProperties['sort_name' ] = 1;
  287. $queryParams =& CRM_Contact_BAO_Query::convertFormValues( $params, 1 );
  288. $query =& new CRM_Contact_BAO_Query( $queryParams, $returnProperties, $fields );
  289. $ids = $query->searchQuery( 0, 0, null,
  290. false, false, false,
  291. true, false );
  292. $contactIds = explode( ',', $ids );
  293. return $contactIds;
  294. }
  295. function getTemplateFileName() {
  296. if ( $this->_gid ) {
  297. $templateFile = "CRM/Profile/Page/{$this->_gid}/Listings.tpl";
  298. $template =& CRM_Core_Page::getTemplate( );
  299. if ( $template->template_exists( $templateFile ) ) {
  300. return $templateFile;
  301. }
  302. }
  303. return parent::getTemplateFileName( );
  304. }
  305. }