Merge branch 4.6 into master
[civicrm-core.git] / CRM / Activity / Form / Activity.php
index 99d080e3441071149f09b73e9bf9e4f57508facc..1fe07df7cb32008c2e2513839f22bba1484893ac 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
@@ -47,35 +47,35 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   public $_activityId;
 
   /**
-   * Store activity ids when multiple activities are created
+   * Store activity ids when multiple activities are created.
    *
    * @var int
    */
   public $_activityIds = array();
 
   /**
-   * The id of activity type
+   * The id of activity type.
    *
    * @var int
    */
   public $_activityTypeId;
 
   /**
-   * The name of activity type
+   * The name of activity type.
    *
    * @var string
    */
   public $_activityTypeName;
 
   /**
-   * The id of currently viewed contact
+   * The id of currently viewed contact.
    *
    * @var int
    */
   public $_currentlyViewedContactId;
 
   /**
-   * The id of source contact and target contact
+   * The id of source contact and target contact.
    *
    * @var int
    */
@@ -98,7 +98,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   public $_currentUserId;
 
   /**
-   * The array of form field attributes
+   * The array of form field attributes.
    *
    * @var array
    */
@@ -112,7 +112,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   protected $_crmDir = 'Activity';
 
   /**
-   * Survey activity
+   * Survey activity.
    *
    * @var boolean
    */
@@ -122,6 +122,13 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
   protected $unsavedWarn = TRUE;
 
+  /**
+   * Explicitly declare the entity api name.
+   */
+  public function getDefaultEntity() {
+    return 'Activity';
+  }
+
   /**
    * The _fields var can be used by sub class to set/unset/edit the
    * form fields based on their requirement
@@ -214,7 +221,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    * @return void
    */
@@ -524,7 +531,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       return CRM_Custom_Form_CustomData::setDefaultValues($this);
     }
 
-    $defaults = $this->_values;
+    $defaults = $this->_values + CRM_Core_Form_RecurringEntity::setDefaultValues();
     // if we're editing...
     if (isset($this->_activityId)) {
       if (empty($defaults['activity_date_time'])) {
@@ -536,9 +543,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
           $defaults['activity_date_time_time']
           ) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
         list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
-        $recurringEntityDefaults = array();
-        $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues();
-        $defaults = array_merge($defaults, $recurringEntityDefaults);
       }
 
       if ($this->_context != 'standalone') {
@@ -638,10 +642,10 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     $this->assign('suppressForm', FALSE);
 
     $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
-      $this->_fields['followup_activity_type_id']['attributes'],
+      array('' => '- ' . ts('select') . ' -') + $this->_fields['followup_activity_type_id']['attributes'],
       FALSE, array(
         'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
-        'class' => 'crm-select2',
+        'class' => 'crm-select2 required',
       )
     );
 
@@ -819,7 +823,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Global form rule
+   * Global form rule.
    *
    * @param array $fields
    *   The input form values.
@@ -836,15 +840,10 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       return TRUE;
     }
     $errors = array();
-    if (!$self->_single && !$fields['activity_type_id']) {
+    if ((array_key_exists('activity_type_id', $fields) || !$self->_single) && empty($fields['activity_type_id'])) {
       $errors['activity_type_id'] = ts('Activity Type is a required field');
     }
 
-    //Activity type is mandatory if creating new activity, CRM-4515
-    if (array_key_exists('activity_type_id', $fields) && empty($fields['activity_type_id'])) {
-      $errors['activity_type_id'] = ts('Activity Type is required field.');
-    }
-
     if (CRM_Utils_Array::value('activity_type_id', $fields) == 3 &&
       CRM_Utils_Array::value('status_id', $fields) == 1
     ) {
@@ -867,7 +866,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Process the form submission
+   * Process the form submission.
    *
    *
    * @param array $params
@@ -964,45 +963,52 @@ 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']);
+    $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';
+      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;
+        }
       }
-      if (property_exists($scheduleReminderDetails, 'id')) {
-        $params['schedule_reminder_id'] = $scheduleReminderDetails->id;
+      $params['dateColumns'] = array('activity_date_time');
+
+      // Set default repetition start if it was not provided.
+      if (empty($params['repetition_start_date'])) {
+        $params['repetition_start_date'] = $params['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);
   }
 
   /**
-   * Process activity creation
+   * Process activity creation.
    *
    * @param array $params
    *   Associated array of submitted values.
@@ -1171,7 +1177,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Let injecting activity type file do any processing
+   * Let injecting activity type file do any processing.
    * needed, before the activity is added/updated
    *
    * @param array $params