From 6ea7a22921b0a322745d349c62b986523e848dcf Mon Sep 17 00:00:00 2001 From: Deepak Srivastava Date: Tue, 25 Jun 2013 16:16:19 +0530 Subject: [PATCH] CRM-12877, splitting instance postprocess in BAO::create and add, and exposing hooks ---------------------------------------- * CRM-12877: Provide api for creating report instances http://issues.civicrm.org/jira/browse/CRM-12877 --- CRM/Report/BAO/Instance.php | 194 ++++++++++++++++++++++++++++++++++- CRM/Report/Form/Instance.php | 165 +++-------------------------- 2 files changed, 207 insertions(+), 152 deletions(-) diff --git a/CRM/Report/BAO/Instance.php b/CRM/Report/BAO/Instance.php index 627daae59f..857fca272b 100644 --- a/CRM/Report/BAO/Instance.php +++ b/CRM/Report/BAO/Instance.php @@ -35,6 +35,199 @@ */ class CRM_Report_BAO_Instance extends CRM_Report_DAO_Instance { + /** + * takes an associative array and creates an instance object + * + * the function extract all the params it needs to initialize the create a + * instance object. the params array could contain additional unused name/value + * pairs + * + * @param array $params (reference ) an assoc array of name/value pairs + * + * @return object CRM_Report_DAO_Instance object + * @access public + * @static + */ + static function add(&$params) { + $instance = new CRM_Report_DAO_Instance(); + if (empty($params)) { + return NULL; + } + + $config = CRM_Core_Config::singleton(); + $params['domain_id'] = CRM_Core_Config::domainID(); + + // convert roles array to string + if (isset($params['grouprole']) && is_array($params['grouprole'])) { + $grouprole_array = array(); + foreach ($params['grouprole'] as $key => $value) { + $grouprole_array[$value] = $value; + } + $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, + array_keys($grouprole_array) + ); + } + + $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); + + $instanceID = CRM_Utils_Array::value('id', $params); + if (CRM_Utils_Array::value('instance_id', $params)) { + $instanceID = CRM_Utils_Array::value('instance_id', $params); + } + + if ($instanceID) { + CRM_Utils_Hook::pre('edit', 'ReportInstance', $instanceID, $params); + } + else { + CRM_Utils_Hook::pre('create', 'ReportInstance', NULL, $params); + } + + $instance = new CRM_Report_DAO_Instance(); + $instance->copyValues($params); + + if ($config->userFramework == 'Joomla') { + $instance->permission = 'null'; + } + + // explicitly set to null if params value is empty + if (empty($params['grouprole'])) { + $instance->grouprole = 'null'; + } + + if ($instanceID) { + $instance->id = $instanceID; + } + + if ($reportID = CRM_Utils_Array::value('report_id', $params)) { + $instance->report_id = $reportID; + } else if ($instanceID) { + $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID); + } else { + // just take it from current url + $instance->report_id = CRM_Report_Utils_Report::getValueFromUrl(); + } + + // unset params that doesn't match with DB columns, and also not required in form-values for sure + $fields = array( + 'title', 'to_emails', 'cc_emails', 'header', 'footer', + 'qfKey', '_qf_default', 'report_header', 'report_footer', 'grouprole', + ); + foreach ($fields as $field) { + unset($params[$field]); + } + $instance->form_values = serialize($params); + $instance->save(); + + if ($instanceID) { + CRM_Utils_Hook::pre('edit', 'ReportInstance', $instance->id, $instance); + } + else { + CRM_Utils_Hook::pre('create', 'ReportInstance', $instance->id, $instance); + } + return $instance; + } + + /** + * Function to create instance + * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc. + * + * This function is invoked from within the web form layer and also from the api layer + * + * @param array $params (reference ) an assoc array of name/value pairs + * + * @return object CRM_Report_BAO_Instance object + * @access public + * @static + */ + static function &create(&$params) { + $params['header'] = CRM_Utils_Array::value('report_header',$params); + $params['footer'] = CRM_Utils_Array::value('report_footer',$params); + + // build navigation parameters + if (CRM_Utils_Array::value('is_navigation', $params)) { + if (!array_key_exists('navigation', $params)) { + $params['navigation'] = array(); + } + $navigationParams =& $params['navigation']; + + $navigationParams['permission'] = array(); + $navigationParams['label'] = $params['title']; + $navigationParams['name'] = $params['title']; + + $navigationParams['current_parent_id'] = CRM_Utils_Array::value('parent_id', $navigationParams); + $navigationParams['parent_id'] = CRM_Utils_Array::value('parent_id', $params); + $navigationParams['is_active'] = 1; + + if ($permission = CRM_Utils_Array::value('permission', $params)) { + $navigationParams['permission'][] = $permission; + } + + // unset the navigation related elements, not used in report form values + unset($params['parent_id']); + unset($params['is_navigation']); + } + + // add to dashboard + $dashletParams = array(); + if (CRM_Utils_Array::value('addToDashboard', $params)) { + $dashletParams = array( + 'label' => $params['title'], + 'is_active' => 1, + ); + if ($permission = CRM_Utils_Array::value('permission', $params)) { + $dashletParams['permission'][] = $permission; + } + } + + $transaction = new CRM_Core_Transaction(); + + $instance = self::add($params); + if (is_a($instance, 'CRM_Core_Error')) { + $transaction->rollback(); + return $instance; + } + + // add / update navigation as required + if (!empty($navigationParams)) { + if (!CRM_Utils_Array::value('id',$params) && + !CRM_Utils_Array::value('instance_id',$params) && + CRM_Utils_Array::value('id', $navigationParams)) { + unset($navigationParams['id']); + } + $navigationParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1"; + $navigation = CRM_Core_BAO_Navigation::add($navigationParams); + + if (CRM_Utils_Array::value('is_active', $navigationParams)) { + //set the navigation id in report instance table + CRM_Core_DAO::setFieldValue('CRM_Report_DAO_Instance', $instance->id, 'navigation_id', $navigation->id); + } + else { + // has been removed from the navigation bar + CRM_Core_DAO::setFieldValue('CRM_Report_DAO_Instance', $instance->id, 'navigation_id', 'NULL'); + } + //reset navigation + CRM_Core_BAO_Navigation::resetNavigation(); + } + + // add to dashlet + if (!empty($dashletParams)) { + $section = 2; + $chart = ''; + if (CRM_Utils_Array::value('charts', $params)) { + $section = 1; + $chart = "&charts=" . $params['charts']; + } + + $dashletParams['url'] = "civicrm/report/instance/{$instance->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashlet"; + $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen"; + $dashletParams['instanceURL'] = "civicrm/report/instance/{$instance->id}"; + CRM_Core_BAO_Dashboard::addDashlet($dashletParams); + } + $transaction->commit(); + + return $instance; + } + /** * Delete the instance of the Report * @@ -60,4 +253,3 @@ class CRM_Report_BAO_Instance extends CRM_Report_DAO_Instance { return NULL; } } - diff --git a/CRM/Report/Form/Instance.php b/CRM/Report/Form/Instance.php index df0c21953b..4152f1ff57 100644 --- a/CRM/Report/Form/Instance.php +++ b/CRM/Report/Form/Instance.php @@ -241,170 +241,33 @@ class CRM_Report_Form_Instance { } static function postProcess(&$form, $redirect = TRUE) { - $params = $form->getVar('_params'); - $config = CRM_Core_Config::singleton(); - $params['header'] = CRM_Utils_Array::value('report_header',$params); - $params['footer'] = CRM_Utils_Array::value('report_footer',$params); - $params['domain_id'] = CRM_Core_Config::domainID(); - - //navigation parameters - if (CRM_Utils_Array::value('is_navigation', $params)) { - $form->_navigation['permission'] = array(); - $form->_navigation['label'] = $params['title']; - $form->_navigation['name'] = $params['title']; - - $permission = CRM_Utils_Array::value('permission', $params); - - $form->_navigation['current_parent_id'] = CRM_Utils_Array::value('parent_id', $form->_navigation); - $form->_navigation['parent_id'] = CRM_Utils_Array::value('parent_id', $params); - $form->_navigation['is_active'] = 1; - - if ($permission) { - $form->_navigation['permission'][] = $permission; - } - //unset the navigation related element, - //not used in report form values - unset($params['parent_id']); - unset($params['is_navigation']); - } - - // convert roles array to string - if (isset($params['grouprole']) && is_array($params['grouprole'])) { - $grouprole_array = array(); - foreach ($params['grouprole'] as $key => $value) { - $grouprole_array[$value] = $value; - } - $params['grouprole'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, - array_keys($grouprole_array) - ); - } - - // add to dashboard - $dashletParams = array(); - if (CRM_Utils_Array::value('addToDashboard', $params)) { - $dashletParams = array( - 'label' => $params['title'], - 'is_active' => 1, - ); - - $permission = CRM_Utils_Array::value('permission', $params); - if ($permission) { - $dashletParams['permission'][] = $permission; - } - } - $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); - - $dao = new CRM_Report_DAO_Instance(); - $dao->copyValues($params); - - if ($config->userFramework == 'Joomla') { - $dao->permission = 'null'; - } - - // explicitly set to null if params value is empty - if (empty($params['grouprole'])) { - $dao->grouprole = 'null'; - } - - // unset all the params that we use - $fields = array( - 'title', 'to_emails', 'cc_emails', 'header', 'footer', - 'qfKey', '_qf_default', 'report_header', 'report_footer', 'grouprole', - ); - foreach ($fields as $field) { - unset($params[$field]); - } - $dao->form_values = serialize($params); - + $params = $form->getVar('_params'); $instanceID = $form->getVar('_id'); - $isNew = $form->getVar('_createNew'); - $isCopy = 0; - if ($instanceID) { - if (!$isNew) { - // updating an existing report instance - $dao->id = $instanceID; - } else { - // making a copy of an existing instance - $isCopy = 1; - } - } - - $dao->report_id = CRM_Report_Utils_Report::getValueFromUrl($instanceID); - - $dao->save(); - - $form->set('id', $dao->id); - - if (!empty($form->_navigation)) { - if ($isNew && CRM_Utils_Array::value('id', $form->_navigation)) { - unset($form->_navigation['id']); - } - $form->_navigation['url'] = "civicrm/report/instance/{$dao->id}&reset=1"; - $navigation = CRM_Core_BAO_Navigation::add($form->_navigation); - - if (CRM_Utils_Array::value('is_active', $form->_navigation)) { - //set the navigation id in report instance table - CRM_Core_DAO::setFieldValue('CRM_Report_DAO_Instance', $dao->id, 'navigation_id', $navigation->id); - } - else { - // has been removed from the navigation bar - CRM_Core_DAO::setFieldValue('CRM_Report_DAO_Instance', $dao->id, 'navigation_id', 'NULL'); - } - - //reset navigation - CRM_Core_BAO_Navigation::resetNavigation(); + if ($isNew = $form->getVar('_createNew')) { + // unset $instanceID so a new copy would be created + $instanceID = NULL; } - - // add to dashlet - if (!empty($dashletParams)) { - $section = 2; - $chart = ''; - if (CRM_Utils_Array::value('charts', $params)) { - $section = 1; - $chart = "&charts=" . $params['charts']; - } - - $dashletParams['url'] = "civicrm/report/instance/{$dao->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashlet"; - $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$dao->id}&reset=1§ion={$section}&snippet=5{$chart}&context=dashletFullscreen"; - $dashletParams['instanceURL'] = "civicrm/report/instance/{$dao->id}"; - CRM_Core_BAO_Dashboard::addDashlet($dashletParams); + $params['instance_id'] = $instanceID; + if (CRM_Utils_Array::value('is_navigation', $params)) { + $params['navigation'] = $form->_navigation; } - $instanceParams = array('value' => $dao->report_id); - $instanceDefaults = array(); - $cmpName = "Contact"; - $statusMsg = "null"; - CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_OptionValue', - $instanceParams, - $instanceDefaults - ); - - if ($cmpID = CRM_Utils_Array::value('component_id', $instanceDefaults)) { - $cmpName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Component', $cmpID, - 'name', 'id' - ); - $cmpName = substr($cmpName, 4); - } + $instance = CRM_Report_BAO_Instance::create($params); + $form->set('id', $instance->id); if ($instanceID && !$isNew) { // updating existing instance - $statusMsg = ts('"%1" report has been updated.', array(1 => $dao->title)); - } elseif ($isCopy) { - $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $dao->title)); + $statusMsg = ts('"%1" report has been updated.', array(1 => $instance->title)); + } elseif ($form->getVar('_id') && $isNew) { + $statusMsg = ts('Your report has been successfully copied as "%1". You are currently viewing the new copy.', array(1 => $instance->title)); } else { - $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $dao->title)); + $statusMsg = ts('"%1" report has been successfully created. You are currently viewing the new report instance.', array(1 => $instance->title)); } - - $instanceUrl = CRM_Utils_System::url("civicrm/report/instance/{$dao->id}", - "reset=1" - ); - CRM_Core_Session::setStatus($statusMsg); if ( $redirect ) { - CRM_Utils_System::redirect($instanceUrl); + CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/report/instance/{$instance->id}", "reset=1")); } } } - -- 2.25.1