dev/core#1241 : Custom field value for a new Event based on an Event Template are...
[civicrm-core.git] / CRM / Event / Form / ManageEvent / EventInfo.php
index 31317c09aa8d0fff18bea5c9a54815b1f1b79ca2..b11e8d50f3b44e4d9580c2f1e38fcc140ef2e334 100644 (file)
@@ -46,12 +46,13 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
    */
   public function preProcess() {
     parent::preProcess();
-    $this->assign('selectedChild', 'settings');
+    $this->setSelectedChild('settings');
 
-    if ($this->_id) {
-      $this->assign('entityID', $this->_id);
+    $entityID = $this->_id ?: $this->_templateId;
+    if ($entityID) {
+      $this->assign('entityID', $entityID);
       $eventType = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
-        $this->_id,
+        $entityID,
         'event_type_id'
       );
     }
@@ -65,9 +66,9 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
       $this->set('type', 'Event');
       $this->set('subType', CRM_Utils_Array::value('event_type_id', $_POST));
       $this->assign('customDataSubType', CRM_Utils_Array::value('event_type_id', $_POST));
-      $this->set('entityId', $this->_id);
+      $this->set('entityId', $entityID);
 
-      CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_eventType, 1, 'Event', $this->_id);
+      CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_eventType, 1, 'Event', $entityID);
       CRM_Custom_Form_CustomData::buildQuickForm($this);
       CRM_Custom_Form_CustomData::setDefaultValues($this);
     }
@@ -127,7 +128,6 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
     if ($this->_eventType) {
       $this->assign('customDataSubType', $this->_eventType);
     }
-    $this->assign('entityId', $this->_id);
 
     $this->_first = TRUE;
     $this->applyFilter('__ALL__', 'trim');
@@ -228,7 +228,7 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
    * Process the form submission.
    */
   public function postProcess() {
-    $params = $this->controller->exportValues($this->_name);
+    $params = array_merge($this->controller->exportValues($this->_name), $this->_submitValues);
 
     //format params
     $params['start_date'] = CRM_Utils_Array::value('start_date', $params);
@@ -241,19 +241,23 @@ class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
     $params['default_role_id'] = CRM_Utils_Array::value('default_role_id', $params, FALSE);
     $params['id'] = $this->_id;
 
-    $customFields = CRM_Core_BAO_CustomField::getFields('Event', FALSE, FALSE,
-      CRM_Utils_Array::value('event_type_id', $params)
-    );
-    $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
-      $this->_id,
-      'Event'
-    );
-
     //merge params with defaults from templates
     if (!empty($params['template_id'])) {
       $params = array_merge(CRM_Event_BAO_Event::getTemplateDefaultValues($params['template_id']), $params);
+      foreach ($params as $key => $value) {
+        $customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE);
+        if (!empty($customFieldInfo[1])) {
+          $params[str_replace($customFieldInfo[1], '-' . $customFieldInfo[1], $key)] = $value;
+          unset($params[$key]);
+        }
+      }
     }
 
+    $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
+      $this->_id,
+      'Event'
+    );
+
     // now that we have the event’s id, do some more template-based stuff
     if (!empty($params['template_id'])) {
       $event = CRM_Event_BAO_Event::copy($params['template_id'], $params);