Skip to content
Snippets Groups Projects
Commit e62c1198 authored by bgm's avatar bgm Committed by Aegir user
Browse files

Add Settings form, and settings to add the report title/date at the top of the report.

parent 494404dd
No related branches found
No related tags found
No related merge requests found
<?php
use CRM_CiviExportExcel_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
*/
class CRM_CiviExportExcel_Form_ExportExcelSettings extends CRM_Core_Form {
private $_settingFilter = ['group' => 'civiexportexcel'];
private $_formurl = 'civicrm/admin/setting/exportexcel';
private $_submittedValues = [];
private $_settings = [];
/**
* Get the settings we are going to allow to be set on this form.
*
* @return array
*/
function getFormSettings() {
if (empty($this->_settings)) {
$settings = civicrm_api3('setting', 'getfields', array('filters' => $this->_settingFilter));
}
return $settings['values'];
}
public function buildQuickForm() {
$settings = $this->getFormSettings();
$descriptions = [];
foreach ($settings as $name => $setting) {
if (isset($setting['quick_form_type'])) {
$add = 'add' . $setting['quick_form_type'];
if ($add == 'addElement') {
$attributes = CRM_Utils_Array::value('html_attributes', $setting, []);
$this->$add($setting['html_type'], $name, $setting['title'], $attributes);
}
elseif ($setting['html_type'] == 'Date') {
$attributes = CRM_Utils_Array::value('html_attributes', $setting, []);
$this->add('text', $name, $setting['title'], $attributes);
$e = $this->getElement($name);
$e->setAttribute('type', 'date');
}
elseif ($setting['html_type'] == 'Select') {
$optionValues = [
'' => ts('- select -'),
];
if (!empty($setting['pseudoconstant'])) {
if (!empty($setting['pseudoconstant']['optionGroupName'])) {
$optionValues = CRM_Core_OptionGroup::values($setting['pseudoconstant']['optionGroupName'], FALSE, FALSE, FALSE, NULL, 'name');
}
elseif (!empty($setting['pseudoconstant']['api_entity'])) {
if (!empty($setting['pseudoconstant']['api_field'])) {
$t = civicrm_api3($setting['pseudoconstant']['api_entity'], 'get', [
'option.limit' => 0,
])['values'];
foreach ($t as $key => $val) {
$field = $setting['pseudoconstant']['api_field'];
$optionValues[$key] = $val[$field];
}
}
elseif (!empty($setting['pseudoconstant']['api_getoptions'])) {
$optionValues += civicrm_api3($setting['pseudoconstant']['api_entity'], 'getoptions', [
'field' => $setting['pseudoconstant']['api_getoptions'],
'option.limit' => 0,
])['values'];
}
}
}
elseif (!empty($setting['select_options'])) {
$optionValues = $setting['select_options'];
}
$this->add('select', $setting['name'], $setting['title'], $optionValues, FALSE, $setting['html_attributes']);
}
else {
$this->$add($name, $setting['title']);
}
if (!empty($setting['description'])) {
$elementDescriptions[$setting['name']] = $setting['description'];
}
}
$this->assign("elementDescriptions", $elementDescriptions);
}
$this->addButtons(array(
array (
'type' => 'submit',
'name' => ts('Submit'),
'isDefault' => TRUE,
)
));
// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
parent::buildQuickForm();
}
public function postProcess() {
$values = $this->exportValues();
$settings = $this->getFormSettings();
$values = array_intersect_key($values, $settings);
civicrm_api3('setting', 'create', $values);
parent::postProcess();
CRM_Utils_System::redirect(CRM_Utils_System::url($this->_formurl, 'reset=1'));
}
/**
* Set defaults for form.
*
* @see CRM_Core_Form::setDefaultValues()
*/
function setDefaultValues() {
$existing = civicrm_api3('setting', 'get', array('return' => array_keys($this->getFormSettings())));
$defaults = array();
$domainID = CRM_Core_Config::domainID();
foreach ($existing['values'][$domainID] as $name => $value) {
$defaults[$name] = $value;
}
return $defaults;
}
/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
public function getRenderableElementNames() {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = array();
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}
<?php
use CRM_Civiexportexcel_ExtensionUtil as E;
use CRM_CiviExportExcel_ExtensionUtil as E;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
......@@ -110,12 +110,40 @@ class CRM_CiviExportExcel_Utils_Report extends CRM_Core_Page {
}
}
// Row counter
$cpt = 1;
$first_data_row = 1;
// Used for later calculating the column widths
$widths = [];
// Add some report metadata
$add_blank_because_of_meta = FALSE;
if (Civi::settings()->get('civiexportexcel_show_title')) {
$cell = 'A' . $cpt;
$objPHPExcel->getActiveSheet()
->setCellValue($cell, $title);
$cpt++;
$add_blank_because_of_meta = TRUE;
}
if (Civi::settings()->get('civiexportexcel_show_export_date')) {
$cell = 'A' . $cpt;
$objPHPExcel->getActiveSheet()
->setCellValue($cell, E::ts('Date prepared: %1', [1 => date('Y-m-d H:i')]));
$cpt++;
$add_blank_because_of_meta = TRUE;
}
if ($add_blank_because_of_meta) {
$cpt++;
}
// Add the column headers.
$col = 0;
$cpt = 1;
foreach ($headers as $h) {
$cell = $cells[$col] . $cpt;
......@@ -129,9 +157,13 @@ class CRM_CiviExportExcel_Utils_Report extends CRM_Core_Page {
$col++;
}
// Add rows.
$cpt = 2;
// Headers row
$cpt++;
// Used later for row height recalculations.
$first_data_row = $cpt;
// Exported data
foreach ($rows as $row) {
$displayRows = array();
$col = 0;
......@@ -221,7 +253,7 @@ class CRM_CiviExportExcel_Utils_Report extends CRM_Core_Page {
// Now set row heights (skip the header)
$sheet = $objPHPExcel->getActiveSheet();
for ($i = 2; $i < $cpt; $i++) {
for ($i = $first_data_row; $i < $cpt; $i++) {
$row = new Row($sheet, $i);
self::autofitRowHeight($row);
}
......
......@@ -6,10 +6,10 @@
* The ExtensionUtil class provides small stubs for accessing resources of this
* extension.
*/
class CRM_Civiexportexcel_ExtensionUtil {
class CRM_CiviExportExcel_ExtensionUtil {
const SHORT_NAME = "civiexportexcel";
const LONG_NAME = "ca.bidon.civiexportexcel";
const CLASS_PREFIX = "CRM_Civiexportexcel";
const CLASS_PREFIX = "CRM_CiviExportExcel";
/**
* Translate a string using the extension's domain.
......@@ -77,7 +77,7 @@ class CRM_Civiexportexcel_ExtensionUtil {
}
use CRM_Civiexportexcel_ExtensionUtil as E;
use CRM_CiviExportExcel_ExtensionUtil as E;
/**
* (Delegated) Implements hook_civicrm_config().
......@@ -205,14 +205,14 @@ function _civiexportexcel_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NU
}
/**
* @return CRM_Civiexportexcel_Upgrader
* @return CRM_CiviExportExcel_Upgrader
*/
function _civiexportexcel_civix_upgrader() {
if (!file_exists(__DIR__ . '/CRM/Civiexportexcel/Upgrader.php')) {
if (!file_exists(__DIR__ . '/CRM/CiviExportExcel/Upgrader.php')) {
return NULL;
}
else {
return CRM_Civiexportexcel_Upgrader_Base::instance();
return CRM_CiviExportExcel_Upgrader_Base::instance();
}
}
......@@ -269,10 +269,10 @@ function _civiexportexcel_civix_civicrm_managed(&$entities) {
if (empty($e['module'])) {
$e['module'] = E::LONG_NAME;
}
$entities[] = $e;
if (empty($e['params']['version'])) {
$e['params']['version'] = '3';
}
$entities[] = $e;
}
}
}
......
<?php
require_once 'civiexportexcel.civix.php';
use CRM_Civiexportexcel_ExtensionUtil as E;
use CRM_CiviExportExcel_ExtensionUtil as E;
require_once(__DIR__ . '/vendor/autoload.php');
......@@ -72,6 +72,13 @@ function civiexportexcel_civicrm_managed(&$entities) {
return _civiexportexcel_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*/
function civiexportexcel_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_civiexportexcel_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implementation of hook_civicrm_buildForm().
*
......
<?php
use CRM_CiviExportExcel_ExtensionUtil as E;
return [
'civiexportexcel_show_title' => [
'group_name' => 'domain',
'group' => 'civiexportexcel',
'name' => 'civiexportexcel_show_title',
'type' => 'Boolean',
'default' => 0,
'add' => '1.0',
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('Show the report title'),
'description' => E::ts("Prints the report title as the first row of the report."),
'quick_form_type' => 'YesNo',
],
'civiexportexcel_show_export_date' => [
'group_name' => 'domain',
'group' => 'civiexportexcel',
'name' => 'civiexportexcel_show_export_date',
'type' => 'Boolean',
'default' => 0,
'add' => '1.0',
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('Show the report export date'),
'description' => E::ts("Prints the export date at the top of the report."),
'quick_form_type' => 'YesNo',
],
];
{foreach from=$elementNames item=elementName}
<div class="crm-section">
<div class="label">{$form.$elementName.label}</div>
<div class="content">
{$form.$elementName.html}
{if $elementDescriptions.$elementName}
<div class="description">{$elementDescriptions.$elementName}</div>
{/if}
</div>
<div class="clear"></div>
</div>
{/foreach}
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
<?xml version="1.0"?>
<menu>
<item>
<path>civicrm/admin/setting/exportexcel</path>
<page_callback>CRM_CiviExportExcel_Form_ExportExcelSettings</page_callback>
<title>ExportExcelSettings</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
</menu>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment