예제 #1
0
 public static function get_clean_structure($default_prop, $value, $closure_defaults = array(), $default_key = '')
 {
     $get_property_value = self::_get_property_value_func();
     $structure = $get_property_value($value, array('if_array' => function ($value) use($default_prop, $default_key) {
         return SmartUtil::set_array_prop_def($default_prop, $value, $default_key);
     }, 'if_closure' => function ($value) use($closure_defaults, $default_prop, $default_key) {
         return SmartUtil::set_closure_prop_def($default_prop, $value, $closure_defaults, $default_key);
     }, 'if_other' => function ($value) use($default_prop, $default_key) {
         $default_prop[$default_key] = $value;
         return $default_prop;
     }));
     return $structure;
 }
예제 #2
0
 public static function print_dropdown($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' => '', 'id' => '', 'multilevel' => false);
     $new_options = parent::set_array_prop_def($default_options, $options, 'class');
     foreach ($items as $item) {
         $item_html = '';
         $item_prop = array('content' => '', 'submenu' => array(), 'class' => array(), 'attr' => array());
         $new_item_prop = $get_property_value($item, array('if_array' => function ($item) use($item_prop) {
             return SmartUtil::set_array_prop_def($item_prop, $item, 'content');
         }, 'if_closure' => function ($item) use($item_prop) {
             return SmartUtil::set_closure_prop_def($item_prop, $item);
         }, 'if_other' => function ($item) use($item_prop) {
             $item_prop['content'] = $item;
             return $item_prop;
         }));
         $classes = array();
         if ($new_item_prop['class']) {
             $classes[] = is_array($new_item_prop['class']) ? implode(' ', $new_item_prop['class']) : $new_item_prop['class'];
         }
         $attrs = array();
         if ($new_item_prop['attr']) {
             foreach ($new_item_prop['attr'] as $key => $value) {
                 $attrs[] = $key . '="' . $value . '"';
             }
         }
         $content = $new_item_prop['content'];
         if ($new_item_prop['submenu']) {
             $content .= self::print_dropdown($new_item_prop['submenu'], null, true);
             $classes[] = 'dropdown-submenu';
         } else {
             if ($content === '-') {
                 $classes[] = 'divider';
             } else {
                 if (preg_match("/##(.*)?##/", $content, $header_matches)) {
                     $classes[] = 'dropdown-header';
                     $content = trim($header_matches[1]);
                 }
             }
         }
         $attrs[] = $classes ? ' class="' . trim(implode(' ', $classes)) . '"' : '';
         $item_html = '<li' . ($attrs ? ' ' . implode(' ', $attrs) : '') . '>' . $content . '</li>';
         $items_html .= $item_html;
     }
     $classes = array('dropdown-menu');
     if ($new_options['multilevel']) {
         $classes[] = 'multi-level';
     }
     $main_attrs[] = 'role="menu"';
     if ($new_options['class']) {
         $classes[] = $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'] . '"';
     }
     $main_attrs[] = 'class="' . implode(' ', $classes) . '"';
     $result = '<ul ' . implode(' ', $main_attrs) . '>';
     $result .= $items_html;
     $result .= '</ul>';
     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" => array(), "content" => true);
                if (isset($structure->each['row']) && $structure->each['row']) {
                    $structure->row[$row_index + 1] = SmartUtil::set_closure_prop_def($row_prop, $structure->each['row'], array($row_data, $row_index), 'class');
                }
                $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) {
                    $cell_classes = array();
                    $cell_attrs = array();
                    if (isset($structure->hide[$col_name]) && $structure->hide[$col_name] === true || in_array($col_name, $structure->hidden)) {
                        $cell_classes[] = 'hidden';
                    }
                    if (isset($new_row_prop["content"]) && !$new_row_prop["content"]) {
                        $rows_html .= '<td class="' . implode(' ', $cell_classes) . '"></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, &$cell_classes, &$cell_attrs) {
                            //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($that, $row_data, $cell_html) {
                                    $cell_html = $that::replace_col_codes($content, $row_data);
                                    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="' . SmartUI::$icon_source . ' ' . $icon_value . '"></i> ' . $cell_html;
                                }, "if_other" => function ($icon) use($cell_html) {
                                    return '<i class="' . SmartUI::$icon_source . ' ' . $icon . '"></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>';
                                }));
                            }
                            //class
                            if (isset($cell_prop["class"])) {
                                if (is_array($cell_prop["class"])) {
                                    $cell_classes = array_merge($cell_classes, $cell_prop["class"]);
                                } else {
                                    $cell_classes[] = $cell_prop["class"];
                                }
                            }
                            if (isset($cell_prop["attr"])) {
                                if (is_array($cell_prop["attr"])) {
                                    foreach ($cell_prop["attr"] as $attr => $value) {
                                        $cell_attrs[] = $attr . '="' . $value . '"';
                                    }
                                } else {
                                    $cell_attrs[] = $cell_prop["attr"];
                                }
                            }
                            //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' . ($cell_classes ? ' class="' . implode(' ', $cell_classes) . '"' : '') . ($cell_attrs ? ' ' . implode(' ', $cell_attrs) : '') . '> ' . $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';
                }
                $attrs = array();
                if (is_array($new_row_prop["attr"])) {
                    foreach ($new_row_prop["attr"] as $attr => $value) {
                        $attrs[] = $attr . '="' . $value . '"';
                    }
                } else {
                    $attrs[] = $new_row_prop["attr"];
                }
                $attr = $attrs ? ' ' . implode(' ', $attrs) : '';
                $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 table-checkbox" width="20px"> ' . $content . ' </td>';
                }
                if (isset($structure->options["row_details"]) && $structure->options["row_details"]) {
                    $option = $structure->options["row_details"];
                    $detail_prop = array("id" => "", "icon" => SmartUI::$icon_source . '-chevron-right', "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="' . SmartUI::$icon_source . ' ' . $detail_prop['icon'] . ' ' . SmartUI::$icon_source . '-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 '';
        }));
        if ($structure->options['columns']) {
            $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("title" => $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), 'title');
                    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['title'] . ' </th>';
                }
                $html_cols = implode('', $html_col_list);
                $checkbox_header = '';
                $detail_header = '';
                if (isset($structure->options["checkboxes"]) && $structure->options["checkboxes"]) {
                    $checkbox_header = '
							<th class="center table-checkbox" 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>';
            }));
        } else {
            $cols = '';
        }
        $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 . '"' : '';
        $classes = array();
        if ($structure->options['table']) {
            $classes[] = 'table';
        }
        if ($structure->options['striped']) {
            $classes[] = 'table-striped';
        }
        if ($structure->options['bordered']) {
            $classes[] = 'table-bordered';
        }
        if ($structure->options['hover']) {
            $classes[] = 'table-hover';
        }
        if ($structure->options['condensed']) {
            $classes[] = 'table-condensed';
        }
        if ($structure->options['forum']) {
            $classes[] = 'table-forum';
        }
        if ($structure->class) {
            $classes[] = is_array($structure->class) ? implode(' ', $structure->class) : $structure->class;
        }
        $table_html = '<table ' . $id . ' class="' . implode(' ', $classes) . '">';
        $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;
        }
    }
