support for multiple recurring activities
[civicrm-core.git] / CRM / Activity / Form / Activity.php
index 5251de5cca6321ad48e885f170a88e8ef9855840..d806d7722fadd1b5847001f282482e5ca1b8356f 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -125,9 +125,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   /**
    * The _fields var can be used by sub class to set/unset/edit the
    * form fields based on their requirement
-   *
    */
-  function setFields() {
+  public function setFields() {
     $this->_fields = array(
       'subject' => array(
         'type' => 'text',
@@ -146,14 +145,14 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
         'type' => 'text',
         'label' => ts('Location'),
         'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'location'),
-        'required' => FALSE
+        'required' => FALSE,
       ),
       'details' => array(
         'type' => 'wysiwyg',
         'label' => ts('Details'),
         // forces a smaller edit window
         'attributes' => array('rows' => 4, 'cols' => 60),
-        'required' => FALSE
+        'required' => FALSE,
       ),
       'status_id' => array(
         'type' => 'select',
@@ -166,22 +165,30 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       'source_contact_id' => array(
         'type' => 'entityRef',
         'label' => ts('Added By'),
-        'required' => FALSE
+        'required' => FALSE,
       ),
       'target_contact_id' => array(
         'type' => 'entityRef',
         'label' => ts('With Contact'),
-        'attributes' => array('multiple' => TRUE, 'create' => TRUE)
+        'attributes' => array('multiple' => TRUE, 'create' => TRUE),
       ),
       'assignee_contact_id' => array(
         'type' => 'entityRef',
         'label' => ts('Assigned to'),
-        'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
+        'attributes' => array(
+          'multiple' => TRUE,
+          'create' => TRUE,
+          'api' => array('params' => array('is_deceased' => 0)),
+        ),
       ),
       'followup_assignee_contact_id' => array(
         'type' => 'entityRef',
         'label' => ts('Assigned to'),
-        'attributes' => array('multiple' => TRUE, 'create' => TRUE, 'api' => array('params' => array('is_deceased' => 0))),
+        'attributes' => array(
+          'multiple' => TRUE,
+          'create' => TRUE,
+          'api' => array('params' => array('is_deceased' => 0)),
+        ),
       ),
       'followup_activity_type_id' => array(
         'type' => 'select',
@@ -195,8 +202,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
         'label' => ts('Subject'),
         'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
           'subject'
-        )
-      )
+        ),
+      ),
     );
 
     if (($this->_context == 'standalone') &&
@@ -210,9 +217,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
    * Build the form object
    *
    * @return void
-   * @access public
    */
-  function preProcess() {
+  public function preProcess() {
     $this->_cdType = CRM_Utils_Array::value('type', $_GET);
     $this->assign('cdType', FALSE);
     if ($this->_cdType) {
@@ -280,7 +286,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     if ($this->_activityId &&
       in_array($this->_action, array(
         CRM_Core_Action::UPDATE,
-        CRM_Core_Action::VIEW
+        CRM_Core_Action::VIEW,
       )) &&
       !CRM_Activity_BAO_Activity::checkPermission($this->_activityId, $this->_action)
     ) {
@@ -311,7 +317,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     // Set title
     if (isset($activityTName)) {
       $activityName = CRM_Utils_Array::value($this->_activityTypeId, $activityTName);
-      $this->assign('pageTitle', ts('%1 Activity', array( 1 => $activityName)));
+      $this->assign('pageTitle', ts('%1 Activity', array(1 => $activityName)));
 
       if ($this->_currentlyViewedContactId) {
         $displayName = CRM_Contact_BAO_Contact::displayName($this->_currentlyViewedContactId);
@@ -322,7 +328,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
         CRM_Utils_System::setTitle($displayName . ' - ' . $activityName);
       }
       else {
-        CRM_Utils_System::setTitle(ts('%1 Activity', array( 1 => $activityName)));
+        CRM_Utils_System::setTitle(ts('%1 Activity', array(1 => $activityName)));
       }
     }
 
@@ -418,7 +424,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       'standalone',
       'home',
       'dashlet',
-      'dashletFullscreen'
+      'dashletFullscreen',
     ))
     ) {
       $urlParams = 'reset=1';
@@ -430,14 +436,15 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
         $urlParams .= "&qfKey=$qfKey";
       }
       $path = CRM_Utils_System::currentPath();
-      if ($this->_compContext == 'advanced' ) {
+      if ($this->_compContext == 'advanced') {
         $urlString = 'civicrm/contact/search/advanced';
       }
       elseif ($path == 'civicrm/group/search'
-       || $path == 'civicrm/contact/search'
+        || $path == 'civicrm/contact/search'
         || $path == 'civicrm/contact/search/advanced'
         || $path == 'civicrm/contact/search/custom'
-        || $path == 'civicrm/group/search') {
+        || $path == 'civicrm/group/search'
+      ) {
         $urlString = $path;
       }
       else {
@@ -477,8 +484,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
     // figure out the file name for activity type, if any
     if ($this->_activityTypeId &&
-      $this->_activityTypeFile =
-        CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId, $this->_crmDir)
+      $this->_activityTypeFile = CRM_Activity_BAO_Activity::getFileForActivityTypeId($this->_activityTypeId, $this->_crmDir)
     ) {
       $this->assign('activityTypeFile', $this->_activityTypeFile);
       $this->assign('crmDir', $this->_crmDir);
@@ -510,11 +516,10 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
    * Set default values for the form. For edit/view mode
    * the default values are retrieved from the database
    *
-   * @access public
    *
    * @return void
    */
-  function setDefaultValues() {
+  public function setDefaultValues() {
     if ($this->_cdType) {
       return CRM_Custom_Form_CustomData::setDefaultValues($this);
     }
@@ -560,8 +565,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       $defaults['source_contact_id'] = $this->_sourceContactId;
       $defaults['target_contact_id'] = $this->_targetContactId;
 
-      list($defaults['activity_date_time'], $defaults['activity_date_time_time']) =
-        CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
+      list($defaults['activity_date_time'], $defaults['activity_date_time_time'])
+        CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
     }
 
     if ($this->_activityTypeId) {
@@ -612,12 +617,12 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
           'type' => 'next',
           'name' => $button,
           'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
-          'isDefault' => TRUE
+          'isDefault' => TRUE,
         ),
         array(
           'type' => 'cancel',
-          'name' => ts('Cancel')
-        )
+          'name' => ts('Cancel'),
+        ),
       ));
       return;
     }
@@ -632,7 +637,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     //enable form element (ActivityLinks sets this true)
     $this->assign('suppressForm', FALSE);
 
-    $element = & $this->add('select', 'activity_type_id', ts('Activity Type'),
+    $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
       $this->_fields['followup_activity_type_id']['attributes'],
       FALSE, array(
         'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
@@ -679,7 +684,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       CRM_Campaign_BAO_Campaign::accessCampaign()
     ) {
       $buildEngagementLevel = TRUE;
-      $this->addSelect('engagement_level',array('entity' => 'activity'));
+      $this->addSelect('engagement_level', array('entity' => 'activity'));
       $this->addRule('engagement_level',
         ts('Please enter the engagement index as a number (integers only).'),
         'positiveInteger'
@@ -767,7 +772,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       $buttons = array();
       $buttons[] = array(
         'type' => 'cancel',
-        'name' => ts('Done')
+        'name' => ts('Done'),
       );
       $this->addButtons($buttons);
     }
@@ -782,12 +787,12 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
             'type' => 'upload',
             'name' => ts('Save'),
             'js' => $js,
-            'isDefault' => TRUE
+            'isDefault' => TRUE,
           ),
           array(
             'type' => 'cancel',
-            'name' => ts('Cancel')
-          )
+            'name' => ts('Cancel'),
+          ),
         )
       );
     }
