Merge pull request #4635 from colemanw/comments
[civicrm-core.git] / CRM / Contact / Form / CustomData.php
index 76f87fc1d3c6e3b60775390cb2000cdab6eb234e..8d9d3641cd007229aaf2d1488881f496b622174b 100644 (file)
@@ -51,14 +51,14 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
   protected $_tableId;
 
   /**
-   * entity type of the table id
+   * Entity type of the table id
    *
    * @var string
    */
   protected $_entityType;
 
   /**
-   * entity sub type of the table id
+   * Entity sub type of the table id
    *
    * @var string
    * @access protected
@@ -66,7 +66,7 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
   protected $_entitySubType;
 
   /**
-   * the group tree data
+   * The group tree data
    *
    * @var array
    */
@@ -94,7 +94,7 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
   protected $_groupCollapseDisplay;
 
   /**
-   * custom group id
+   * Custom group id
    *
    * @int
    * @access public
@@ -105,7 +105,7 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
 
   public $_copyValueId;
   /**
-   * pre processing work done here.
+   * Pre processing work done here.
    *
    * gets session variables for table name, id of entity in table, type of entity and stores them.
    *
@@ -127,15 +127,26 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
       // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func
       // this is due to some condition inside it which restricts it from saving in session
       // so doing this for multi record edit action
-      CRM_Custom_Form_CustomData::preProcess($this);
+      $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this);
+      if(!empty($entityId)) {
+        $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ',');
+      }
+      CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
       if ($this->_multiRecordDisplay) {
         $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this);
         $this->_tableID = $this->_entityId;
+        $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
+        $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
+        $hasReachedMax = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($this->_groupID, $this->_tableID);
+        if ($hasReachedMax && $mode == 'add') {
+          CRM_Core_Error::statusBounce(ts('The maximum record limit is reached'));
+        }
         $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this);
 
-        $backUrl = 'civicrm/contact/view';
-        $backUrl = CRM_Utils_System::url($backUrl, 'action=browse&selectedChild=custom_' . $this->_groupID, TRUE, NULL, FALSE);
-        $this->assign('backUrl', $backUrl);
+        $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupID);
+        $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET');
+        $mode = ucfirst($mode);
+        CRM_Utils_System::setTitle(ts('%1 %2 Record', array(1 => $mode, 2 => $groupTitle)));
 
         if (!empty($_POST['hidden_custom'])) {
           $this->assign('postedInfo', TRUE);
@@ -164,7 +175,7 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
   }
 
   /**
-   * Function to actually build the form
+   * Build the form object
    *
    * @return void
    * @access public
@@ -186,6 +197,11 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
                 'name' => ts('%1', array(1 => $saveButtonName)),
                 'isDefault' => TRUE,
               ),
+              array(
+                'type' => 'upload',
+                'name' => ts('Save and New'),
+                'subName' => 'new',
+              ),
               array(
                 'type' => 'cancel',
                 'name' => ts('Cancel'),
@@ -285,13 +301,20 @@ class CRM_Contact_Form_CustomData extends CRM_Core_Form {
   public function postProcess() {
     // Get the form values and groupTree
     $params = $this->controller->exportValues($this->_name);
+
     CRM_Core_BAO_CustomValueTable::postProcess($params,
       $this->_groupTree[$this->_groupID]['fields'],
       'civicrm_contact',
       $this->_tableID,
       $this->_entityType
     );
-
+    $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name');
+    $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE);
+    $cgcount += 1;
+    $buttonName = $this->controller->getButtonName();
+    if ($buttonName == $this->getButtonName('upload', 'new')) {
+      CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/cd/edit', "reset=1&type={$this->_contactType}&groupID={$this->_groupID}&entityID={$this->_tableID}&cgcount={$cgcount}&multiRecordDisplay=single&mode=add"));
+    }
     // reset the group contact cache for this group
     CRM_Contact_BAO_GroupContactCache::remove();
   }