Skip to content

Commit 586962d

Browse files
committed
heavy modifications on admin
1 parent 6d0fbc8 commit 586962d

27 files changed

+757
-877
lines changed

app/base/abstracts/AdminFormPage.php

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,6 @@ protected function beforeRender()
8080
return parent::beforeRender();
8181
}
8282

83-
/**
84-
* loads object by id
85-
*
86-
* @param integer $id
87-
* @return \App\Base\Abstracts\Model
88-
*/
89-
protected function loadObject($id)
90-
{
91-
if (!is_subclass_of($this->getObjectClass(), \App\Base\Abstracts\Model::class)) {
92-
return null;
93-
}
94-
95-
return $this->getContainer()->call([$this->getObjectClass(), 'load'], [ 'id' => $id]);
96-
}
97-
98-
/**
99-
* gets new empty model
100-
*
101-
* @return \App\Base\Abstracts\Model
102-
*/
103-
protected function newEmptyObject()
104-
{
105-
if (!is_subclass_of($this->getObjectClass(), \App\Base\Abstracts\Model::class)) {
106-
return null;
107-
}
108-
109-
return $this->getContainer()->make($this->getObjectClass());
110-
}
111-
112-
/**
113-
* adds a "new" button
114-
*/
115-
public function addNewButton()
116-
{
117-
$this->addActionLink('new-btn', 'new-btn', $this->getUtils()->getIcon('plus').' '.$this->getUtils()->translate('New', $this->getCurrentLocale()), $this->getControllerUrl().'?action=new', 'btn btn-sm btn-success');
118-
}
119-
12083
/**
12184
* get form object
12285
*
@@ -144,26 +107,47 @@ protected function fillConfirmationForm($confirm_message, $form)
144107
'suffix' => '<br /><br />',
145108
]
146109
)
147-
->addMarkup('<a class="btn btn-danger btn-sm" href="'.$this->getControllerUrl().'">'.$this->getUtils()->translate('Cancel', $this->getCurrentLocale()).'</a>')
148-
->addField(
149-
'button',
150-
[
151-
'type' => 'submit',
152-
'container_tag' => null,
153-
'prefix' => '&nbsp;',
154-
'value' => 'Confirm',
155-
'attributes' => ['class' => 'btn btn-primary btn-sm'],
156-
]
157-
);
110+
->addMarkup('<a class="btn btn-danger btn-sm" href="'.$this->getControllerUrl().'">'.$this->getUtils()->translate('Cancel', $this->getCurrentLocale()).'</a>');
111+
$this->addSubmitButton($form, true);
158112
return $form;
159113
}
160114

161115
/**
162-
* gets object to show class name for loading
116+
* adds submit button to form
163117
*
164-
* @return string
118+
* @param FAPI\Form $form
119+
* @param boolean $inline_button
120+
* @return FAPI\Form
165121
*/
166-
abstract public function getObjectClass();
122+
protected function addSubmitButton(FAPI\Form $form, $inline_button = false)
123+
{
124+
if ($inline_button) {
125+
$form
126+
->addField(
127+
'button',
128+
[
129+
'type' => 'submit',
130+
'container_tag' => null,
131+
'prefix' => '&nbsp;',
132+
'value' => 'Ok',
133+
'attributes' => ['class' => 'btn btn-primary btn-sm'],
134+
]
135+
);
136+
} else {
137+
$form
138+
->addField(
139+
'button',
140+
[
141+
'type' => 'submit',
142+
'value' => 'Ok',
143+
'container_class' => 'form-item mt-3',
144+
'attributes' => ['class' => 'btn btn-primary btn-lg btn-block'],
145+
]
146+
);
147+
}
148+
149+
return $form;
150+
}
167151

