|
From: <gem...@li...> - 2012-09-26 10:33:46
|
Revision: 957
http://gemstracker.svn.sourceforge.net/gemstracker/?rev=957&view=rev
Author: mennodekker
Date: 2012-09-26 10:33:36 +0000 (Wed, 26 Sep 2012)
Log Message:
-----------
Some further optimization of excel export
Modified Paths:
--------------
trunk/library/classes/Gems/Controller/BrowseEditAction.php
trunk/library/classes/Gems/FormattedData.php
trunk/library/classes/Gems/View/Helper/Excel.php
Modified: trunk/library/classes/Gems/Controller/BrowseEditAction.php
===================================================================
--- trunk/library/classes/Gems/Controller/BrowseEditAction.php 2012-09-25 15:34:45 UTC (rev 956)
+++ trunk/library/classes/Gems/Controller/BrowseEditAction.php 2012-09-26 10:33:36 UTC (rev 957)
@@ -439,7 +439,7 @@
$this->_applySearchParameters($model, true);
$this->addExcelColumns($model); // Hook to modify the model
-
+
// Use $this->formatExcelData to switch between formatted and unformatted data
$excelData = new Gems_FormattedData($this->getExcelData($model->load(), $model), $model, $this->formatExcelData);
@@ -695,10 +695,7 @@
if ($headings) {
if ($data) {
foreach ($data as $row) {
- foreach ($headings as $key => $value) {
- $result[$key] = isset($row[$key]) ? $row[$key] : null;
- }
- $results[] = $result;
+ $results[] = array_intersect_key($row, $headings);
}
return $results;
} else {
@@ -997,4 +994,4 @@
$this->html[] = $table;
}
-}
+}
\ No newline at end of file
Modified: trunk/library/classes/Gems/FormattedData.php
===================================================================
--- trunk/library/classes/Gems/FormattedData.php 2012-09-25 15:34:45 UTC (rev 956)
+++ trunk/library/classes/Gems/FormattedData.php 2012-09-26 10:33:36 UTC (rev 957)
@@ -80,7 +80,7 @@
* @return array The formatted array
*/
static function format($row, $model) {
- foreach ($row as $fieldname=>$value) {
+ foreach ($row as $fieldname=>$value) {
$row[$fieldname] = self::_format($fieldname, $row[$fieldname], $model);
}
return $row;
@@ -91,76 +91,113 @@
*
* @param type $name
* @param type $result
+ *@param MUtil_Model_ModelAbstract $model
* @return type
*/
private static function _format($name, $result, $model)
{
- if (null === $result && $default = $model->get($name,'default')) {
- $result = $default;
+ static $opts = array();
+ static $view = null;
+
+ if (!isset($opts[$name])) {
+ $opts[$name] = $model->get($name, array('default', 'multiOptions', 'formatFunction', 'dateFormat', 'storageFormat', 'itemDisplay'));
}
- if ($multiOptions = $model->get($name, 'multiOptions')) {
- if (is_array($multiOptions)) {
- /*
- * Sometimes a field is an array and will be formatted later on using the
- * formatFunction -> handle each element in the array.
- */
- if (is_array($result)) {
- foreach($result as $key => $value) {
- if (array_key_exists($value, $multiOptions)) {
- $result[$key] = $multiOptions[$value];
+ $options = $opts[$name];
+
+ foreach($options as $key => $value) {
+ switch ($key) {
+ case 'default':
+ if (is_null($result)) {
+ $result = $value;
+ }
+ break;
+
+ case 'multiOptions':
+ $multiOptions = $value;
+ if (is_array($multiOptions)) {
+ /*
+ * Sometimes a field is an array and will be formatted later on using the
+ * formatFunction -> handle each element in the array.
+ */
+ if (is_array($result)) {
+ foreach($result as $key => $value) {
+ if (array_key_exists($value, $multiOptions)) {
+ $result[$key] = $multiOptions[$value];
+ }
+ }
+ } else {
+ if (array_key_exists($result, $multiOptions)) {
+ $result = $multiOptions[$result];
+ }
}
}
- } else {
- if (array_key_exists($result, $multiOptions)) {
- $result = $multiOptions[$result];
+ break;
+
+ case 'formatFunction':
+ $callback = $value;
+ $result = call_user_func($callback, $result);
+ break;
+
+ case 'dateFormat':
+ if (array_key_exists('formatFunction', $options)) {
+ // if there is a formatFunction skip the date formatting
+ continue;
}
- }
- }
- }
+
+ $dateFormat = $value;
+ $storageFormat = $model->get($name, 'storageFormat');
+ $result = MUtil_Date::format($result, $dateFormat, $storageFormat);
+ break;
+ case 'itemDisplay':
+ $function = $value;
+ if (is_callable($function)) {
+ $result = call_user_func($function, $result);
+ } elseif (is_object($function)) {
+ if (($function instanceof MUtil_Html_ElementInterface)
+ || method_exists($function, 'append')) {
+ $object = clone $function;
+ $result = $object->append($result);
+ }
+ } elseif (is_string($function)) {
+ // Assume it is a html tag when a string
+ $result = MUtil_Html::create($function, $result);
+ }
- if ($callback = $model->get($name, 'formatFunction')) {
- $result = call_user_func($callback, $result);
- } elseif ($dateFormat = $model->get($name, 'dateFormat')) {
- $storageFormat = $model->get($name, 'storageFormat');
- $result = MUtil_Date::format($result, $dateFormat, $storageFormat);
+ default:
+ break;
+ }
}
- if ($function = $model->get($name, 'itemDisplay')) {
- if (is_callable($function)) {
- $result = call_user_func($function, $result);
- } elseif (is_object($function)) {
- if (($function instanceof MUtil_Html_ElementInterface)
- || method_exists($function, 'append')) {
- $object = clone $function;
- $result = $object->append($result);
- }
- } elseif (is_string($function)) {
- // Assume it is a html tag when a string
- $result = MUtil_Html::create($function, $result);
+ if (is_object($result)) {
+ // If it is Lazy, execute it
+ if ($result instanceof MUtil_Lazy_LazyInterface) {
+ $result = MUtil_Lazy::rise($result);
}
- }
- // If it is Lazy, execute it
- if ($result instanceof MUtil_Lazy_LazyInterface) {
- $result = MUtil_Lazy::rise($result);
- }
+ // If it is Html, render it
+ if ($result instanceof MUtil_Html_HtmlInterface) {
- // If it is Html, render it
- if ($result instanceof MUtil_Html_HtmlInterface) {
+ if (is_null($view)) {
+ $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
+ if (null === $viewRenderer->view) {
+ $viewRenderer->initView();
+ }
+ $view = $viewRenderer->view;
+ }
- $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- if (null === $viewRenderer->view) {
- $viewRenderer->initView();
+ $result = $result->render($view);
}
- $view = $viewRenderer->view;
- $result = $result->render($view);
}
return $result;
}
+ public function getFormatted() {
+ return $this->formatted;
+ }
+
public function setFormatted($bool) {
- $this->formatted = $bool;
+ $this->formatted = (bool) $bool;
}
}
\ No newline at end of file
Modified: trunk/library/classes/Gems/View/Helper/Excel.php
===================================================================
--- trunk/library/classes/Gems/View/Helper/Excel.php 2012-09-25 15:34:45 UTC (rev 956)
+++ trunk/library/classes/Gems/View/Helper/Excel.php 2012-09-26 10:33:36 UTC (rev 957)
@@ -50,6 +50,7 @@
} catch (Exception $e) {}
$this->view->layout()->setLayout('excel');
if ($rowset instanceof Gems_FormattedData) {
+ $formatted = $rowset->getFormatted();
$rowset->setFormatted(false);
}
$rowcnt = 0;
@@ -72,7 +73,7 @@
$output .= "\t</thead>\r\n";
$output .= "\t<tbody>\r\n";
if ($rowset instanceof Gems_FormattedData) {
- $rowset->setFormatted(true);
+ $rowset->setFormatted($formatted);
}
} else {
$output .= "\t\t<tr>\r\n";
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|