Add EntityPageTrait
authorMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Sat, 1 Jun 2019 18:41:02 +0000 (19:41 +0100)
committerMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Sat, 1 Jun 2019 18:47:40 +0000 (19:47 +0100)
CRM/Contact/Page/View/Relationship.php
CRM/Core/Page/EntityPageTrait.php [new file with mode: 0644]

index 8e7006369a4aad0cec2f0595cca2c1b28b152c2a..cc05f439e2906c8e79da93d4eefd77967cb0619e 100644 (file)
  */
 class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
 
-  /**
-   * The action links that we need to display for the browse screen.
-   *
-   * @var array
-   */
-  public static $_links = NULL;
+  use CRM_Core_Page_EntityPageTrait;
 
   /**
    * Casid set if called from case context.
@@ -46,83 +41,112 @@ class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
    */
   public $_caseId = NULL;
 
-  public $_permission = NULL;
-  public $_contactId = NULL;
+  /**
+   * @param int $caseId
+   */
+  public function setCaseId($caseId) {
+    $this->_caseId = $caseId;
+  }
+
+  /**
+   * @return int
+   */
+  public function getCaseId() {
+    return $this->_caseId;
+  }
+
+  /**
+   * Explicitly declare the entity api name.
+   *
+   * @return string
+   */
+  public function getDefaultEntity() {
+    return 'Relationship';
+  }
+
+  /**
+   * Explicitly declare the form context.
+   *
+   * @return string|null
+   */
+  public function getDefaultContext() {
+    return 'search';
+  }
 
   /**
    * View details of a relationship.
    */
   public function view() {
-    $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId, NULL, NULL, NULL, $this->_id);
+    $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->getContactId(), NULL, NULL, NULL, $this->getEntityId());
     //To check whether selected contact is a contact_id_a in
     //relationship type 'a_b' in relationship table, if yes then
     //revert the permissionship text in template
     $relationship = new CRM_Contact_DAO_Relationship();
-    $relationship->id = $viewRelationship[$this->_id]['id'];
+    $relationship->id = $viewRelationship[$this->getEntityId()]['id'];
 
     if ($relationship->find(TRUE)) {
-      if (($viewRelationship[$this->_id]['rtype'] == 'a_b') && ($this->_contactId == $relationship->contact_id_a)) {
+      if (($viewRelationship[$this->getEntityId()]['rtype'] == 'a_b') && ($this->getContactId() == $relationship->contact_id_a)) {
         $this->assign("is_contact_id_a", TRUE);
       }
     }
-    $relType = $viewRelationship[$this->_id]['civicrm_relationship_type_id'];
+    $relType = $viewRelationship[$this->getEntityId()]['civicrm_relationship_type_id'];
     $this->assign('viewRelationship', $viewRelationship);
 
-    $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id');
+    $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->getContactId(), 'employer_id');
     $this->assign('isCurrentEmployer', FALSE);
 
     $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
 
-    if ($viewRelationship[$this->_id]['employer_id'] == $this->_contactId) {
+    if ($viewRelationship[$this->getEntityId()]['employer_id'] == $this->getContactId()) {
       $this->assign('isCurrentEmployer', TRUE);
     }
     elseif ($relType == $relTypes['Employee of']['id'] &&
-      ($viewRelationship[$this->_id]['cid'] == $employerId)
+      ($viewRelationship[$this->getEntityId()]['cid'] == $employerId)
     ) {
       // make sure we are viewing employee of relationship
       $this->assign('isCurrentEmployer', TRUE);
     }
 
-    $viewNote = CRM_Core_BAO_Note::getNote($this->_id);
+    $viewNote = CRM_Core_BAO_Note::getNote($this->getEntityId());
     $this->assign('viewNote', $viewNote);
 
-    $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', NULL, $this->_id, 0, $relType);
-    CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->_id);
+    $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', NULL, $this->getEntityId(), 0, $relType);
+    CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->getEntityId());
 
-    $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->_id]);
+    $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->getEntityId()]);
     // add viewed contribution to recent items list
     $url = CRM_Utils_System::url('civicrm/contact/view/rel',
-      "action=view&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&context=home"
+      "action=view&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&context=home"
     );
 
     $session = CRM_Core_Session::singleton();
     $recentOther = array();
 
-    if (($session->get('userID') == $this->_contactId) ||
-      CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)
+    if (($session->get('userID') == $this->getContactId()) ||
+      CRM_Contact_BAO_Contact_Permission::allow($this->getContactId(), CRM_Core_Permission::EDIT)
     ) {
       $recentOther = array(
         'editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
-          "action=update&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
+          "action=update&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home"
         ),
         'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel',
-          "action=delete&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"
+          "action=delete&reset=1&id={$viewRelationship[$this->getEntityId()]['id']}&cid={$this->getContactId()}&rtype={$rType}&context=home"
         ),
       );
     }
 
