Skip to content

Commit e48d074

Browse files
committed
added admin action logs, improved admin page, creating a new websites now copies system configuration from website 1
1 parent 75aa8ba commit e48d074

File tree

9 files changed

+317
-49
lines changed

9 files changed

+317
-49
lines changed

app/base/abstracts/AdminPage.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use \Symfony\Component\HttpFoundation\Response;
1515
use \Psr\Container\ContainerInterface;
1616
use \App\Base\Traits\AdminTrait;
17+
use \App\Site\Routing\RouteInfo;
18+
use \App\Site\Models\AdminActionLog;
1719
use \App\App;
1820

1921
/**
@@ -77,6 +79,33 @@ protected function beforeRender()
7779
return parent::beforeRender();
7880
}
7981

82+
/**
83+
* {@inheritdocs}
84+
*
85+
* @param RouteInfo|null $route_info
86+
* @param array $route_data
87+
* @return Response
88+
*/
89+
public function renderPage(RouteInfo $route_info = null, $route_data = [])
90+
{
91+
$return = parent::renderPage($route_info, $route_data);
92+
93+
if ($this->getSiteData()->getConfigValue('app/backend/log_requests') == true) {
94+
try {
95+
$log = $this->getContainer()->make(AdminActionLog::class);
96+
$log->fillWithRequest($this->getRequest(), $this);
97+
$log->persist();
98+
} catch (Exception $e) {
99+
$this->getUtils()->logException($e, "Can't write AdminActionLog");
100+
if ($this->getEnv('DEBUG')) {
101+
return $this->getUtils()->errorException($e);
102+
}
103+
}
104+
}
105+
106+
return $return;
107+
}
108+
80109
/**
81110
* {@inheritfocs}
82111
*

app/base/abstracts/FrontendPage.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,20 @@ protected function beforeRender()
165165
* @param array $route_data
166166
* @return Response
167167
*/
168-
public function process(RouteInfo $route_info = null, $route_data = [])
168+
public function renderPage(RouteInfo $route_info = null, $route_data = [])
169169
{
170-
$return = parent::process($route_info, $route_data);
171-
172-
try {
173-
$log = $this->getContainer()->make(RequestLog::class);
174-
$log->fillWithRequest($this->getRequest(), $this);
175-
$log->persist();
176-
} catch (Exception $e) {
177-
$this->getUtils()->logException($e, "Can't write RequestLog");
178-
if ($this->getEnv('DEBUG')) {
179-
return $this->getUtils()->errorException($e);
170+
$return = parent::renderPage($route_info, $route_data);
171+
172+
if ($this->getSiteData()->getConfigValue('app/frontend/log_requests') == true) {
173+
try {
174+
$log = $this->getContainer()->make(RequestLog::class);
175+
$log->fillWithRequest($this->getRequest(), $this);
176+
$log->persist();
177+
} catch (Exception $e) {
178+
$this->getUtils()->logException($e, "Can't write RequestLog");
179+
if ($this->getEnv('DEBUG')) {
180+
return $this->getUtils()->errorException($e);
181+
}
180182
}
181183
}
182184

app/base/abstracts/Model.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ abstract class Model extends ContainerAwareObject implements \ArrayAccess, \Iter
3535
*/
3636
public $tablename;
3737

38+
/** @var boolean is first save flag */
39+
protected $is_first_save;
40+
3841
/**
3942
* {@inheritdocs}
4043
*
@@ -51,6 +54,7 @@ public function __construct(ContainerInterface $container, $dbrow = null)
5154
} else {
5255
$dbrow = $this->getDb()->createRow($name);
5356
}
57+
$this->is_first_save = $this->isNew();
5458
$this->tablename = $name;
5559
$this->dbrow = $dbrow;
5660
}
@@ -300,7 +304,9 @@ public function reset()
300304
public static function load(ContainerInterface $container, $id)
301305
{
302306
$dbrow = $container->get('db')->table(static::defaultTableName(), $id);
303-
return new static($container, $dbrow);
307+
$object = new static($container, $dbrow);
308+
$object->setIsFirstSave(false);
309+
return $object;
304310
}
305311

306312
/**
@@ -309,10 +315,13 @@ public static function load(ContainerInterface $container, $id)
309315
* @param ContainerInterface $container
310316
* @return self
311317
*/
312-
public static function new(ContainerInterface $container)
318+
public static function new(ContainerInterface $container, $initialdata = [])
313319
{
314320
$dbrow = $container->get('db')->createRow(static::defaultTableName());
315-
return new static($container, $dbrow);
321+
$dbrow->setData($initialdata);
322+
$object = new static($container, $dbrow);
323+
$object->setIsFirstSave(true);
324+
return $object;
316325
}
317326

318327
/**
@@ -326,7 +335,9 @@ public static function new(ContainerInterface $container)
326335
public static function loadBy(ContainerInterface $container, $field, $value)
327336
{
328337
$dbrow = $container->get('db')->table(static::defaultTableName())->where($field, $value)->limit(1)->fetch();
329-
return new static($container, $dbrow);
338+
$object = new static($container, $dbrow);
339+
$object->setIsFirstSave(false);
340+
return $object;
330341
}
331342

332343
/**
@@ -390,6 +401,17 @@ public function __call($name, $arguments)
390401
return call_user_func_array([$this->dbrow, $name], $arguments);
391402
}
392403

404+
public function getData($column = null)
405+
{
406+
$data = $this->dbrow->getData();
407+
408+
if ($column != null) {
409+
return $data[$column] ?? null;
410+
}
411+
412+
return $data;
413+
}
414+
393415
/**
394416
* {@inheritdocs}
395417
*/
@@ -497,6 +519,8 @@ public function persist()
497519

498520
$this->postPersist();
499521

522+
$this->setIsFirstSave(false);
523+
500524
return $this;
501525
}
502526

@@ -563,4 +587,23 @@ public function postRemove()
563587
{
564588
return $this;
565589
}
590+
591+
/**
592+
*
593+
* [isFirstSave description]
594+
* */
595+
public function setIsFirstSave($is_first_save)
596+
{
597+
$this->is_first_save = $is_first_save;
598+
return $this;
599+
}
600+
601+
/**
602+
*
603+
* [isFirstSave description]
604+
* */
605+
public function isFirstSave()
606+
{
607+
return ($this->is_first_save == true);
608+
}
566609
}

app/base/tools/Utils/HtmlPartsRenderer.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,128 @@ public function renderAdminTable($elements, $header = null, BasePage $current_pa
706706
return $table;
707707
}
708708

709+
710+
public function renderLog($log)
711+
{
712+
$table = $this->getContainer()->make(
713+
TagElement::class,
714+
['options' => [
715+
'tag' => 'table',
716+
'width' => '100%',
717+
'id' => $table_id,
718+
'cellspacing' => '0',
719+
'cellpadding' => '0',
720+
'border' => '0',
721+
'attributes' => ['class' => "table table-striped"],
722+
]]
723+
);
724+
725+
$thead = $this->getContainer()->make(
726+
TagElement::class,
727+
['options' => [
728+
'tag' => 'thead',
729+
]]
730+
);
731+
$tbody = $this->getContainer()->make(
732+
TagElement::class,
733+
['options' => [
734+
'tag' => 'tbody',
735+
]]
736+
);
737+
$tfoot = $this->getContainer()->make(
738+
TagElement::class,
739+
['options' => [
740+
'tag' => 'tfoot',
741+
]]
742+
);
743+
744+
$table->addChild($thead);
745+
746+
$row = $this->getContainer()->make(
747+
TagElement::class,
748+
['options' => [
749+
'tag' => 'tr',
750+
'attributes' => ['class' => "thead-dark"],
751+
]]
752+
);
753+
754+
$fields = ['Field Name', 'Field Value'];
755+
foreach ($fields as $th) {
756+
$row->addChild(
757+
$this->getContainer()->make(
758+
TagElement::class,
759+
['options' => [
760+
'tag' => 'th',
761+
'text' => $th,
762+
'scope' => 'col',
763+
'attributes' => ['class' => 'nowrap'],
764+
]]
765+
)
766+
);
767+
}
768+
$thead->addChild($row);
769+
770+
$table->addChild($tbody);
771+
772+
$counter = 0;
773+
foreach (array_keys($log->getData()) as $property) {
774+
$handler = [$log, 'get'.$this->getUtils()->snakeCaseToPascalCase($property)];
775+
$value = call_user_func($handler);
776+
777+
$row = $this->getContainer()->make(
778+
TagElement::class,
779+
['options' => [
780+
'tag' => 'tr',
781+
'attributes' => ['class' => $counter++ % 2 == 0 ? 'odd' : 'even'],
782+
]]
783+
);
784+
785+
$row->addChild(
786+
$this->getContainer()->make(
787+
TagElement::class,
788+
['options' => [
789+
'tag' => 'td',
790+
'text' => $property,
791+
'scope' => 'col',
792+
'attributes' => ['class' => 'nowrap'],
793+
]]
794+
)
795+
);
796+
797+
if (is_scalar($value)) {
798+
$row->addChild(
799+
$this->getContainer()->make(
800+
TagElement::class,
801+
['options' => [
802+
'tag' => 'td',
803+
'text' => $value,
804+
'scope' => 'col',
805+
'attributes' => ['class' => 'nowrap'],
806+
]]
807+
)
808+
);
809+
} else {
810+
$row->addChild(
811+
$this->getContainer()->make(
812+
TagElement::class,
813+
['options' => [
814+
'tag' => 'td',
815+
'text' => "<pre>".var_export($value, true)."</pre>",
816+
'scope' => 'col',
817+
'attributes' => ['class' => 'nowrap'],
818+
]]
819+
)
820+
);
821+
}
822+
$tbody->addChild($row);
823+
}
824+
825+
$table->addChild($tfoot);
826+
827+
828+
return $table;
829+
}
830+
709831
/**
710832
* Get either a Gravatar image tag for a specified email address.
711833
*

0 commit comments

Comments
 (0)