Merge pull request #4087 from robinmitra/master
[civicrm-core.git] / CRM / Case / Form / CaseView.php
index 9e2dd8676850e8019f744540b970bdfb90d0a7fe..7a2ca49d0bfc8afbfd51d5b6d67d1f516ff20a05 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
  *
  */
 class CRM_Case_Form_CaseView extends CRM_Core_Form {
-  /*
-     * check for merge cases.
-     */
-
+  /**
+   * Check for merge cases.
+   * @var bool
+   */
   private $_mergeCases = FALSE;
 
   /**
-   * Function to set variables up before form is built
+   * Set variables up before form is built
    *
    * @return void
    * @access public
@@ -113,20 +113,13 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     $returnProperties = array('case_type_id', 'subject', 'status_id', 'start_date');
     CRM_Core_DAO::commonRetrieve('CRM_Case_BAO_Case', $params, $values, $returnProperties);
 
-    $values['case_type_id'] = trim(CRM_Utils_Array::value('case_type_id', $values),
-      CRM_Core_DAO::VALUE_SEPARATOR
-    );
-    $values['case_type_id'] = explode(CRM_Core_DAO::VALUE_SEPARATOR,
-      CRM_Utils_Array::value('case_type_id', $values)
-    );
-
     $statuses     = CRM_Case_PseudoConstant::caseStatus('label', FALSE);
     $caseTypeName = CRM_Case_BAO_Case::getCaseType($this->_caseID, 'name');
     $caseType     = CRM_Case_BAO_Case::getCaseType($this->_caseID);
 
     $this->_caseDetails = array(
       'case_type' => $caseType,
-      'case_status' => $statuses[$values['case_status_id']],
+      'case_status' => CRM_Utils_Array::value($values['case_status_id'], $statuses),
       'case_subject' => CRM_Utils_Array::value('subject', $values),
       'case_start_date' => $values['case_start_date'],
     );
@@ -186,7 +179,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
       )));
     }
 
-    $entitySubType = !empty($values['case_type_id']) ? $values['case_type_id'][0] : NULL;
+    $entitySubType = !empty($values['case_type_id']) ? $values['case_type_id'] : NULL;
     $this->assign('caseTypeID', $entitySubType);
     $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Case',
       $this,
@@ -200,7 +193,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
   }
 
   /**
-   * This function sets the default values for the form. For edit/view mode
+   * Set default values for the form. For edit/view mode
    * the default values are retrieved from the database
    *
    * @access public
@@ -213,7 +206,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
   }
 
   /**
-   * Function to build the form
+   * Build the form object
    *
    * @return void
    * @access public
@@ -225,8 +218,8 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     }
 
     CRM_Core_Resources::singleton()
-      ->addScriptFile('civicrm', 'js/crm.livePage.js')
-      ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js');
+      ->addScriptFile('civicrm', 'js/crm.livePage.js', 1, 'html-header')
+      ->addScriptFile('civicrm', 'templates/CRM/Case/Form/CaseView.js', 2, 'html-header');
 
     $xmlProcessor = new CRM_Case_XMLProcessor_Process();
     $caseRoles    = $xmlProcessor->get($this->_caseType, 'CaseRoles');
@@ -243,6 +236,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     $allActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name');
 
     $emailActivityType = array_search('Email', $allActTypes);
+    $pdfActivityType = array_search('Print PDF Letter', $allActTypes);
 
     // remove Open Case activity type since we're inside an existing case
     if ($openActTypeId = array_search('Open Case', $allActTypes)) {
@@ -267,6 +261,11 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
           FALSE, NULL, FALSE
         );
       }
+      else if ($type == $pdfActivityType ) {
+         $url = CRM_Utils_System::url('civicrm/activity/pdf/add',
+          "action=add&context=standalone&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
+          FALSE, NULL, FALSE );
+      }
       else {
         $url = CRM_Utils_System::url('civicrm/case/activity',
           "action=add&reset=1&cid={$this->_contactID}&caseid={$this->_caseID}&atype=$type",
@@ -276,23 +275,20 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
       $activityLinks[$url] = $label;
     }
 
-    $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu'));
+    $this->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu action-icon-plus twenty'));
     if ($this->_hasAccessToAllCases) {
-      $this->add('select', 'report_id', ts('Run QA Audit / Redact'),
-        array(
-          '' => ts('- select activity set -')) + $reports
+      $this->add('select', 'report_id', '',
+        array('' => ts('Activity Audit')) + $reports,
+        FALSE,
+        array('class' => 'crm-select2 crm-action-menu action-icon-clipboard')
       );
-      $this->add('select', 'timeline_id', ts('Add Timeline'),
-        array(
-          '' => ts('- select activity set -')) + $reports
+      $this->add('select', 'timeline_id', '',
+        array('' => ts('Add Timeline')) + $reports,
+        FALSE,
+        array('class' => 'crm-select2 crm-action-menu action-icon-play')
       );
     }
-    $this->addElement('submit', $this->getButtonName('next'), ts('Go'),
-      array(
-        'class' => 'form-submit-inline',
-        'onclick' => "return checkSelection( this );",
-      )
-    );
+    $this->addElement('submit', $this->getButtonName('next'), ' ', array('class' => 'hiddenElement'));
 
     if ($this->_mergeCases) {
       $allCases = CRM_Case_BAO_Case::getContactCases($this->_contactID);
@@ -317,29 +313,22 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
         $this->add('select', 'merge_case_id',
           ts('Select Case for Merge'),
           array(
-            '' => ts('- select case -')) + $otherCases
+            '' => ts('- select case -')) + $otherCases,
+          FALSE,
+          array('class' => 'crm-select2 huge')
         );
         $this->addElement('submit',
           $this->getButtonName('next', 'merge_case'),
           ts('Merge'),
           array(
-            'class' => 'form-submit-inline',
-            'onclick' => "return checkSelection( this );",
+            'class' => 'crm-form-submit-inline hiddenElement',
           )
         );
       }
     }
 
-    $activityStatus = CRM_Core_PseudoConstant::activityStatus();
-    $this->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) + $activityStatus);
-
-    // activity dates
-    $this->addDate('activity_date_low', ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
-    $this->addDate('activity_date_high', ts('To'), FALSE, array('formatType' => 'searchDate'));
-
-    if (CRM_Core_Permission::check('administer CiviCRM')) {
-      $this->add('checkbox', 'activity_deleted', ts('Deleted Activities'));
-    }
+    //call activity form
+    self::activityForm($this, $aTypes);
 
     //get case related relationships (Case Role)
     $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($this->_contactID, $this->_caseID);
@@ -347,11 +336,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     //save special label because we unset it in the loop
     $managerLabel = empty($managerRoleId) ? '' : $caseRoles[$managerRoleId];
 
-    //build reporter select
-    $reporters = array("" => ts(' - any reporter - '));
     foreach ($caseRelationships as $key => & $value) {
-      $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
-
       if (!empty($managerRoleId)) {
         if ($managerRoleId == $value['relation_type']) {
           $value['relation'] = $managerLabel;
@@ -364,18 +349,6 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
       }
     }
 
-    // take all case activity types for search filter, CRM-7187
-    $aTypesFilter = array();
-    $allCaseActTypes = CRM_Case_PseudoConstant::caseActivityType();
-    foreach ($allCaseActTypes as $typeDetails) {
-      if (!in_array($typeDetails['name'], array(
-        'Open Case'))) {
-        $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array::value('label', $typeDetails);
-      }
-    }
-    asort($aTypesFilter);
-    $this->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) + $aTypesFilter);
-
     $this->assign('caseRelationships', $caseRelationships);
 
     //also add client as role. CRM-4438
@@ -383,8 +356,6 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
 
     $this->assign('caseRoles', $caseRoles);
 
-    $this->add('select', 'reporter_id', ts('Reporter/Role'), $reporters);
-
     // Retrieve ALL client relationships
     $relClient = CRM_Contact_BAO_Relationship::getRelationship($this->_contactID,
       CRM_Contact_BAO_Relationship::CURRENT,
@@ -403,8 +374,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
 
     // Now global contact list that appears on all cases.
     $globalGroupInfo = array();
-    $relGlobal = CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
-    $this->assign('globalRelationships', $relGlobal);
+    CRM_Case_BAO_Case::getGlobalContacts($globalGroupInfo);
     $this->assign('globalGroupInfo', $globalGroupInfo);
 
     // List of relationship types
@@ -414,7 +384,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     foreach ($relType as $k => $v) {
       $roleTypes[substr($k, 0, strpos($k, '_'))] = $v;
     }
-    $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) + $roleTypes);
+    $this->add('select', 'role_type', ts('Relationship Type'), array('' => ts('- select type -')) + $roleTypes, FALSE, array('class' => 'crm-select2 twenty'));
 
     $hookCaseSummary = CRM_Utils_Hook::caseSummary($this->_caseID);
     if (is_array($hookCaseSummary)) {
@@ -459,7 +429,7 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
     else {
       $this->assign('showTagsets', FALSE);
     }
-    CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID, TRUE, TRUE);
+    CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_case', $this->_caseID, FALSE, TRUE);
 
     $this->addButtons(array(
         array(
@@ -527,5 +497,42 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form {
       $session->pushUserContext($url);
     }
   }
-}
 
+  /**
+   * Build the activity selector/datatable
+   * @param CRM_Core_Form $form
+   * @param array $aTypes to include acivities related to current case id $form->_caseID
+   */
+  static function activityForm($form, $aTypes = array()) {
+    $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($form->_contactID, $form->_caseID);
+    //build reporter select
+    $reporters = array("" => ts(' - any reporter - '));
+    foreach ($caseRelationships as $key => & $value) {
+      $reporters[$value['cid']] = $value['name'] . " ( {$value['relation']} )";
+    }
+    $form->add('select', 'reporter_id', ts('Reporter/Role'), $reporters, FALSE, array('id' => 'reporter_id_'.$form->_caseID));
+
+    // take all case activity types for search filter, CRM-7187
+    $aTypesFilter = array();
+    $allCaseActTypes = CRM_Case_PseudoConstant::caseActivityType();
+    foreach ($allCaseActTypes as $typeDetails) {
+      if (!in_array($typeDetails['name'], array('Open Case'))) {
+        $aTypesFilter[$typeDetails['id']] = CRM_Utils_Array::value('label', $typeDetails);
+      }
+    }
+    $aTypesFilter = $aTypesFilter + $aTypes;
+    asort($aTypesFilter);
+    $form->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) + $aTypesFilter, FALSE, array('id' => 'activity_type_filter_id_'.$form->_caseID));
+
+    $activityStatus = CRM_Core_PseudoConstant::activityStatus();
+    $form->add('select', 'status_id', ts('Status'), array("" => ts(' - any status - ')) + $activityStatus, FALSE, array('id' => 'status_id_'.$form->_caseID));
+
+    // activity dates
+    $form->addDate('activity_date_low_'.$form->_caseID, ts('Activity Dates - From'), FALSE, array('formatType' => 'searchDate'));
+    $form->addDate('activity_date_high_'.$form->_caseID, ts('To'), FALSE, array('formatType' => 'searchDate'));
+
+    if (CRM_Core_Permission::check('administer CiviCRM')) {
+      $form->add('checkbox', 'activity_deleted', ts('Deleted Activities'), '', FALSE, array('id' => 'activity_deleted_'.$form->_caseID));
+    }
+  }
+}