예제 #1
0
 public static function print_list($items, $options = array(), $return = false)
 {
     $get_property_value = self::_get_property_value_func();
     $items_html = '';
     $main_attrs = array();
     $default_options = array('type' => 'ul', 'attr' => array(), 'class' => array(), 'id' => '');
     $new_options = parent::set_array_prop_def($default_options, $options, 'class');
     foreach ($items as $item) {
         $item_html = '';
         $item_prop = array('content' => '', 'subitems' => array(), 'class' => '', 'attr' => array());
         $new_item_prop = parent::get_clean_structure($item_prop, $item, array($item), 'content');
         $content = $new_item_prop['content'];
         if ($new_item_prop['subitems']) {
             $content .= self::print_list($new_item_prop['subitems'], false, true);
         }
         $attrs = array();
         if ($new_item_prop['class']) {
             $attrs[] = 'class="' . (is_array($new_item_prop['class']) ? implode(' ', $new_item_prop['class']) : $new_item_prop['class']) . '"';
         }
         if ($new_item_prop['attr']) {
             foreach ($new_item_prop['attr'] as $key => $value) {
                 $attrs[] = $key . '="' . $value . '"';
             }
         }
         $item_html = '<li' . ($attrs ? ' ' . implode(' ', $attrs) : '') . '>' . $content . '</li>';
         $items_html .= $item_html;
     }
     if ($new_options['class']) {
         $main_attrs[] = 'class="' . (is_array($new_options['class']) ? implode(' ', $new_options['class']) : $new_options['class']) . '"';
     }
     if ($new_options['attr']) {
         foreach ($new_options['attr'] as $key => $value) {
             $main_attrs[] = $key . '="' . $value . '"';
         }
     }
     if ($new_options['id']) {
         $main_attrs[] = 'id="' . $new_options['id'] . '"';
     }
     $result = '<' . $new_options['type'] . ($main_attrs ? ' ' . implode(' ', $main_attrs) : '') . '>';
     $result .= $items_html;
     $result .= '</' . $new_options['type'] . '>';
     if ($return) {
         return $result;
     } else {
         echo $result;
     }
 }
    public function print_html($return = false)
    {
        $get_property_value = parent::_get_property_value_func();
        $that = $this;
        $structure = $this->_structure;
        $rows = $get_property_value($structure->data, array("if_array" => function ($data) use($that, $get_property_value, $structure) {
            $html_rows = array();
            foreach ($data as $row_index => $row_data) {
                $row_prop = array("hidden" => false, "checkbox" => true, "detail" => true, "class" => "", "attr" => "", "content" => true);
                $new_row_prop = $row_prop;
                if (isset($structure->row[$row_index + 1])) {
                    $row_prop_value = $structure->row[$row_index + 1];
                    if ($row_prop_value === false) {
                        $new_row_prop["hidden"] = true;
                    } else {
                        if ($row_prop_value === "") {
                            $new_row_prop["content"] = "";
                        } else {
                            $new_row_prop = SmartUtil::get_clean_structure($row_prop, $row_prop_value, array($row_data, $row_index), 'class');
                        }
                    }
                }
                $rows_html = '';
                foreach ($row_data as $col_name => $cell_value) {
                    $hide_class = '';
                    if (isset($structure->hide[$col_name]) && $structure->hide[$col_name] === true || in_array($col_name, $structure->hidden)) {
                        $hide_class = ' class="hidden"';
                    }
                    if (isset($new_row_prop["content"]) && !$new_row_prop["content"]) {
                        $rows_html .= '<td' . $hide_class . '></td>';
                        continue;
                    }
                    $cell_html = $cell_value;
                    if (isset($structure->cell[$col_name])) {
                        $cell_prop = $structure->cell[$col_name];
                        $cell_html = $get_property_value($cell_prop, array("if_closure" => function ($prop) use($that, $row_data, $row_index, $cell_value) {
                            return SmartUtil::run_callback($prop, array($row_data, $cell_value, $row_index));
                        }, "if_array" => function ($cell_prop) use($that, $row_data, $row_index, $cell_value, $get_property_value) {
                            //icon, content, color, url[href, title, tooltip, attr]
                            $cell_html = $cell_value;
                            //content
                            if (isset($cell_prop["content"])) {
                                $cell_html = $get_property_value($cell_prop["content"], array("if_closure" => function ($content) use($that, $row_data, $row_index, $cell_value) {
                                    $content_value = $that::replace_col_codes(SmartUtil::run_callback($content, array($row_data, $cell_value, $row_index)), $row_data);
                                    return $content_value;
                                }, "if_other" => function ($content) use($cell_html) {
                                    $cell_html = $content;
                                    return $cell_html;
                                }));
                            }
                            //url
                            if (isset($cell_prop["url"])) {
                                $map_url_prop = array("href" => "#", "target" => "_self", "title" => "", "attr" => "");
                                $map_url_prop = $get_property_value($cell_prop["url"], array("if_closure" => function ($prop) use($row_data, $row_index, $cell_value, $map_url_prop) {
                                    $url = SmartUtil::run_callback($prop, array($row_data, $cell_value, $row_index));
                                    $map_url_prop["href"] = $url;
                                    return $map_url_prop;
                                }, "if_array" => function ($url_prop) use($that, $row_data, $cell_html, $map_url_prop) {
                                    $map_url_prop["target"] = isset($url_prop['target']) ? $url_prop['target'] : "_self";
                                    $map_url_prop["href"] = isset($url_prop['href']) ? $that::replace_col_codes($url_prop['href'], $row_data, true) : '#';
                                    $map_url_prop["attr"] = isset($url_prop['attr']) && $url_prop['attr'] ? $url_prop['attr'] : '';
                                    $map_url_prop["title"] = isset($url_prop['title']) ? $that::replace_col_codes($url_prop['title'], $row_data, true) : '';
                                    return $map_url_prop;
                                }, "if_other" => function ($url_prop) use($that, $row_data, $cell_html, $map_url_prop) {
                                    $map_url_prop["href"] = $that::replace_col_codes($url_prop, SmartUtil::object_to_array($row_data), true);
                                    return $map_url_prop;
                                }));
                                $cell_html = '<a href="' . $map_url_prop["href"] . '" target="' . $map_url_prop["target"] . '" ' . $map_url_prop["attr"] . ' title="' . $map_url_prop["title"] . '">' . $cell_html . '</a>';
                            }
                            //icon
                            if (isset($cell_prop["icon"])) {
                                $cell_html = $get_property_value($cell_prop["icon"], array("if_closure" => function ($icon) use($row_data, $row_index, $cell_value, $cell_html) {
                                    $icon_value = SmartUtil::run_callback($prop, array($row_data, $cell_value, $row_index));
                                    return '<i class="fa ' . $icon_value . ' fa-md"></i> ' . $cell_html;
                                }, "if_other" => function ($icon) use($cell_html) {
                                    return '<i class="fa ' . $icon . ' fa-md"></i> ' . $cell_html;
                                }));
                            }
                            //color
                            if (isset($cell_prop["color"])) {
                                $cell_html = $get_property_value($cell_prop["color"], array("if_closure" => function ($color) use($row_data, $row_index, $cell_value, $cell_html) {
                                    $color_value = SmartUtil::run_callback($color, array($row_data, $cell_value, $row_index));
                                    return '<span class="' . $color_value . '">' . $cell_html . '</span>';
                                }, "if_other" => function ($color) use($cell_html) {
                                    return '<span class="' . $color . '">' . $cell_html . '</span>';
                                }));
                            }
                            //callback
                            if (isset($cell_prop["callback"]) && SmartUtil::is_closure($cell_prop["callback"])) {
                                $new_cell_html = SmartUtil::run_callback($cell_prop["callback"], array($row_data, $cell_html, $row_index));
                                if (trim($new_cell_html) != "") {
                                    $cell_html = $new_cell_html;
                                }
                            }
                            return $cell_html;
                        }, "if_other" => function ($cell_prop) use($that, $row_data) {
                            return $that::replace_col_codes($cell_prop, $row_data);
                        }));
                    }
                    $rows_html .= '<td' . $hide_class . '> ' . $cell_html . ' </td>';
                }
                $row_classes = array();
                if ($new_row_prop["class"]) {
                    $row_classes[] = $new_row_prop["class"];
                }
                if ($new_row_prop["hidden"] === true) {
                    $row_classes[] = 'hidden';
                }
                $attr = $new_row_prop["attr"] ? ' ' . $new_row_prop["attr"] : '';
                $row_class = $row_classes ? ' class="' . implode(' ', $row_classes) . '"' : '';
                $row_checkbox = '';
                $row_details = '';
                if (isset($structure->options["checkboxes"]) && $structure->options["checkboxes"]) {
                    $option = $structure->options["checkboxes"];
                    $checkbox_prop = array("name" => $structure->id . "_checkbox", "id" => "", "checked" => false);
                    $new_checkbox_prop = SmartUtil::get_clean_structure($checkbox_prop, $option, array($that, $row_data, $row_index), 'name');
                    $id = $new_checkbox_prop["id"] ? 'id="' . $new_checkbox_prop["id"] . '"' : '';
                    $content = '<label class="checkbox-inline">
		                          <input type="checkbox" ' . ($new_checkbox_prop["checked"] ? 'checked' : '') . ' class="checkbox style-0" name="' . $checkbox_prop["name"] . '[]" ' . $id . ' />
		                          <span></span>
		                        </label>';
                    if ($new_row_prop["checkbox"] === false) {
                        $content = '';
                    }
                    $row_checkbox = '
			                <td class="center" width="20px"> ' . $content . ' </td>';
                }
                if (isset($structure->options["row_details"]) && $structure->options["row_details"]) {
                    $option = $structure->options["row_details"];
                    $detail_prop = array("id" => "", "icon" => 'fa-plus-square', "title" => 'Show Details');
                    $new_detail_prop = SmartUtil::get_clean_structure($detail_prop, $option, array($that, $row_data, $row_index), 'icon');
                    $id = $new_detail_prop["id"] ? 'id="' . $new_detail_prop["id"] . '"' : '';
                    $content = '<a href="#" ' . $id . '>
									<i class="fa ' . $detail_prop['icon'] . ' fa-lg" data-toggle="row-detail" title="' . $detail_prop['title'] . '"></i>
								</a>';
                    if ($new_row_prop["detail"] === false) {
                        $content = '';
                    }
                    $row_details = '<td class="center" width="20px"> ' . $content . ' </td>';
                }
                $html_rows[] = '<tr' . $row_class . $attr . '>' . $row_details . $row_checkbox . $rows_html . '</tr>';
            }
            return implode('', $html_rows);
        }, "if_closure" => function ($data) {
            SmartUI::err('SmartUI::DataTable::data requires an array of objects/array');
            return '';
        }, "if_other" => function ($data) {
            SmartUI::err('SmartUI::DataTable::data requires an array of objects/array');
            return '';
        }));
        $cols = $get_property_value($structure->col, array("if_array" => function ($cols) use($that, $get_property_value, $structure) {
            $html_col_list = array();
            foreach ($cols as $col_name => $col_value) {
                if (is_null($col_value) || $col_value === false) {
                    continue;
                }
                $col_value_prop = array("name" => $col_name, "class" => "", "attr" => array(), "icon" => "", "hidden" => isset($structure->hide[$col_name]) && $structure->hide[$col_name] === true || in_array($col_name, $structure->hidden));
                $new_col_value = SmartUtil::get_clean_structure($col_value_prop, $col_value, array($that, $cols), 'name');
                if ($new_col_value['attr']) {
                    if (is_array($new_col_value["attr"])) {
                        foreach ($new_col_value["attr"] as $attr => $value) {
                            $attrs[] = $attr . '="' . $value . '"';
                        }
                    } else {
                        $attrs[] = $new_col_value["attr"];
                    }
                    $new_col_value["attr"] = $attrs;
                }
                $classes = array();
                if ($new_col_value['class']) {
                    $classes[] = $new_col_value['class'];
                }
                if ($new_col_value['hidden'] === true) {
                    $classes[] = "hidden";
                }
                $class = $classes ? 'class="' . implode(' ', $classes) . '"' : '';
                $main_attributes = array($class, implode(' ', $new_col_value['attr']));
                $htm_attrs = trim(implode(' ', $main_attributes));
                $htm_attrs = $htm_attrs ? ' ' . $htm_attrs : '';
                $html_col_list[] = '<th' . $htm_attrs . '>' . $new_col_value['icon'] . ' ' . $new_col_value['name'] . ' </th>';
            }
            $html_cols = implode('', $html_col_list);
            $checkbox_header = '';
            $detail_header = '';
            if (isset($structure->options["checkboxes"]) && $structure->options["checkboxes"]) {
                $checkbox_header = '
						<th class="center" width="20px">
							<label class="checkbox-inline">
								<input type="checkbox" class="checkbox style-0">
								<span></span>
							</label>
						</th>';
            }
            if (isset($structure->options["row_details"]) && $structure->options["row_details"]) {
                $detail_header = '
						<th class="center" width="20px"></th>';
            }
            return '<tr>' . $detail_header . $checkbox_header . $html_cols . '</tr>';
        }));
        $id = $get_property_value($structure->id, array("if_closure" => function ($prop) use($that) {
            return SmartUtil::run_callback($prop, array($that));
        }, "if_other" => function ($prop) {
            return $prop;
        }, "if_array" => function ($prop) use($structure) {
            SmartUI::err('SmartUI::Widget::id requires string.');
            return $structure->id;
        }));
        $id = $id ? 'id="' . $id . '"' : '';
        $table_html = '<table ' . $id . ' class="table table-striped table-bordered table-hover">';
        $table_html .= '<thead>';
        $table_html .= $cols;
        $table_html .= '</thead>';
        $table_html .= '<tbody>';
        $table_html .= $rows;
        $table_html .= '</tbody>';
        $table_html .= '</table>';
        $result = $table_html;
        if (isset($structure->options["in_widget"]) && $structure->options["in_widget"]) {
            // no need for widget's toolbar for datatable 1.10.x
            // if (!$structure->options["static"])
            // 	$structure->widget->body('toolbar', '');
            $structure->widget->body("content", $table_html);
            $result = $structure->widget->print_html(true);
        }
        if ($return) {
            return $result;
        } else {
            echo $result;
        }
    }