PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/drupal/sites/all/modules/civicrm/CRM/Profile/Page/Dynamic.php

https://github.com/michaelmcandrew/vaw
PHP | 320 lines | 166 code | 43 blank | 111 comment | 35 complexity | 7c65d4f66d876a77a92e5ac583a77bff 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/Page.php';
  35. require_once 'CRM/Core/BAO/UFGroup.php';
  36. /**
  37. * Create a page for displaying CiviCRM Profile Fields.
  38. *
  39. * Heart of this class is the run method which checks
  40. * for action type and then displays the appropriate
  41. * page.
  42. *
  43. */
  44. class CRM_Profile_Page_Dynamic extends CRM_Core_Page {
  45. /**
  46. * The contact id of the person we are viewing
  47. *
  48. * @var int
  49. * @access protected
  50. */
  51. protected $_id;
  52. /**
  53. * the profile group are are interested in
  54. *
  55. * @var int
  56. * @access protected
  57. */
  58. protected $_gid;
  59. /**
  60. * The profile types we restrict this page to display
  61. *
  62. * @var string
  63. * @access protected
  64. */
  65. protected $_restrict;
  66. /**
  67. * Should we bypass permissions
  68. *
  69. * @var boolean
  70. * @access prootected
  71. */
  72. protected $_skipPermission;
  73. /**
  74. * Store profile ids if multiple profile ids are passed using comma separated.
  75. * Currently lets implement this functionality only for dialog mode
  76. */
  77. protected $_profileIds = array( );
  78. /**
  79. * Contact profile having activity fields?
  80. *
  81. * @var string
  82. */
  83. protected $_isContactActivityProfile = false;
  84. /**
  85. * Activity Id connected to the profile
  86. *
  87. * @var string
  88. */
  89. protected $_activityId = null;
  90. /**
  91. * class constructor
  92. *
  93. * @param int $id the contact id
  94. * @param int $gid the group id
  95. *
  96. * @return void
  97. * @access public
  98. */
  99. function __construct( $id, $gid, $restrict, $skipPermission = false, $profileIds = null ) {
  100. parent::__construct( );
  101. $this->_id = $id;
  102. $this->_gid = $gid;
  103. $this->_restrict = $restrict;
  104. $this->_skipPermission = $skipPermission;
  105. if ( $profileIds ) {
  106. $this->_profileIds = $profileIds;
  107. } else {
  108. $this->_profileIds = array( $gid );
  109. }
  110. $this->_activityId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, false, 0, 'GET');
  111. if (is_numeric($this->_activityId)) {
  112. require_once 'CRM/Activity/BAO/Activity.php';
  113. $latestRevisionId = CRM_Activity_BAO_Activity::getLatestActivityId($this->_activityId);
  114. if ($latestRevisionId) {
  115. $this->_activityId = $latestRevisionId;
  116. }
  117. }
  118. require_once 'CRM/Core/BAO/UFField.php';
  119. $this->_isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType( $this->_gid );
  120. }
  121. /**
  122. * Get the action links for this page.
  123. *
  124. * @return array $_actionLinks
  125. *
  126. */
  127. function &actionLinks()
  128. {
  129. return null;
  130. }
  131. /**
  132. * Run the page.
  133. *
  134. * This method is called after the page is created. It checks for the
  135. * type of action and executes that action.
  136. *
  137. * @return void
  138. * @access public
  139. *
  140. */
  141. function run()
  142. {
  143. $template = CRM_Core_Smarty::singleton( );
  144. if ( $this->_id && $this->_gid ) {
  145. // first check that id is part of the limit group id, CRM-4822
  146. $limitListingsGroupsID = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_UFGroup',
  147. $this->_gid,
  148. 'limit_listings_group_id' );
  149. $config = CRM_Core_Config::singleton( );
  150. if ( $limitListingsGroupsID ) {
  151. require_once 'CRM/Contact/BAO/GroupContact.php';
  152. if ( !CRM_Contact_BAO_GroupContact::isContactInGroup( $this->_id,
  153. $limitListingsGroupsID ) ) {
  154. CRM_Utils_System::setTitle( ts( 'Profile View - Permission Denied' ) );
  155. return CRM_Core_Session::setStatus(ts('You do not have permission to view this contact record. Contact the site administrator if you need assistance.'));
  156. }
  157. }
  158. $values = array( );
  159. $fields = CRM_Core_BAO_UFGroup::getFields( $this->_profileIds, false, CRM_Core_Action::VIEW,
  160. null, null, false, $this->_restrict,
  161. $this->_skipPermission, null,
  162. CRM_Core_Permission::VIEW );
  163. if ( $this->_isContactActivityProfile && $this->_gid ) {
  164. require_once 'CRM/Profile/Form.php';
  165. $errors = CRM_Profile_Form::validateContactActivityProfile($this->_activityId, $this->_id, $this->_gid);
  166. if ( !empty($errors) ) {
  167. CRM_Core_Error::fatal( ts(array_pop($errors)) );
  168. }
  169. }
  170. $session = CRM_Core_Session::singleton( );
  171. $userID = $session->get( 'userID' );
  172. $this->_isPermissionedChecksum = false;
  173. require_once 'CRM/Contact/BAO/Contact/Utils.php';
  174. require_once 'CRM/Contact/BAO/Contact/Permission.php';
  175. if ( $this->_id != $userID ) {
  176. // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
  177. require_once 'CRM/Contact/BAO/Contact/Permission.php';
  178. if ( $config->userFrameworkFrontend ) {
  179. $this->_isPermissionedChecksum = CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum( $this->_id, $this, false );
  180. } else {
  181. $this->_isPermissionedChecksum = CRM_Contact_BAO_Contact_Permission::validateChecksumContact( $this->_id, $this, false );
  182. }
  183. }
  184. // make sure we dont expose all fields based on permission
  185. $admin = false;
  186. if ( ( ! $config->userFrameworkFrontend &&
  187. ( CRM_Core_Permission::check( 'administer users' ) ||
  188. CRM_Core_Permission::check( 'view all contacts' ) ||
  189. CRM_Contact_BAO_Contact_Permission::allow( $this->_id, CRM_Core_Permission::VIEW ) ) ) ||
  190. $this->_id == $userID ||
  191. $this->_isPermissionedChecksum ) {
  192. $admin = true;
  193. }
  194. if ( ! $admin ) {
  195. foreach ( $fields as $name => $field ) {
  196. // make sure that there is enough permission to expose this field
  197. if ( $field['visibility'] == 'User and User Admin Only' ) {
  198. unset( $fields[$name] );
  199. }
  200. }
  201. }
  202. if ( $this->_isContactActivityProfile ) {
  203. $contactFields = $activityFields = array( );
  204. foreach ( $fields as $fieldName => $field ) {
  205. if ( CRM_Utils_Array::value('field_type', $field) == 'Activity' ) {
  206. $activityFields[$fieldName] = $field;
  207. } else {
  208. $contactFields[$fieldName] = $field;
  209. }
  210. }
  211. CRM_Core_BAO_UFGroup::getValues( $this->_id, $contactFields, $values );
  212. if ( $this->_activityId ) {
  213. CRM_Core_BAO_UFGroup::getValues( null, $activityFields, $values, true, array( array( 'activity_id', '=', $this->_activityId, 0, 0 ) ) );
  214. }
  215. } else {
  216. CRM_Core_BAO_UFGroup::getValues( $this->_id, $fields, $values );
  217. }
  218. // $profileFields array can be used for customized display of field labels and values in Profile/View.tpl
  219. $profileFields = array( );
  220. $labels = array( );
  221. foreach ( $fields as $name => $field ) {
  222. $labels[$field['title']] = preg_replace('/\s+|\W+/', '_', $name);
  223. }
  224. foreach ( $values as $title => $value ) {
  225. $profileFields[$labels[$title]] = array( 'label' => $title,
  226. 'value' => $value );
  227. }
  228. $template->assign_by_ref( 'row', $values );
  229. $template->assign_by_ref( 'profileFields', $profileFields );
  230. }
  231. $name = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_UFGroup', $this->_gid, 'name' );
  232. if ( strtolower( $name ) == 'summary_overlay' ) {
  233. $template->assign( 'overlayProfile', true );
  234. }
  235. $title = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_UFGroup', $this->_gid, 'title' );
  236. //CRM-4131.
  237. $displayName = CRM_Core_DAO::getFieldValue( 'CRM_Contact_DAO_Contact', $this->_id, 'display_name' );
  238. if ( $displayName ) {
  239. require_once 'CRM/Core/Permission.php';
  240. require_once 'CRM/Contact/BAO/Contact/Permission.php';
  241. $session = CRM_Core_Session::singleton( );
  242. $config = CRM_Core_Config::singleton( );
  243. if ( $session->get( 'userID' ) &&
  244. CRM_Core_Permission::check('access CiviCRM') &&
  245. CRM_Contact_BAO_Contact_Permission::allow( $session->get( 'userID' ), CRM_Core_Permission::VIEW ) &&
  246. !$config->userFrameworkFrontend ) {
  247. $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "action=view&reset=1&cid={$this->_id}", true);
  248. $this->assign( 'displayName', $displayName);
  249. $displayName = "<a href=\"$contactViewUrl\">{$displayName}</a>";
  250. }
  251. $title .= ' - ' . $displayName;
  252. }
  253. CRM_Utils_System::setTitle( $title );
  254. // invoke the pagRun hook, CRM-3906
  255. require_once 'CRM/Utils/Hook.php';
  256. CRM_Utils_Hook::pageRun( $this );
  257. return trim( $template->fetch( $this->getTemplateFileName( ) ) );
  258. }
  259. function getTemplateFileName() {
  260. if ( $this->_gid ) {
  261. $templateFile = "CRM/Profile/Page/{$this->_gid}/Dynamic.tpl";
  262. $template =& CRM_Core_Page::getTemplate( );
  263. if ( $template->template_exists( $templateFile ) ) {
  264. return $templateFile;
  265. }
  266. // lets see if we have customized by name
  267. $ufGroupName = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_UFGroup', $this->_gid, 'name' );
  268. if ( $ufGroupName ) {
  269. $templateFile = "CRM/Profile/Page/{$ufGroupName}/Dynamic.tpl";
  270. if ( $template->template_exists( $templateFile ) ) {
  271. return $templateFile;
  272. }
  273. }
  274. }
  275. return parent::getTemplateFileName( );
  276. }
  277. }