@@ -816,15 +821,16 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   /**
    * Global form rule
    *
-   * @param array $fields the input form values
-   * @param array $files the uploaded files if any
+   * @param array $fields
+   *   The input form values.
+   * @param array $files
+   *   The uploaded files if any.
    * @param $self
    *
-   * @return true if no errors, else array of errors
-   * @access public
-   * @static
+   * @return bool|array
+   *   true if no errors, else array of errors
    */
-  static function formRule($fields, $files, $self) {
+  public static function formRule($fields, $files, $self) {
     // skip form rule if deleting
     if (CRM_Utils_Array::value('_qf_Activity_next_', $fields) == 'Delete') {
       return TRUE;
@@ -863,7 +869,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   /**
    * Process the form submission
    *
-   * @access public
    *
    * @param array $params
    * @return array|null
@@ -877,13 +882,13 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       // delete tags for the entity
       $tagParams = array(
         'entity_table' => 'civicrm_activity',
-        'entity_id' => $this->_activityId
+        'entity_id' => $this->_activityId,
       );
 
       CRM_Core_BAO_EntityTag::del($tagParams);
 
       CRM_Core_Session::setStatus(ts("Selected Activity has been deleted successfully."), ts('Record Deleted'), 'success');
-      return;
+      return NULL;
     }
 
     // store the submitted values in an array
@@ -959,39 +964,42 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       $activity = $this->processActivity($params);
     }
 
-    //Set for repeat configuration in create mode
-    $params['entity_id'] = $this->_activityId;
-    $params['entity_table'] = 'civicrm_activity';
-    $scheduleReminderDetails = array();
-    if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
-      $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($params['entity_id'], $params['entity_table']);
-      if ($checkParentExistsForThisId) {
-        $params['parent_entity_id'] = $checkParentExistsForThisId;
-        $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
-      }
-      else {
-        $params['parent_entity_id'] = $params['entity_id'];
-        $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
-      }
-      if (property_exists($scheduleReminderDetails, 'id')) {
-        $params['schedule_reminder_id'] = $scheduleReminderDetails->id;
+    $activityIds = empty($this->_activityIds) ? array($this->_activityId) : $this->_activityIds;
+    foreach ($activityIds as $activityId) {
+      // set params for repeat configuration in create mode
+      $params['entity_id'] = $activityId;
+      $params['entity_table'] = 'civicrm_activity';
+      $scheduleReminderDetails = array();
+      if (!empty($params['entity_id']) && !empty($params['entity_table'])) {
+        $checkParentExistsForThisId = CRM_Core_BAO_RecurringEntity::getParentFor($params['entity_id'], $params['entity_table']);
+        if ($checkParentExistsForThisId) {
+          $params['parent_entity_id'] = $checkParentExistsForThisId;
+          $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($checkParentExistsForThisId, $params['entity_table']);
+        }
+        else {
+          $params['parent_entity_id'] = $params['entity_id'];
+          $scheduleReminderDetails = CRM_Core_BAO_RecurringEntity::getReminderDetailsByEntityId($params['entity_id'], $params['entity_table']);
+        }
+        if (property_exists($scheduleReminderDetails, 'id')) {
+          $params['schedule_reminder_id'] = $scheduleReminderDetails->id;
+        }
       }
-    }
-    $params['dateColumns'] = array('activity_date_time');
+      $params['dateColumns'] = array('activity_date_time');
 
-    //Unset activity id
-    unset($params['id']);
-    $linkedEntities = array(
-      array(
-        'table'         => 'civicrm_activity_contact',
-        'findCriteria'  => array(
-          'activity_id' => $this->_activityId,
+      // unset activity id
+      unset($params['id']);
+      $linkedEntities = array(
+        array(
+          'table' => 'civicrm_activity_contact',
+          'findCriteria' => array(
+            'activity_id' => $activityId,
+          ),
+          'linkedColumns' => array('activity_id'),
+          'isRecurringEntityRecord' => FALSE,
         ),
-        'linkedColumns' => array('activity_id'),
-        'isRecurringEntityRecord' => FALSE,
-      )
-    );
-    CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_activity', $linkedEntities);
+      );
+      CRM_Core_Form_RecurringEntity::postProcess($params, 'civicrm_activity', $linkedEntities);
+    }
 
     return array('activity' => $activity);
   }
@@ -999,10 +1007,10 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   /**
    * Process activity creation
    *
-   * @param array $params associated array of submitted values
+   * @param array $params
+   *   Associated array of submitted values.
    *
-   * @return $this|null|object
-   * @access protected
+   * @return self|null|object
    */
   protected function processActivity(&$params) {
     $activityAssigned = array();
@@ -1065,7 +1073,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     $mailStatus = '';
 
     if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-        'activity_assignee_notification')) {
+      'activity_assignee_notification')
+    ) {
       $activityIDs = array($activity->id);
       if ($followupActivity) {
         $activityIDs = array_merge($activityIDs, array($followupActivity->id));
@@ -1131,7 +1140,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       array(
         1 => $subject,
         2 => $followupStatus,
-        3 => $mailStatus
+        3 => $mailStatus,
       )
     ), ts('Saved'), 'success');
 
@@ -1140,8 +1149,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
   /**
    * Shorthand for getting id by display name (makes code more readable)
-   *
-   * @access protected
+   * @param $displayName
+   * @return null|string
    */
   protected function _getIdByDisplayName($displayName) {
     return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
@@ -1153,8 +1162,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
   /**
    * Shorthand for getting display name by id (makes code more readable)
-   *
-   * @access protected
+   * @param $id
+   * @return null|string
    */
   protected function _getDisplayNameById($id) {
     return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
@@ -1170,7 +1179,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
    *
    * @param array $params
    */
-  function beginPostProcess(&$params) {
+  public function beginPostProcess(&$params) {
     if ($this->_activityTypeFile) {
       $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
       $className::beginPostProcess($this, $params);
@@ -1184,11 +1193,11 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
    * @param array $params
    * @param $activity
    */
-  function endPostProcess(&$params, &$activity) {
+  public function endPostProcess(&$params, &$activity) {
     if ($this->_activityTypeFile) {
       $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
       $className::endPostProcess($this, $params, $activity);
     }
   }
-}
 
+}