168152
/**
169153
* gets form definition object
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/**
3+
* SiteBase
4+
* PHP Version 7.0
5+
*
6+
* @category CMS / Framework
7+
* @package Degami\Sitebase
8+
* @author Mirko De Grandis <degami@github.com>
9+
* @license MIT https://opensource.org/licenses/mit-license.php
10+
* @link https://github.com/degami/sitebase
11+
*/
12+
namespace App\Base\Abstracts;
13+
14+
use \Psr\Container\ContainerInterface;
15+
use \Symfony\Component\HttpFoundation\Response;
16+
use \App\Base\Abstracts\AdminPage;
17+
use \Degami\PHPFormsApi as FAPI;
18+
use \Degami\PHPFormsApi\Accessories\TagElement;
19+
use \App\App;
20+
21+
/**
22+
* Base for admin page that manages a Frontend Model
23+
*/
24+
abstract class AdminManageFrontendModelsPage extends AdminManageModelsPage
25+
{
26+
/**
27+
* {@inheriydocs}
28+
*
29+
* @param ContainerInterface $container
30+
*/
31+
public function __construct(ContainerInterface $container)
32+
{
33+
parent::__construct($container);
34+
}
35+
36+
protected function addFrontendFormElements(FAPI\Form $form, &$form_state, $form_elements = ['url', 'website_id', 'locale'])
37+
{
38+
$object = $this->getObject();
39+
40+
$form_elements = array_intersect($form_elements, ['url', 'website_id', 'locale']);
41+
42+
$object_url = $object_website = $object_locale = '';
43+
if ($object instanceof FrontendModel && $object->isLoaded()) {
44+
$languages = $this->getUtils()->getSiteLanguagesSelectOptions($object->getWebsiteId());
45+
46+
foreach ($form_elements as $key => $element) {
47+
${"object_".$element} = $object->{$element};
48+
}
49+
} else {
50+
$languages = $this->getUtils()->getSiteLanguagesSelectOptions();
51+
}
52+
53+
$websites = $this->getUtils()->getWebsitesSelectOptions();
54+
55+
$fieldset = $form->addField(
56+
'frontend',
57+
[
58+
'type' => 'fieldset',
59+
'title' => 'Frontend',
60+
'collapsible' => true,
61+
'collapsed' => false,
62+
]
63+
);
64+
65+
if (in_array('url', $form_elements)) {
66+
$fieldset->addField(
67+
'url',
68+
[
69+
'type' => 'textfield',
70+
'title' => 'Url key',
71+
'default_value' => $object_url,
72+
'validate' => ['required'],
73+
]
74+
);
75+
}
76+
77+
if (in_array('website_id', $form_elements)) {
78+
$fieldset->addField(
79+
'website_id',
80+
[
81+
'type' => 'select',
82+
'title' => 'Website',
83+
'default_value' => $object_website,
84+
'options' => $websites,
85+
'validate' => ['required'],
86+
]
87+
);
88+
}
89+
90+
if (in_array('locale', $form_elements)) {
91+
$fieldset->addField(
92+
'locale',
93+
[
94+
'type' => 'select',
95+
'title' => 'Locale',
96+
'default_value' => $object_locale,
97+
'options' => $languages,
98+
'validate' => ['required'],
99+
]
100+
);
101+
}
102+
103+
return $form;
104+
}
105+
106+
protected function addSeoFormElements(FAPI\Form $form, &$form_state)
107+
{
108+
$object = $this->getObject();
109+
110+
$object_meta_description = $object_meta_keywords = $object_html_title = '';
111+
if ($object instanceof FrontendModel && $object->isLoaded()) {
112+
$object_meta_description = $object->meta_description;
113+
$object_meta_keywords = $object->meta_keywords;
114+
$object_html_title = $object->html_title;
115+
}
116+
117+
$fieldset = $form->addField(
118+
'seo',
119+
[
120+
'type' => 'fieldset',
121+
'title' => 'SEO',
122+
'collapsible' => true,
123+
'collapsed' => true,
124+
]
125+
);
126+
127+
$fieldset->addField(
128+
'meta_description',
129+
[
130+
'type' => 'textfield',
131+
'title' => 'Meta Description',
132+
'default_value' => $object_meta_description,
133+
]
134+
)
135+
->addField(
136+
'meta_keywords',
137+
[
138+
'type' => 'textfield',
139+
'title' => 'Meta Keywords',
140+
'default_value' => $object_meta_keywords,
141+
]
142+
)
143+
->addField(
144+
'html_title',
145+
[
146+
'type' => 'textfield',
147+
'title' => 'Html Title',
148+
'default_value' => $object_html_title,
149+
]
150+
);
151+
152+
153+
return $form;
154+
}
155+
}