-    $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
+    $displayName = CRM_Contact_BAO_Contact::displayName($this->getContactId());
     $this->assign('displayName', $displayName);
     CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName);
 
-    $title = $displayName . ' (' . $viewRelationship[$this->_id]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->_id]['cid']) . ')';
+    $title = $displayName . ' (' . $viewRelationship[$this->getEntityId()]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->getEntityId()]['cid']) . ')';
 
     // add the recently viewed Relationship
     CRM_Utils_Recent::add($title,
       $url,
-      $viewRelationship[$this->_id]['id'],
+      $viewRelationship[$this->getEntityId()]['id'],
       'Relationship',
-      $this->_contactId,
+      $this->getContactId(),
       NULL,
       $recentOther
     );
@@ -141,70 +165,59 @@ class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
    *
    */
   public function edit() {
-    $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->_action);
+    $controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Relationship', ts('Contact Relationships'), $this->getAction());
     $controller->setEmbedded(TRUE);
 
     // set the userContext stack
     $session = CRM_Core_Session::singleton();
 
     // if this is called from case view, we need to redirect back to same page
-    if ($this->_caseId) {
-      $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->_contactId}&id={$this->_caseId}");
+    if ($this->getCaseId()) {
+      $url = CRM_Utils_System::url('civicrm/contact/view/case', "action=view&reset=1&cid={$this->getContactId()}&id={$this->getCaseId()}");
     }
     else {
-      $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->_contactId}");
+      $url = CRM_Utils_System::url('civicrm/contact/view', "action=browse&selectedChild=rel&reset=1&cid={$this->getContactId()}");
     }
 
     $session->pushUserContext($url);
 
     if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
-      if ($this->_caseId) {
+      if ($this->getCaseId()) {
         //create an activity for case role removal.CRM-4480
-        CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $this->_id);
+        CRM_Case_BAO_Case::createCaseRoleActivity($this->getCaseId(), $this->getEntityId());
         CRM_Core_Session::setStatus(ts('Case Role has been deleted successfully.'), ts('Record Deleted'), 'success');
       }
 
       // delete relationship
-      CRM_Contact_BAO_Relationship::del($this->_id);
+      CRM_Contact_BAO_Relationship::del($this->getEntityId());
 
       CRM_Utils_System::redirect($url);
     }
 
-    $controller->set('contactId', $this->_contactId);
-    $controller->set('id', $this->_id);
+    $controller->set('contactId', $this->getContactId());
+    $controller->set('id', $this->getEntityId());
     $controller->process();
     $controller->run();
   }
 
-  public function preProcess() {
-    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
-    $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
-    $this->assign('contactId', $this->_contactId);
-
-    // check logged in url permission
-    CRM_Contact_Page_View::checkUserPermission($this);
-
-    $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
-    $this->assign('action', $this->_action);
-  }
-
   /**
    * the main function that is called when the page loads,
    * it decides the which action has to be taken for the page.
    *
    * @return null
+   * @throws \CRM_Core_Exception
    */
   public function run() {
-    $this->preProcess();
+    $this->preProcessQuickEntityPage();
 
     $this->setContext();
 
-    $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this);
+    $this->setCaseId(CRM_Utils_Request::retrieve('caseID', 'Integer', $this));
 
