/**
  * Adds any controls for this display to the content node.
  * @param DOMNode $contentNode 
  * @returns boolean;
  */
 protected function displayReportControl($contentNode)
 {
     parent::displayReportControl($contentNode);
     if (function_exists('gzencode')) {
         $this->template->setDisplayDataImmediate('has_gz', 1);
     } else {
         $this->template->setDisplayDataImmediate('has_gz', 0);
     }
     if (function_exists('bzcompress')) {
         $this->template->setDisplayDataImmediate('has_bz2', 1);
     } else {
         $this->template->setDisplayDataImmediate('has_bz2', 0);
     }
     if (@class_exists('ZipArchive', false)) {
         $this->template->setDisplayDataImmediate('has_zip', 1);
     } else {
         $this->template->setDisplayDataImmediate('has_zip', 0);
     }
     $xslts = $this->getAvailableXSLT();
     if (count($xslts) == 0) {
         return true;
     }
     if (!($selNode = $this->template->getElementById('export_style', $contentNode)) instanceof DOMNode) {
         I2CE::raiseError("Bad");
     }
     foreach ($xslts as $xslt => $data) {
         $optionNode = $this->template->createElement('option', array('value' => 'xml:' . $xslt), "XML: " . $xslt);
         $this->template->appendNode($optionNode, $selNode);
     }
     return true;
 }
 /**
  * Adds any controls for this display to the content node.
  * @param DOMNode $contentNode 
  * @returns boolean;
  */
 protected function displayReportControl($contentNode)
 {
     parent::displayReportControl($contentNode);
     $js = '';
     $style = $this->defaultOptions['style'];
     $selectNode = $this->template->getElementByName('style', 0, $contentNode);
     if (!$selectNode instanceof DOMNode) {
         I2CE::raiseError("Could not find style node");
         return false;
     }
     $styles = $this->defaultOptions['styles'];
     foreach ($styles as $s => $data) {
         $chart_types = $data['chart_type_options'];
         //these have already been made sane.
         foreach ($chart_types as $k => $v) {
             $chart_types[$k] = "'{$v}'";
         }
         $js .= "\tcustom_report_chart_types['{$s}'] = new Array(" . implode(',', $chart_types) . ");\n";
         $attr = array('value' => $s);
         if ($style === $s) {
             $attr['selected'] = 'selected';
         }
         $header = $s;
         if (array_key_exists('display_name', $data) && $data['display_name']) {
             $header = $data['display_name'];
         }
         $selectNode->appendChild($this->template->createElement('option', $attr, $header));
     }
     $js .= "\n    if (window.addEvent) {\n       window.addEvent('domready',function(e) {\n           chart_styles = \$('chart_styles');\n\t   if (!chart_styles) {\n   \t      return;\n\t   }\n\t   custom_report_update_chart_types(chart_styles.options[chart_styles.selectedIndex].value);\n";
     if ($this->defaultOptions['displayFieldsType']) {
         $js .= "           \n           var types = \$('displayFieldTypes');\n           if (types) {\n                  var selected = -1;\n\t\t  \$each(types.options, function(option,i){\n                      if (option && option.value == '" . $this->defaultOptions['displayFieldsType'] . "') {\n                         selected = i;\n                      }\n                  });\n                  if (selected > -1)  {\n                        types.selectedIndex = selected;\n                  } \n                 custom_report_show_chart_type('" . $this->defaultOptions['displayFieldsType'] . "');\n            }\n";
     }
     $js .= "\n       });\n    }\n";
     $this->template->addHeaderLink('mootools-core.js');
     $this->template->addHeaderText($js, 'script', 'script_chart_type_options');
     $avail_fields = $this->getReportViewDisplayedFields(false, array(''));
     $numeric = $this->findNumericFields();
     $selects = $this->template->query('.//select[@class="reportformfield" or @class="reportformfield_numeric"]', $contentNode);
     for ($i = 0; $i < $selects->length; $i++) {
         $selectNode = $selects->item($i);
         $keys = explode(':', $selectNode->getAttribute('name'));
         //Example:  <select name="displayFieldsTypes:one_row_total:0:formfield" class='reportformfield'>
         $selected = $this->defaultOptions;
         while (count($keys) > 0) {
             $key = array_shift($keys);
             if (!is_array($selected) || !array_key_exists($key, $selected)) {
                 array_unshift($keys, $key);
                 break;
             }
             $selected = $selected[$key];
         }
         if (!is_scalar($selected) || count($keys) > 0 || is_string($selected) && strlen($selected) == 0) {
             $selected = false;
         }
         if ($selectNode->getAttribute('class') == 'reportformfield') {
             $rffs = $avail_fields;
         } else {
             $rffs = $numeric;
         }
         if ($selected && !array_key_exists($selected, $rffs)) {
             $selected = false;
         }
         foreach ($rffs as $reportformfield => $data) {
             $options = array('value' => $reportformfield);
             if ($selected === false || $selected == $reportformfield) {
                 //make the first one selected or the current one selected
                 $selected = $reportformfield;
                 $options['selected'] = 'selected';
             }
             $selectNode->appendChild($this->template->createElement('option', $options, $data['header']));
         }
     }
     $selects = $this->template->query('.//select[@class="reportformfield" or @class="reportformfield_numeric" or @class="reportformfield_aggregate"]', $contentNode);
     for ($i = 0; $i < $selects->length; $i++) {
         $selectNode = $selects->item($i);
         $selected = $this->defaultOptions;
         $keys = explode(':', $selectNode->getAttribute('name'));
         while (count($keys) > 0) {
             $key = array_shift($keys);
             if (!is_array($selected) || !array_key_exists($key, $selected)) {
                 array_unshift($keys, $key);
                 break;
             }
             $selected = $selected[$key];
         }
         if (!is_scalar($selected) || count($keys) > 0 || is_string($selected) && strlen($selected) == 0) {
             $selected = false;
         }
         if ($selected === false) {
             continue;
         }
         $options = $this->template->query(".//option[@value='{$selected}']", $selectNode);
         if ($options->length == 1) {
             $selected_options = $this->template->query(".//option[@selected]", $selectNode);
             //remove any existing selections
             for ($j = 0; $j < $selected_options->length; $j++) {
                 $selected_options->item($j)->removeAttribute('selected');
             }
             $options->item(0)->setAttribute('selected', 'selected');
         }
     }
     return true;
 }
 /**
  * Adds any controls for this display to the content node.
  * @param DOMNode $contentNode
  * @returns boolean
  */
 protected function displayReportControl($contentNode)
 {
     if (!parent::displayReportControl($contentNode)) {
         return false;
     }
     $this->template->setDisplayData("has_odt_templates", false);
     if ($this->config->is_parent("printed_forms")) {
         $print_forms = $this->config->printed_forms->getAsArray();
         foreach ($print_forms as $print_form => $print_data) {
             $this->template->setDisplayDataImmediate("has_odt_templates", " ");
             $this->template->addOption("odt_template", $print_form, $print_data['displayName'], $contentNode);
         }
     }
 }
 /**
  * Adds any controls for this display to the content node.
  * @param DOMNode $contentNode 
  * @returns boolean;
  */
 protected function displayReportControl($contentNode)
 {
     parent::displayReportControl($contentNode);
     $js = '';
     $avail_fields = $this->getReportViewDisplayedFields(false, array(''));
     $numeric = $this->findNumericFields();
     if ($this->defaultOptions['total']) {
         $total = 'Total';
         I2CE::getConfig()->setIfIsSet($total, "/modules/CustomReports/text/headers/count");
         $numeric['total'] = array('header' => $total);
     }
     $ol_config = I2CE::getConfig()->modules->OpenLayers;
     if ($ol_config->is_parent('default/layers')) {
         $layers = $ol_config->default->layers->getAsArray();
         foreach ($layers as $layer => $layer_data) {
             $layer_node = $this->template->appendFileById("customReports_display_control_OpenLayers_layer.html", "div", "openlayers_layers", false, $contentNode);
             $display = $layer;
             if (array_key_exists('_display', $layer_data)) {
                 $display = $layer_data['_display'];
             }
             $this->template->setDisplayDataImmediate("openlayers_layer_name", $display, $layer_node);
             $defaults = array();
             if (array_key_exists('layers', $this->defaultOptions) && is_array($this->defaultOptions['layers']) && array_key_exists($layer, $this->defaultOptions['layers']) && is_array($this->defaultOptions['layers'][$layer])) {
                 $defaults = $this->defaultOptions['layers'][$layer];
             }
             $renames = array("layers_enable" => "enable", "layers_style" => "style", "layers_exist" => "exist");
             $checkbox = array("enable" => true, "style" => true);
             foreach ($renames as $id => $new_id) {
                 $this->template->setAttribute("id", "layers:{$layer}:{$new_id}", $id, null, $layer_node);
                 $this->template->setAttribute("name", "layers:{$layer}:{$new_id}", "layers:{$layer}:{$new_id}", null, $layer_node);
                 if (array_key_exists($new_id, $defaults) && $defaults[$new_id] == 'on') {
                     if (array_key_exists($new_id, $checkbox) && $checkbox[$new_id]) {
                         $this->template->setAttribute("checked", "checked", "layers:{$layer}:{$new_id}", null, $layer_node);
                         $this->template->setAttribute("id", "layers:{$layer}:{$new_id}_for", "layers_{$new_id}_for", null, $layer_node);
                         $this->template->setAttribute("for", "layers:{$layer}:{$new_id}", "layers:{$layer}:{$new_id}_for", null, $layer_node);
                     } else {
                         $this->template->setAttribute("value", $defaults[$new_id], "layers:{$layer}:{$new_id}", null, $layer_node);
                     }
                 }
             }
         }
     }
     /*
     $selects = $this->template->query('.//select[@class="reportformfield" or @class="reportformfield_numeric"]',$contentNode);
     for ($i=0; $i < $selects->length; $i++) {
         $selectNode = $selects->item($i);
         $keys = explode(':',$selectNode->getAttribute('name'));
         //Example:  <select name="displayFieldsTypes:one_row_total:0:formfield" class='reportformfield'>
         $selected = $this->defaultOptions;
         while (count($keys) > 0) {
             $key = array_shift($keys);
             if (!is_array($selected) || !array_key_exists($key,$selected)) {
                 array_unshift($keys,$key);
                 break;
             }
             $selected = $selected[$key];
         }
         if (!is_scalar($selected) || count($keys) > 0 || (is_string($selected) && strlen($selected) == 0)) {
             $selected = false;
         }
         if ($selectNode->getAttribute('class') == 'reportformfield') {
             $rffs = $avail_fields;
         } else {
             $rffs = $numeric;
         }
         if ($selected && !array_key_exists($selected,$rffs)) {
             $selected = false;
         }
         foreach ($rffs as $reportformfield=>$data) {
             if ( !$data ) {
                 continue;
             }
             $options = array('value'=>$reportformfield);
             if (($selected == $reportformfield)) { //make the first one selected or the current one selected
                 $options['selected'] = 'selected';
             }
             $selectNode->appendChild($this->template->createElement('option',$options,$data['header']));
         }
     }
     
     $selects = $this->template->query('.//select[@class="reportformfield" or @class="reportformfield_numeric" or @class="reportformfield_aggregate"]',$contentNode);
     for ($i=0; $i < $selects->length; $i++) {
         $selectNode = $selects->item($i);
         $selected = $this->defaultOptions;
         $keys = explode(':',$selectNode->getAttribute('name'));
         while (count($keys) > 0) {
             $key = array_shift($keys);
             if (!is_array($selected) || !array_key_exists($key,$selected)) {
                 array_unshift($keys,$key);
                 break;
             }
             $selected = $selected[$key];
         }
         if (!is_scalar($selected) || count($keys) > 0 || (is_string($selected) && strlen($selected) == 0)) {
             $selected = false;
         }
         if ( $selected === false ) {
             continue;
         }
         $options = $this->template->query(".//option[@value='$selected']",$selectNode);
         if ($options->length == 1) {
             $selected_options = $this->template->query( ".//option[@selected]", $selectNode );
             //remove any existing selections
             for( $j = 0; $j < $selected_options->length; $j++ ) {
                 $selected_options->item($j)->removeAttribute('selected');
             }
             $options->item(0)->setAttribute('selected','selected');
         }           
     }
     */
     return true;
 }
 /**
  * returns an array of the default display options.  Default options are read and overriddenb
  * in the following order:
  * /modules/CustomReports/displays/$display/display_options
  * /modules/CustomReports/relationships/display_options/$display
  * /modules/CustomReports/reports/display_options/$display
  * /modules/CustomReports/reportViews/$view/display_options/$display
  * Finally any options that have a key in $get are replaced by that value
  * @param array $get
  * @param array $options. Default to the empty array.  The options that we want to be already set before we start goinng through
  * @returns array
  */
 protected function getDefaultOptions($get, $options = array())
 {
     //make sure we get these values from the get variables
     $make_exist = array('reverseOrder' => false, 'displayFieldsType' => null, 'displayFieldsTypes' => array(), 'displayFields' => array(), 'limit_page' => 1, 'limit_per_page' => false, 'limit_offset' => 0, 'limit_amount' => false, 'limit_paginated' => false);
     foreach ($make_exist as $key => $val) {
         if (!array_key_exists($key, $options)) {
             $options[$key] = $val;
         }
     }
     $t_options = array();
     if (I2CE::getConfig()->setIfIsSet($t_options, "/modules/CustomReports/displays/Visualization/display_options", true)) {
         I2CE_Util::merge_recursive($options, $t_options);
     }
     $t_options = array();
     if ($this->config->setIfIsSet($t_options, "display_options/Visualization", true)) {
         I2CE_Util::merge_recursive($options, $t_options);
     }
     $defaultOptions = parent::getDefaultOptions($get, $options);
     $this->defaultOptions = $defaultOptions;
     $this->findNumericFields();
     $this->makeVisualizationStylesSane($defaultOptions);
     if (count($defaultOptions['styles']) == 0) {
         I2CE::raiseError("No valid styles for Visualization!");
         return false;
     }
     if (!$this->ensureValidStyleAndType($defaultOptions)) {
         I2CE::raiseError("No valid style and type can be chosen for Visualization");
         return false;
     }
     if (!$this->setupVisualization($defaultOptions)) {
         I2CE::raiseError("Could not set up visualization options!");
         return false;
     }
     // Currently don't allow saving datasource as default view since Visualization passes this through
     unset($defaultOptions['save_options_as_default_view']);
     return $defaultOptions;
 }
 /**
  * Process results
  * @param array $results_data an array of results.  indices are 'results' and MDB2 Buffered result and 'num_results' the
  * number of results.  (these values may be false on failure)
  * @param DOMNode $contentNode.  Default to null a node to append the results onto
  */
 protected function processResults($results_data, $contentNode = null)
 {
     $this->pivots = $this->getPivots();
     //add int the header fields to the table
     if (!$results_data['num_results']) {
         $this->noData($contentNode);
         return true;
     }
     $tableContainerNode = $this->template->appendFileByNode("customReports_table.html", 'div', $contentNode);
     //add the report table shell
     if (!$tableContainerNode instanceof DOMNode) {
         I2CE::raiseError("Could not add table container template");
         return false;
     }
     $this->template->setAttribute("id", $this->getReportPrefix() . 'report_pager_display', "report_pager_display");
     $this->doJumper($results_data['num_results'], $tableContainerNode);
     $reportBodyNode = $this->template->getElementById('report_body', $tableContainerNode);
     if (!$reportBodyNode instanceof DOMNode) {
         I2CE::raiseError("Could not find report body");
         return false;
     }
     $this->doHeaderRow($tableContainerNode);
     $this->template->setDisplayDataImmediate('num_results', $results_data['num_results'], $tableContainerNode);
     return parent::processResults($results_data, $reportBodyNode);
 }