예제 #4
0
 public static function print_dropdown($items, $multi_level = false, $return = false)
 {
     $get_property_value = self::_get_property_value_func();
     $items_html = '';
     foreach ($items as $item) {
         $item_html = '';
         $item_prop = array('content' => '', 'submenu' => array(), 'class' => '');
         $new_item_prop = $get_property_value($item, array('if_array' => function ($item) use($item_prop) {
             return SmartUtil::set_array_prop_def($item_prop, $item, 'content');
         }, 'if_closure' => function ($item) use($item_prop) {
             return SmartUtil::set_closure_prop_def($item_prop, $item);
         }, 'if_other' => function ($item) use($item_prop) {
             $item_prop['content'] = $item;
             return $item_prop;
         }));
         $classes = array();
         if ($new_item_prop['class']) {
             $classes[] = $new_item_prop['class'];
         }
         $content = $new_item_prop['content'];
         if ($new_item_prop['submenu']) {
             $content .= self::print_dropdown($new_item_prop['submenu'], false, true);
             $classes[] = 'dropdown-submenu';
         } else {
             if ($content === '-') {
                 $classes[] = 'divider';
             }
         }
         $class = $classes ? ' class="' . trim(implode(' ', $classes)) . '"' : '';
         $item_html = '<li' . $class . '>' . $content . '</li>';
         $items_html .= $item_html;
     }
     $result = '<ul class="dropdown-menu' . ($multi_level ? ' multi-level' : '') . '" role="menu">';
     $result .= $items_html;
     $result .= '</ul>';
     if ($return) {
         return $result;
     } else {
         echo $result;
     }
 }