protected function _render_function_callback($row, HTMLTableColumn $column, HTMLTable $table)
 {
     $array = $row;
     $json = HTMLHelper::escape(JSHelper::cast_obj($array));
     $block = new HTMLLongTag('div');
     $block->add_style('white-space', 'nowrap');
     $block->add_style('text-align', 'center');
     $block->add_style('padding', '0 10px');
     $html = "";
     $count_added = 0;
     /**
      *
      * @var $tag HTMLTag
      *
      */
     foreach ($column->_actions_tags as $index => $tag) {
         if ($tag->call_filter_callback($row, $column, $table) === false) {
             continue;
         }
         $tag_html = $tag->to_string();
         $tag_html = str_replace('%7B%7B', '{{', $tag_html);
         $tag_html = str_replace('%7D%7D', '}}', $tag_html);
         if (preg_match(self::$_row_json_pattern, $tag_html, $match)) {
             $tag_html = preg_replace(self::$_row_json_pattern, $json, $tag_html);
         }
         while (preg_match(self::$_row_field_pattern, $tag_html, $match)) {
             if ($match['field'] == self::ROW_ID_FIELD) {
                 $field = $table->get_row_id($row);
             } else {
                 $field = ArrayHelper::get_value($row, $match['field']);
             }
             $tag_html = str_replace($match[0], JSHelper::cast($field), $tag_html);
         }
         if ($count_added > 0) {
             $html .= '   ';
         }
         $html .= $tag_html;
         $count_added++;
     }
     $block->set_content($html);
     return $block;
 }
 protected function _get_input_html(array $params = array())
 {
     $title = $this->_get_title();
     if ((!array_key_exists('placeholder', $params) || !$params['placeholder']) && $this->get_use_title_label() && $title) {
         $params['placeholder'] = $title;
     }
     $value = null;
     if (array_key_exists('value', $params)) {
         $value = self::parse_autocomplete_option($params['value']);
         if (!$value) {
             $value = array('id' => $params['value'], 'text' => '');
         }
         $params['value'] = $value['id'];
     }
     $attrs_html = self::_get_attr_html($params);
     $input_html = "<{$this->_tagname}{$attrs_html} />";
     $js_id = \ZPHP\JSHelper::cast_str('#' . $this->_get_id());
     $js_searching = \ZPHP\String::get_js('searching');
     $js_no_matches = \ZPHP\String::get_js('no_matches_found');
     $js_url = \ZPHP\JSHelper::cast_str($this->_request_url);
     $js_search_varname = \ZPHP\JSHelper::cast_str($this->_search_varname);
     $js_min_length = \ZPHP\JSHelper::cast_number($this->_min_length);
     $js_data = \ZPHP\JSHelper::cast_obj($this->_request_data);
     $js_method = \ZPHP\JSHelper::cast_str($this->_request_method);
     $js_placeholder = $this->_get_title();
     if ((!array_key_exists('placeholder', $params) || !$params['placeholder']) && $this->get_use_title_label() && $title) {
         $js_placeholder = $title;
     }
     $js_placeholder = \ZPHP\JSHelper::cast_str($js_placeholder);
     $js_window_id = \ZPHP\JSHelper::cast_str('#s2id_' . $this->_get_id());
     $html = "{$input_html}\n";
     $html .= "<script type='text/javascript'>\n";
     $html .= "if(!window[{$js_window_id}]) {\n\n\t\t\$({$js_id}).select2({formatSearching: function() {\n\t\t\treturn {$js_searching};\n\t\t\t},\n\t\t\tallowClear: " . JSHelper::cast_bool($this->_allow_clear) . ",\n\t\t\tformatNoMatches: function() {\n\t\t\treturn {$js_no_matches};\n\t\t\t},formatInputTooShort: function() {\n\t\t\t\treturn '';\n\t\t\t},\n\t\t\tescapeMarkup: function(s) { return s; },\n\t\t\tminimumInputLength: {$js_min_length},";
     if ($this->get_width()) {
         $html .= "width: " . \ZPHP\JSHelper::cast_str($this->get_width()) . ",";
     }
     $html .= "multiple: false,";
     if ($this->get_value()) {
         $html .= "placeholder: {$js_placeholder},";
     }
     $html .= "ajax: {\n\t\t\turl: {$js_url},\n\t\t\tdataType: 'json',\n\t\t\tdata: function(term, page)\n\t\t\t{\n\t\t\t\treturn \$.extend({}, { {$js_search_varname}: term}, {$js_data});\n\t\t\t},\n\t\t\tresults: function(data, page)\n\t\t\t{\n\t\t\t\treturn {results: data['rows']};\n\t\t\t}\n\t\t\t}\n\t\t";
     if ($value && is_array($value) && isset($value['id'])) {
         if (isset($value['text']) && $value['text']) {
             $html .= ", initSelection : function (element, callback) {\n\t\t\t\t\telement.val(" . \ZPHP\JSHelper::cast_str($value['id']) . ");\n\t\t\t\t\tcallback(" . \ZPHP\JSHelper::cast_obj($value) . ");\n\t\t\t\t}";
         } else {
             $html .= ", initSelection : function (element, callback) {\n\n\t\t\t\t\tvar ajaxData = {};\n\t\t\t\t\tajaxData[" . JSHelper::cast_str($this->_id_varname) . "] = " . JSHelper::cast_str($value['id']) . ";\n\n\t\t\t\t\t\$.ajax({\n\t\t\t\t\t\turl: {$js_url},\n\t\t\t\t\t\tdataType: 'json',\n\t\t\t\t\t\ttype: " . JSHelper::cast_str($this->_request_method) . ",\n\t\t\t\t\t\tdata: ajaxData,\n\t\t\t\t\t\tsuccess: function(data)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvar row = null;\n\n\t\t\t\t\t\t\tif(data)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tif(data['row'])\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\trow = data['row'];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\telse if(data['rows'])\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\trow = data['rows'][0];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif(row)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\telement.val(row['id']);\n\t\t\t\t\t\t\t\tcallback(row);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}";
         }
     }
     $html .= "});";
     $html .= "window[{$js_window_id}] = true; }";
     $html .= "\n</script>";
     return $html;
 }
 protected function _get_input_html(array $params = array())
 {
     if (isset($params['value'])) {
         unset($params['value']);
     }
     $title = $this->_get_title();
     if ((!array_key_exists('placeholder', $params) || !$params['placeholder']) && $this->get_use_title_label() && $title) {
         $params['placeholder'] = $title;
     }
     if ($this->_date_value) {
         $params['value'] = $this->_date_value->format($this->_date_format);
     }
     $style = '';
     if (isset($params['style'])) {
         $params['style'] = String::put_sufix($params['style'], ';') . $style;
     } else {
         $params['style'] = $style;
     }
     $params['style'] = "letter-spacing: 3px; {$params['style']}";
     $attrs_html = self::_get_attr_html($params);
     $datepicker_options = array_merge(array(), self::$_DEFAULT_DATEPICKER_OPTIONS);
     $datepicker_options['format'] = self::_datepicker_format($this->_date_format);
     $datepicker_options['clearBtn'] = $this->_clear_button;
     if ($this->_before_show_day) {
         $datepicker_options[JSHelper::get_avoid_cast_key('beforeShowDay')] = $this->_before_show_day;
     }
     if ($this->_before_show_month) {
         $datepicker_options[JSHelper::get_avoid_cast_key('beforeShowMonth')] = $this->_before_show_month;
     }
     if ($this->_start_date) {
         $datepicker_options['startDate'] = $this->_start_date->format(self::$_START_DATE_FORMAT);
     }
     if ($this->_end_date) {
         $datepicker_options['endDate'] = $this->_end_date->format(self::$_END_DATE_FORMAT);
     }
     $datepicker_options['language'] = 'es';
     $html = "<{$this->_tagname}{$attrs_html} />";
     $html .= new HTMLScriptTag("\$(" . JSHelper::cast_str('#' . $this->_get_id()) . ").datepicker(" . JSHelper::cast_obj($datepicker_options) . ");");
     $html .= new HTMLScriptTag("\$(" . JSHelper::cast_str('#' . $this->_get_id()) . ").mask(" . JSHelper::cast_str($this->_date_mask) . ");");
     return $html;
 }
