PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/CRM/Contact/Page/View/Relationship.php

https://github.com/ksecor/civicrm
PHP | 285 lines | 155 code | 36 blank | 94 comment | 23 complexity | 2327b2cc27e55a59fc56d6b8bb1b48f2 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/Contact/Page/View.php';
  34. class CRM_Contact_Page_View_Relationship extends CRM_Contact_Page_View {
  35. /**
  36. * The action links that we need to display for the browse screen
  37. *
  38. * @var array
  39. * @static
  40. */
  41. static $_links = null;
  42. /**
  43. * casid set if called from case context
  44. *
  45. * @var int
  46. */
  47. public $_caseId = null;
  48. /**
  49. * View details of a relationship
  50. *
  51. * @return void
  52. *
  53. * @access public
  54. */
  55. function view( )
  56. {
  57. require_once 'CRM/Core/DAO.php';
  58. $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship( $this->_contactId, null, null, null, $this->_id );
  59. //To check whether selected contact is a contact_id_a in
  60. //relationship type 'a_b' in relationship table, if yes then
  61. //revert the permissionship text in template
  62. $relationship =& new CRM_Contact_DAO_Relationship( );
  63. $relationship->id = $viewRelationship[$this->_id]['id'];
  64. if ($relationship->find(true)) {
  65. if ( ( $viewRelationship[$this->_id]['rtype'] == 'a_b' ) && ( $this->_contactId == $relationship->contact_id_a ) ) {
  66. $this->assign( "is_contact_id_a", true );
  67. }
  68. }
  69. $relType = $viewRelationship[$this->_id]['civicrm_relationship_type_id'];
  70. $this->assign( 'viewRelationship', $viewRelationship );
  71. $viewNote = CRM_Core_BAO_Note::getNote($this->_id);
  72. $this->assign( 'viewNote', $viewNote );
  73. $groupTree =& CRM_Core_BAO_CustomGroup::getTree('Relationship', $this, $this->_id,0,$relType);
  74. CRM_Core_BAO_CustomGroup::buildCustomDataView( $this, $groupTree );
  75. }
  76. /**
  77. * This function is called when action is browse
  78. *
  79. * return null
  80. * @access public
  81. */
  82. function browse( ) {
  83. $links =& self::links( );
  84. //CRM-4418, handling edit and delete separately.
  85. $permissions = array( $this->_permission );
  86. if ( $this->_permission == CRM_Core_Permission::EDIT ) {
  87. //previously delete was subset of edit
  88. //so for consistency lets grant delete also.
  89. $permissions[] = CRM_Core_Permission::DELETE;
  90. }
  91. $mask = CRM_Core_Action::mask( $permissions );
  92. $currentRelationships = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId,
  93. CRM_Contact_BAO_Relationship::CURRENT ,
  94. 0, 0, 0,
  95. $links, $mask );
  96. $inactiveRelationships = CRM_Contact_BAO_Relationship::getRelationship( $this->_contactId,
  97. CRM_Contact_BAO_Relationship::INACTIVE ,
  98. 0, 0, 0,
  99. $links, $mask );
  100. $this->assign( 'currentRelationships', $currentRelationships );
  101. // to show the 'Current Relationships' title and links only when viewed
  102. // from relationship tab, not from dashboard
  103. $this->assign( 'relationshipTabContext', true );
  104. $this->assign( 'inactiveRelationships', $inactiveRelationships );
  105. }
  106. /**
  107. * This function is called when action is update or new
  108. *
  109. * return null
  110. * @access public
  111. */
  112. function edit( ) {
  113. $controller =& new CRM_Core_Controller_Simple( 'CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action );
  114. $controller->setEmbedded( true );
  115. // set the userContext stack
  116. $session =& CRM_Core_Session::singleton();
  117. // if this is called from case view, we need to redirect back to same page
  118. if ( $this->_caseId ) {
  119. $cid = CRM_Utils_Request::retrieve( 'cid', 'Integer', $this, false );
  120. $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$cid}&id={$this->_caseId}" );
  121. } else {
  122. $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel' );
  123. }
  124. $session->pushUserContext( $url );
  125. if (CRM_Utils_Request::retrieve('confirmed', 'Boolean',
  126. CRM_Core_DAO::$_nullObject ) ) {
  127. if ( $this->_caseId ) {
  128. //create an activity for case role removal.CRM-4480
  129. require_once "CRM/Case/BAO/Case.php";
  130. CRM_Case_BAO_Case::createCaseRoleActivity( $this->_caseId, $this->_id );
  131. CRM_Core_Session::setStatus( ts('Case Role has been deleted successfuly.'), false );
  132. }
  133. // delete relationship
  134. CRM_Contact_BAO_Relationship::del( $this->_id);
  135. CRM_Utils_System::redirect($url);
  136. }
  137. $controller->set( 'contactId', $this->_contactId );
  138. $controller->set( 'id' , $this->_id );
  139. $controller->process( );
  140. $controller->run( );
  141. }
  142. /**
  143. * This function is the main function that is called when the page loads,
  144. * it decides the which action has to be taken for the page.
  145. *
  146. * return null
  147. * @access public
  148. */
  149. function run( ) {
  150. $this->preProcess( );
  151. $this->setContext( );
  152. $this->_caseId = CRM_Utils_Request::retrieve( 'caseID', 'Integer', $this );
  153. if ( $this->_action & CRM_Core_Action::VIEW ) {
  154. $this->view( );
  155. } else if ( $this->_action & ( CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE ) ) {
  156. $this->edit( );
  157. } else if ( $this->_action & CRM_Core_Action::DISABLE ) {
  158. CRM_Contact_BAO_Relationship::disableEnableRelationship( $this->_id, CRM_Core_Action::DISABLE );
  159. CRM_Contact_BAO_Relationship::setIsActive( $this->_id, 0 ) ;
  160. $session =& CRM_Core_Session::singleton();
  161. CRM_Utils_System::redirect( $session->popUserContext() );
  162. } else if ( $this->_action & CRM_Core_Action::ENABLE ) {
  163. CRM_Contact_BAO_Relationship::disableEnableRelationship( $this->_id, CRM_Core_Action::ENABLE );
  164. CRM_Contact_BAO_Relationship::setIsActive( $this->_id, 1 ) ;
  165. $session =& CRM_Core_Session::singleton();
  166. CRM_Utils_System::redirect( $session->popUserContext() );
  167. }
  168. // if this is called from case view, suppress browse relationships form
  169. if ( !$this->_caseId ) {
  170. $this->browse( );
  171. }
  172. return parent::run( );
  173. }
  174. function setContext( )
  175. {
  176. $context = CRM_Utils_Request::retrieve( 'context', 'String',
  177. $this, false, 'search' );
  178. if ( $context == 'dashboard' ) {
  179. $cid = CRM_Utils_Request::retrieve( 'cid', 'Integer',
  180. $this, false );
  181. $url = CRM_Utils_System::url( 'civicrm/user',
  182. "reset=1&id={$cid}" );
  183. } else {
  184. $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel' );
  185. }
  186. $session =& CRM_Core_Session::singleton( );
  187. $session->pushUserContext( $url );
  188. }
  189. /**
  190. * This function is called to delete the relationship of a contact
  191. *
  192. * return null
  193. * @access public
  194. */
  195. function delete( ) {
  196. // calls a function to delete relationship
  197. CRM_Contact_BAO_Relationship::del($this->_id);
  198. }
  199. /**
  200. * Get action links
  201. *
  202. * @return array (reference) of action links
  203. * @static
  204. */
  205. static function &links()
  206. {
  207. if (!(self::$_links)) {
  208. $deleteExtra = ts('Are you sure you want to delete this relationship?');
  209. $disableExtra = ts('Are you sure you want to disable this relationship?');
  210. $enableExtra = ts('Are you sure you want to re-enable this relationship?');
  211. self::$_links = array(
  212. CRM_Core_Action::VIEW => array(
  213. 'name' => ts('View'),
  214. 'url' => 'civicrm/contact/view/rel',
  215. 'qs' => 'action=view&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
  216. 'title' => ts('View Relationship')
  217. ),
  218. CRM_Core_Action::UPDATE => array(
  219. 'name' => ts('Edit'),
  220. 'url' => 'civicrm/contact/view/rel',
  221. 'qs' => 'action=update&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
  222. 'title' => ts('Edit Relationship')
  223. ),
  224. CRM_Core_Action::ENABLE => array(
  225. 'name' => ts('Enable'),
  226. 'url' => 'civicrm/contact/view/rel',
  227. 'qs' => 'action=enable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
  228. 'extra' => 'onclick = "return confirm(\'' . $enableExtra . '\');"',
  229. 'title' => ts('Enable Relationship')
  230. ),
  231. CRM_Core_Action::DISABLE => array(
  232. 'name' => ts('Disable'),
  233. 'url' => 'civicrm/contact/view/rel',
  234. 'qs' => 'action=disable&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%&selectedChild=rel',
  235. 'extra' => 'onclick = "return confirm(\'' . $disableExtra . '\');"',
  236. 'title' => ts('Disable Relationship')
  237. ),
  238. CRM_Core_Action::DELETE => array(
  239. 'name' => ts('Delete'),
  240. 'url' => 'civicrm/contact/view/rel',
  241. 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&rtype=%%rtype%%',
  242. 'extra' => 'onclick = "if (confirm(\'' . $deleteExtra . '\') ) this.href+=\'&amp;confirmed=1\'; else return false;"',
  243. 'title' => ts('Delete Relationship')
  244. ),
  245. );
  246. }
  247. return self::$_links;
  248. }
  249. }