app/base/abstracts/AdminManageModelsPage.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
abstract class AdminManageModelsPage extends AdminFormPage
2525
{
26+
protected $objectInstance = null;
27+
2628
/**
2729
* {@inheriydocs}
2830
*
@@ -60,6 +62,27 @@ public function __construct(ContainerInterface $container)
6062
}
6163
}
6264

65+
66+
/**
67+
* gets model object (loaded or new)
68+
*
69+
* @return mixed
70+
*/
71+
public function getObject()
72+
{
73+
if (($this->objectInstance != null) && (is_subclass_of($this->objectInstance, $this->getObjectClass()))) {
74+
return $this->objectInstance;
75+
}
76+
77+
if ($this->getRequest()->query->has($this->getObjectIdQueryParam())) {
78+
$this->objectInstance = $this->loadObject($this->getRequest()->query->get($this->getObjectIdQueryParam()));
79+
} else {
80+
$this->objectInstance = $this->newEmptyObject();
81+
}
82+
83+
return $this->objectInstance;
84+
}
85+
6386
/**
6487
* gets model table html
6588
*
@@ -97,4 +120,55 @@ protected function getTableHeader()
97120
* @return array
98121
*/
99122
abstract protected function getTableElements($data);
123+
124+
/**
125+
* loads object by id
126+
*
127+
* @param integer $id
128+
* @return \App\Base\Abstracts\Model
129+
*/
130+
protected function loadObject($id)
131+
{
132+
if (!is_subclass_of($this->getObjectClass(), \App\Base\Abstracts\Model::class)) {
133+
return null;
134+
}
135+
136+
return $this->getContainer()->call([$this->getObjectClass(), 'load'], [ 'id' => $id]);
137+
}
138+
139+
/**
140+
* gets new empty model
141+
*
142+
* @return \App\Base\Abstracts\Model
143+
*/
144+
protected function newEmptyObject()
145+
{
146+
if (!is_subclass_of($this->getObjectClass(), \App\Base\Abstracts\Model::class)) {
147+
return null;
148+
}
149+
150+
return $this->getContainer()->make($this->getObjectClass());
151+
}
152+
153+
/**
154+
* adds a "new" button
155+
*/
156+
public function addNewButton()
157+
{
158+
$this->addActionLink('new-btn', 'new-btn', $this->getUtils()->getIcon('plus').' '.$this->getUtils()->translate('New', $this->getCurrentLocale()), $this->getControllerUrl().'?action=new', 'btn btn-sm btn-success');
159+
}
160+
161+
/**
162+
* gets object to show class name for loading
163+
*
164+
* @return string
165+
*/
166+
abstract public function getObjectClass();
167+
168+
/**
169+
* defines object id query param name
170+
*
171+
* @return string
172+
*/
173+
abstract protected function getObjectIdQueryParam();
100174
}

app/base/abstracts/Model.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ public function isLoaded()
249249
return ($this->dbrow instanceof Row) && $this->dbrow->exists();
250250
}
251251

252+
/**
253+
* checks if model is new
254+
*
255+
* @return boolean
256+
*/
257+
public function isNew()
258+
{
259+
return !$this->isLoaded();
260+
}
261+
252262
/**
253263
* ensures model is loaded
254264
*

0 commit comments

Comments
 (0)