diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0d17920 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "focalstrategy/core", + "description": "Core Library for Focal Strategy's components", + "type": "library", + "authors": [ + { + "name": "Shane Glee", + "email": "shane@focalstrategy.com" + } + ], + "require": { + "myclabs/php-enum": "^1.5" + }, + "autoload": { + "psr-4": { + "FocalStrategy\\Core\\": "src" + } + } +} diff --git a/src/BaseBgType.php b/src/BaseBgType.php new file mode 100644 index 0000000..08455ca --- /dev/null +++ b/src/BaseBgType.php @@ -0,0 +1,10 @@ + 'btn ' + ]; + + public function __construct($text, $url, BaseBtnType $action_type) + { + $this->text = $text; + + if ($url != null) { + $this->attributes['href'] = $url; + } + + $this->attributes['class'] .= $action_type; + } + + public function addDataAttributes(array $data_attributes) : Button + { + $this->data_attributes = $data_attributes; + return $this; + } + + public function mergeAttributes(array $attributes) : Button + { + foreach ($attributes as $key => $a) { + if (isset($this->attributes[$key])) { + $this->attributes[$key] .= ' '.$a; + } else { + $this->attributes[$key] = $a; + } + } + + return $this; + } + + public function getText() : string + { + return $this->text; + } + + public function isButton() : bool + { + return !isset($this->attributes['href']); + } + + public function getRoute() : string + { + return $this->attributes['href'] ?? ''; + } + + public function attributes() : string + { + $result = ''; + foreach ($this->attributes as $key => $attr) { + $result .= ' '.$key.'="'.$attr.'"'; + } + + return $result; + } + + public function getDataAttributes() : string + { + $result = ''; + foreach ($this->data_attributes as $key => $attr) { + $result .= ' '.$key.'="'.$attr.'"'; + } + + return $result; + } + + public function setTarget($target) : Button + { + $this->attributes['target'] = $target; + + return $this; + } + + public function setUrlTemplate(string $template) : Button + { + $this->url_template = $template; + + return $this; + } + + public function addData(array $data) + { + if ($this->url_template) { + $url = $this->url_template; + foreach ($data as $key => $value) { + $url = str_replace('+'.$key.'+', $value, $url); + $url = str_replace('%2B'.$key.'%2B', $value, $url); + } + $this->attributes['href'] = $url; + } + + if ($this->text) { + $text = $this->text; + foreach ($data as $key => $value) { + if (!is_array($value)) { + $text = str_replace('+'.$key.'+', $value, $text); + $text = str_replace('%2B'.$key.'%2B', $value, $text); + } + } + $this->text = $text; + } + } + + public function hasAttribute($key) + { + return isset($this->attributes[$key]); + } + + public function render() + { + return view('_components.button')->with('button', $this); + } +} diff --git a/src/Buttons/ButtonGroupDropdown.php b/src/Buttons/ButtonGroupDropdown.php new file mode 100644 index 0000000..9cab767 --- /dev/null +++ b/src/Buttons/ButtonGroupDropdown.php @@ -0,0 +1,73 @@ + 'btn ' + ]; + + public function __construct($text, $url, BaseBtnType $action_type, $buttons) + { + $this->text = $text; + + if ($url != null) { + $this->attributes['href'] = $url; + } + + $this->attributes['class'] .= $action_type; + $this->buttons = $buttons; + } + + public function mergeAttributes(array $attributes) + { + foreach ($attributes as $key => $a) { + if (isset($this->attributes[$key])) { + $this->attributes[$key] .= ' '.$a; + } else { + $this->attributes[$key] = $a; + } + } + } + + public function getText() + { + return $this->text; + } + + public function isButton() + { + return !isset($this->attributes['href']); + } + + public function getRoute() + { + return $this->attributes['href'] ?? ''; + } + + public function attributes() + { + $result = ''; + foreach ($this->attributes as $key => $attr) { + $result .= ' '.$key.'="'.$attr.'"'; + } + + return $result; + } + + public function setTarget($target) + { + $this->attributes['target'] = $target; + } + + public function render() + { + return view('_components.button_group_dropdown')->with('button', $this); + } +} diff --git a/src/Buttons/ImageButton.php b/src/Buttons/ImageButton.php new file mode 100644 index 0000000..90d2cc2 --- /dev/null +++ b/src/Buttons/ImageButton.php @@ -0,0 +1,26 @@ +img_url = $img_url; + } + + public function render() + { + return view('_components.image_button')->with('button', $this); + } + + public function getImage() + { + return $this->img_url; + } +} diff --git a/src/Buttons/PostButton.php b/src/Buttons/PostButton.php new file mode 100644 index 0000000..3ed610c --- /dev/null +++ b/src/Buttons/PostButton.php @@ -0,0 +1,26 @@ +data = $data; + } + + public function render() + { + return view('_components.post_button')->with('button', $this); + } + + public function data() + { + return $this->data; + } +} diff --git a/src/CoreServiceProvider.php b/src/CoreServiceProvider.php new file mode 100644 index 0000000..d520681 --- /dev/null +++ b/src/CoreServiceProvider.php @@ -0,0 +1,30 @@ +loadViewsFrom(__DIR__.'/views', 'core'); + $this->publishes([ + __DIR__.'/views' => resource_path('views/vendor/core'), + ]); + } +} diff --git a/src/Page.php b/src/Page.php new file mode 100644 index 0000000..8fb0b59 --- /dev/null +++ b/src/Page.php @@ -0,0 +1,224 @@ +html_page_title = $html_page_title; + + return $this; + } + + public function setTitle($page_title) + { + $this->page_title = $page_title; + + return $this; + } + + public function setSubTitle($page_sub_title) + { + $this->page_sub_title = $page_sub_title; + + return $this; + } + + public function setHideSideBar($hide_side_bar) + { + $this->hide_side_bar = $hide_side_bar; + } + + public function addButton($text, $route, BtnType $type) + { + $this->addRenderable(new Button($text, $route, $type)); + } + + public function addRenderable(RenderableComponent $renderable) + { + $this->renderables[] = $renderable; + } + + public function withTable(DataTable $data_table) + { + $this->data_table = $data_table; + } + + public function view($view, array $withs = []) + { + $this->view = $view; + $this->with($withs); + + return $this; + } + + public function breadcrumb(string $text, string $link = '') + { + $this->breadcrumbs[] = [$link, $text]; + } + + public function withVoArray(array $data) + { + foreach ($data as $key => $value) { + $this->checkAllowed($value); + } + + $this->with = array_merge($this->with, $data); + + return $this; + } + + public function withVoNamed($key, $value) + { + $this->checkAllowed($value); + + $this->with[$key] = $value; + + return $this; + } + + public function withArray(array $data) + { + $this->with = array_merge($this->with, $data); + + return $this; + } + + public function withNamed($key, $value) + { + $this->with[$key] = $value; + + return $this; + } + + public function validate() + { + if (!isset($this->with['dev_errors'])) { + $this->with['dev_errors'] = []; + } + + if (!View::exists($this->view)) { + $this->with['dev_errors'][] = 'View "'.$this->view.'" does not exist'; + } + + if (count($this->breadcrumbs) == 0) { + $this->with['dev_errors'][] = "View has no breadcrumbs"; + } + + if (empty($this->html_page_title)) { + $this->with['dev_errors'][] = "HTML Page title not set"; + } + + if (empty($this->page_title)) { + $this->with['dev_errors'][] = "Page title not set"; + } + + if ($this->non_view_object) { + $this->with['dev_errors'][] = "Use of a non view object detected"; + } + + if ($this->renderables) { + $this->with['header_renderables'] = $this->renderables; + } + } + + public function render() + { + $this->validate(); + + if ($this->html_page_title && !isset($this->with['html_page_title'])) { + $this->with['html_page_title'] = $this->html_page_title; + } + + if ($this->page_title && !isset($this->with['page_title'])) { + $this->with['page_title'] = $this->page_title; + } + + if ($this->page_sub_title && !isset($this->with['page_sub_title'])) { + $this->with['page_sub_title'] = $this->page_sub_title; + } + + if (!isset($this->with['hide_side_bar'])) { + $this->with['hide_side_bar'] = $this->hide_side_bar; + } + + if (count($this->breadcrumbs) > 0) { + $this->with['breadcrumbs'] = $this->breadcrumbs; + } + + if (!View::exists($this->view)) { + return view('core::page') + ->with($this->with) + ->with('data_dump', $this->with) + ->render(); + } + + if ($this->data_table != null) { + return $this->data_table->render($this->view, $this->with); + } + + return View::make($this->view) + ->with($this->with) + ->render(); + } + + //Buttons + //Create a Template and include the view + + public function __call($name, $args) + { + if ($name == 'with' || $name == 'withVo') { + if (count($args) > 0) { + if (count($args) > 1) { + $name .= 'Named'; + } else { + $name .= 'Array'; + } + } + return call_user_func_array(array($this, $name), $args); + } + } + + private function checkAllowed($data) + { + if (is_array($data)) { + if (count($data) > 0) { + if (!(array_values($array)[0] instanceof ViewObject)) { + $this->non_view_object = true; + } + } + } + + if ($data instanceof EloquentCollection + || $data instanceof SupportCollection) { + if (count($data) > 0) { + if (!($data->first() instanceof ViewObject)) { + $this->non_view_object = true; + } + } + } + } +} diff --git a/src/ReceivesData.php b/src/ReceivesData.php new file mode 100644 index 0000000..c595809 --- /dev/null +++ b/src/ReceivesData.php @@ -0,0 +1,8 @@ + + + @if(Config::get('app.debug')) + @if(isset($dev_errors) && count($dev_errors) > 0) + @foreach($dev_errors as $error) +
+ Developer Notice + {{ $error }} +
+ @endforeach + @endif + @endif + + @section('page_content') + @if(isset($data_dump)) +
+
+
{{ json_encode($data_dump,JSON_PRETTY_PRINT | JSON_HEX_TAG) }}
+
+
+ @endif + @show + +@endsection \ No newline at end of file