Merge branch 4.6 into master
[civicrm-core.git] / CRM / Activity / Form / Activity.php
index 4f6fad8acccd8d34b36683f9f3d84a60a025b5b8..1fe07df7cb32008c2e2513839f22bba1484893ac 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | 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-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
    */
@@ -111,24 +111,29 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
    */
   protected $_crmDir = 'Activity';
 
-  /*
-     * Survey activity
-     *
-     * @var boolean
-     */
-
+  /**
+   * Survey activity.
+   *
+   * @var boolean
+   */
   protected $_isSurveyActivity;
 
   protected $_values = array();
 
   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
-   *
    */
-  function setFields() {
+  public function setFields() {
     $this->_fields = array(
       'subject' => array(
         'type' => 'text',
@@ -146,18 +151,15 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       'location' => array(
         'type' => 'text',
         'label' => ts('Location'),
-        'attributes' =>
-        CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity',
-          'location'
-        ),
-        'required' => FALSE
+        'attributes' => CRM_Core_DAO::getAttribute('CRM_Activity_DAO_Activity', 'location'),
+        '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',
@@ -170,22 +172,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))),
+        'label' => ts('Assigned to'),
+        '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))),
+        'label' => ts('Assigned to'),
+        'attributes' => array(
+          'multiple' => TRUE,
+          'create' => TRUE,
+          'api' => array('params' => array('is_deceased' => 0)),
+        ),
       ),
       'followup_activity_type_id' => array(
         'type' => 'select',
@@ -199,8 +209,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') &&
@@ -211,19 +221,18 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Function to build the form
+   * 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) {
       $this->assign('cdType', TRUE);
       return CRM_Custom_Form_CustomData::preProcess($this);
     }
-
+    CRM_Core_Form_RecurringEntity::preProcess('civicrm_activity');
     $this->_atypefile = CRM_Utils_Array::value('atypefile', $_GET);
     $this->assign('atypefile', FALSE);
     if ($this->_atypefile) {
@@ -259,7 +268,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
 
     if ($this->_action & CRM_Core_Action::DELETE) {
       if (!CRM_Core_Permission::check('delete activities')) {
-        CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
+        CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
       }
     }
 
@@ -284,7 +293,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)
     ) {
@@ -315,7 +324,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);
@@ -326,7 +335,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)));
       }
     }
 
@@ -422,7 +431,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       'standalone',
       'home',
       'dashlet',
-      'dashletFullscreen'
+      'dashletFullscreen',
     ))
     ) {
       $urlParams = 'reset=1';
@@ -434,13 +443,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/contact/search'
+      elseif ($path == 'civicrm/group/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 {
@@ -480,8 +491,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);
@@ -503,22 +513,25 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       }
       $this->set('values', $this->_values);
     }
+
+    if ($this->_action & CRM_Core_Action::UPDATE) {
+      CRM_Core_Form_RecurringEntity::preProcess('civicrm_activity');
+    }
   }
 
   /**
-   * 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
    *
    * @return void
    */
-  function setDefaultValues() {
+  public function setDefaultValues() {
     if ($this->_cdType) {
       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'])) {
@@ -529,6 +542,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
         list($defaults['activity_date_time'],
           $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');
       }
 
       if ($this->_context != 'standalone') {
@@ -555,8 +569,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) {
@@ -607,12 +621,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;
     }
@@ -627,11 +641,11 @@ 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'),
-      $this->_fields['followup_activity_type_id']['attributes'],
+    $element = &$this->add('select', 'activity_type_id', ts('Activity Type'),
+      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',
       )
     );
 
@@ -640,6 +654,11 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       $element->freeze();
     }
 
+    //Call to RecurringEntity buildQuickForm for add/update mode
+    if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
+      CRM_Core_Form_RecurringEntity::buildQuickForm($this);
+    }
+
     foreach ($this->_fields as $field => $values) {
       if (!empty($this->_fields[$field])) {
         $attribute = CRM_Utils_Array::value('attributes', $values);
@@ -669,7 +688,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'
@@ -728,7 +747,8 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
     $this->assign('customDataSubType', $this->_activityTypeId);
     $this->assign('entityID', $this->_activityId);
 
-    $tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
+    CRM_Core_BAO_Tag::getTags('civicrm_activity', $tags, NULL,
+      '&nbsp;&nbsp;', TRUE);
 
     if (!empty($tags)) {
       $this->add('select', 'tag', ts('Tags'), $tags, FALSE,
@@ -756,7 +776,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);
     }
@@ -771,12 +791,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'),
+          ),
         )
       );
     }
@@ -803,33 +823,27 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * global form rule
+   * 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
    *
-   * @internal param array $options additional user data
-   *
-   * @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;
     }
     $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
     ) {
@@ -852,12 +866,11 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Function to process the form
+   * Process the form submission.
    *
-   * @access public
    *
-   * @param null $params
-   * @return void
+   * @param array $params
+   * @return array|null
    */
   public function postProcess($params = NULL) {
     if ($this->_action & CRM_Core_Action::DELETE) {
@@ -868,13 +881,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
@@ -950,16 +963,57 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       $activity = $this->processActivity($params);
     }
 
+    $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;
+        }
+      }
+      $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'];
+      }
+
+      // unset activity id
+      unset($params['id']);
+      $linkedEntities = array(
+        array(
+          'table' => 'civicrm_activity_contact',
+          'findCriteria' => array(
+            'activity_id' => $activityId,
+          ),
+          'linkedColumns' => array('activity_id'),
+          'isRecurringEntityRecord' => FALSE,
+        ),
+      );
+      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
+   * @param array $params
+   *   Associated array of submitted values.
    *
-   * @return $this|null|object
-   * @access protected
+   * @return self|null|object
    */
   protected function processActivity(&$params) {
     $activityAssigned = array();
@@ -1022,7 +1076,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));
@@ -1088,7 +1143,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       array(
         1 => $subject,
         2 => $followupStatus,
-        3 => $mailStatus
+        3 => $mailStatus,
       )
     ), ts('Saved'), 'success');
 
@@ -1097,8 +1152,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',
@@ -1110,8 +1165,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',
@@ -1122,11 +1177,12 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Function to 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
    */
-  function beginPostProcess(&$params) {
+  public function beginPostProcess(&$params) {
     if ($this->_activityTypeFile) {
       $className = "CRM_{$this->_crmDir}_Form_Activity_{$this->_activityTypeFile}";
       $className::beginPostProcess($this, $params);
@@ -1134,15 +1190,17 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
   }
 
   /**
-   * Function to let injecting activity type file do any processing
+   * Let injecting activity type file do any processing
    * needed, after the activity has been added/updated
    *
+   * @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 );
+      $className::endPostProcess($this, $params, $activity);
     }
   }
-}
 
+}