Beispiel #4
0
    public function to_string()
    {
        $prepared_rows = array();
        $prepared_columns = array();
        $array_rows = array();
        $has_checkbox_column = false;
        foreach ($this->_rows as $row) {
            $prepared_row = array();
            foreach ($this->_columns as $column) {
                $prepared_row[] = $column->call_row_render_function($row, $this);
            }
            $prepared_rows[] = $prepared_row;
            $array_rows[$this->get_row_id($row)] = $row;
        }
        foreach ($this->_columns as $column) {
            $prepared_column = array();
            $prepared_column['title'] = $column->call_header_render_function($this);
            $prepared_column['orderable'] = $column->get_orderable();
            $prepared_column['class'] = trim($column->get_class());
            $prepared_columns[] = $prepared_column;
            if (ClassHelper::is_instance_of($column, 'ZPHP\\HTML\\HTMLTableColumnCheckbox')) {
                $has_checkbox_column = true;
            }
        }
        $html = '';
        if (empty($this->_rows)) {
            $html = '<div class="panel panel-default datatable-empty">
				<div class="panel-body">
					' . $this->_empty_html . '
				</div>
			</div>';
        } else {
            $attrs_html = self::_get_attr_html($this->_params);
            $html = '

				<div id="' . HTMLHelper::escape('div_container_' . $this->id) . '" class="datatable-container">
					<table' . $attrs_html . '>
						<thead><tr>';
            foreach ($this->_columns as $column) {
                $html .= $column;
            }
            $data_table_params = array();
            $data_table_params['data'] = $prepared_rows;
            $data_table_params['columns'] = $prepared_columns;
            $data_table_params['paging'] = $this->_paging;
            $data_table_params['info'] = $this->_show_info;
            $data_table_params['ordering'] = $this->_ordering;
            $data_table_params['language'] = array();
            $data_table_params['language']['search'] = \ZPHP\String::get('datatables_search');
            $data_table_params['language']['lengthMenu'] = \ZPHP\String::get('datatables_length_menu');
            $data_table_params['language']['zeroRecords'] = \ZPHP\String::get('datatables_zero_records');
            $data_table_params['language']['info'] = \ZPHP\String::get('datatables_info');
            $data_table_params['language']['infoEmpty'] = \ZPHP\String::get('datatables_info_empty');
            $data_table_params['language']['infoFiltered'] = \ZPHP\String::get('datatables_info_filtered');
            $data_table_params['language']['loadingRecords'] = \ZPHP\String::get('datatables_loading_records');
            $data_table_params['language']['paginate'] = array();
            $data_table_params['language']['paginate']['first'] = \ZPHP\String::get('datatables_first');
            $data_table_params['language']['paginate']['last'] = \ZPHP\String::get('datatables_last');
            $data_table_params['language']['paginate']['last'] = \ZPHP\String::get('datatables_last');
            $data_table_params['language']['paginate']['next'] = \ZPHP\String::get('datatables_next');
            $data_table_params['language']['paginate']['previous'] = \ZPHP\String::get('datatables_previous');
            $html .= '

				</tr></thead>
						<tbody></tbody>
					</table>
				</div>
				<script type="text/javascript">
					(function() {
						var table = $(' . JSHelper::cast_str('#' . $this->id) . ');
						var dataTable = table.dataTable(' . JSHelper::cast_obj($data_table_params) . ');

						table.data("rowsData", ' . JSHelper::cast_obj($array_rows) . ');
						table.data("dataTable", dataTable);

						table.data("getRowData", function(id) {
							return table.data("rowsData")[id];
						});

						';
            foreach ($this->_columns as $column) {
                $html .= $column->call_load_render_function($this);
            }
            if ($has_checkbox_column) {
                $html .= 'table.data("getCheckedRows", function(field) {
							var checkedInputs = table.find("tbody tr input.checkbox:checked");
							var rowsData = [];
							checkedInputs.each(function(index, item)
							{
								var id = $(this).val();
								var rowData = table.data("getRowData")(id);

								if(field)
								{
									rowData = rowData[field];
								}

								rowsData.push(rowData);
							});

							return rowsData;
						});';
            }
            $html .= '	})();';
            $html .= "\n\t\t\t\$(document).ready(function() {\n\t\t\t\tvar table = \$(" . JSHelper::cast_str('#' . $this->id) . ");\n\t\t\t\ttable.parent().css({'min-height': table.height()});\n\n\t\t\t\tvar selects = \$('.dataTables_length select');\n\n\t\t\t\tselects.each(function() {\n\n\t\t\t\t\tif(\$(this).children().length == 0)\n\t\t\t\t\t{\n\t\t\t\t\t\t\$(this).parents('.dataTables_length').css({'visibility': 'hidden'});\n\t\t\t\t\t}\n\n\t\t\t\t});\n\t\t\t});\n\n\t\t\t";
            $html .= "</script>";
        }
        return $html;
    }