-    if ($this->_action & CRM_Core_Action::VIEW) {
+    if ($this->isViewContext()) {
       $this->view();
     }
-    elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
+    elseif ($this->isEditContext() || $this->isDeleteContext()) {
       $this->edit();
     }
 
@@ -217,17 +230,8 @@ class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
   }
 
   public function setContext() {
-    $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric',
-      $this, FALSE, 'search'
-    );
-
-    if ($context == 'dashboard') {
-      $cid = CRM_Utils_Request::retrieve('cid', 'Integer',
-        $this, FALSE
-      );
-      $url = CRM_Utils_System::url('civicrm/user',
-        "reset=1&id={$cid}"
-      );
+    if ($this->getContext() == 'dashboard') {
+      $url = CRM_Utils_System::url('civicrm/user', "reset=1&id={$this->getContactId()}");
     }
     else {
       $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=rel');
@@ -242,7 +246,7 @@ class CRM_Contact_Page_View_Relationship extends CRM_Core_Page {
    */
   public function delete() {
     // calls a function to delete relationship
-    CRM_Contact_BAO_Relationship::del($this->_id);
+    CRM_Contact_BAO_Relationship::del($this->getEntityId());
   }
 
   /**
diff --git a/CRM/Core/Page/EntityPageTrait.php b/CRM/Core/Page/EntityPageTrait.php
new file mode 100644 (file)
index 0000000..f5ff786
--- /dev/null
@@ -0,0 +1,189 @@
+<?php
+/*
+  +--------------------------------------------------------------------+
+  | CiviCRM version 5                                                  |
+  +--------------------------------------------------------------------+
+  | Copyright CiviCRM LLC (c) 2004-2019                                |
+  +--------------------------------------------------------------------+
+  | This file is a part of CiviCRM.                                    |
+  |                                                                    |
+  | CiviCRM is free software; you can copy, modify, and distribute it  |
+  | under the terms of the GNU Affero General Public License           |
+  | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+  |                                                                    |
+  | CiviCRM is distributed in the hope that it will be useful, but     |
+  | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+  | See the GNU Affero General Public License for more details.        |
+  |                                                                    |
+  | You should have received a copy of the GNU Affero General Public   |
+  | License and the CiviCRM Licensing Exception along                  |
+  | with this program; if not, contact CiviCRM LLC                     |
+  | at info[AT]civicrm[DOT]org. If you have questions about the        |
+  | GNU Affero General Public License or the licensing of CiviCRM,     |
+  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+  +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2019
+ */
+trait CRM_Core_Page_EntityPageTrait {
+
+  /**
+   * Pages MUST declare the following functions:
+   * public function getDefaultEntity() {
+   */
+
+  /**
+   * Page MAY want to override the following functions:
+   * public function getDefaultContext()
+   */
+
+  /**
+   * The id of the contact.
+   *
+   * @var int
+   */
+  protected $_id;
+
+  /**
+   * The mode of operation for this page
+   *
+   * @var int
+   */
+  protected $_action;
+
+  /**
+   * The context that we are working on.
+   *
+   * @var string
+   */
+  protected $_context;
+
+  /**
+   * Contact ID of the contact on the page.
+   *
+   * @var int
+   */
+  public $_contactID = NULL;
+
+  /**
+   * Contact ID of the contact on the page.
+   *
+   * @var int
+   * @deprecated Historically pages alternate between $_contactID and $_contactId. We'll standardise on one
+   */
+  public $_contactId = NULL;
+
+  /**
+   * @var int
+   */
+  public $_permission = NULL;
+
+  /**
+   * The action links that we need to display for the browse screen.
+   *
+   * @var array
+   */
+  public static $_links = NULL;
+
+  /**
+   * Get the entity id being edited.
+   *
+   * @return int|null
+   */
+  public function getEntityId() {
+    return $this->_id;
+  }
+
+  /**
+   * Get the context we are working in
+   *
+   * @return string
+   */
+  public function getContext() {
+    return $this->_context;
+  }
+
+  /**
+   * Get the contact ID
+   *
+   * @return int
+   */
+  public function getContactId() {
+    return $this->_contactID;
+  }
+
+  /**
+   * Set the contact ID
+   *
+   * @param $contactId
+   */
+  public function setContactId($contactId) {
+    $this->_contactID = $contactId;
+    $this->_contactId = $contactId;
+  }
+
+  public function getAction() {
+    return $this->_action;
+  }
+
+  /**
+   * Explicitly declare the form context.
+   *
+   * @return string|null
+   */
+  public function getDefaultContext() {
+    return NULL;
+  }
+
+  public function preProcessQuickEntityPage() {
+    $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
+    $this->assign('action', $this->getAction());
+
+    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
+    $this->setContactId(CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE));
+    $this->assign('contactId', $this->getContactId());
+
+    $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, $this->getDefaultContext());
+    $this->assign('context', $this->_context);
+
+    // check logged in url permission
+    CRM_Contact_Page_View::checkUserPermission($this);
+
+    $this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
+  }
+
+  /**
+   * Is the form being used in the context of a deletion.
+   *
+   * (For some reason rather than having separate forms Civi overloads one form).
+   *
+   * @return bool
+   */
+  protected function isDeleteContext() {
+    return ($this->getAction() & CRM_Core_Action::DELETE);
+  }
+
+  /**
+   * Is the form being used in the context of a view.
+   *
+   * @return bool
+   */
+  protected function isViewContext() {
+    return ($this->getAction() & CRM_Core_Action::VIEW);
+  }
+
+  /**
+   * Is the form being used in the context of a edit.
+   *
+   * @return bool
+   */
+  protected function isEditContext() {
+    return ($this->getAction() & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD));
+  }
+
+}