Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
manix committed May 2, 2019
1 parent c97870b commit 1cde48e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 62 deletions.
9 changes: 8 additions & 1 deletion src/Components/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function has(string $name): bool {
* @return FormInput
*/
public function input($name) {
return $this->inputs[$name];
return $this->inputs[$name] ?? null;
}

/**
Expand Down Expand Up @@ -171,6 +171,13 @@ public function fill($data) {
case 'password': // passwords should not be filled
break;

case 'checkbox':
case 'radio':
if ($value) {
$this->inputs[$key]->setAttribute('checked', 'checked');
}
break;

default:
$this->inputs[$key]->setAttribute('value', $value);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Components/Persistence/SQL/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ abstract class Query {
protected $joins = [];
protected $columns;
protected $where = '';
protected $whereglue = 'AND';
public $whereglue = 'AND';
public $whereglueGroup = '';
public $group = '';
public $order = '';
public $limit = '';
Expand Down Expand Up @@ -51,12 +52,14 @@ public function where($column, $operand = null, $data = null) {

public function whereGroupStart() {
$this->where .= ' ' . $this->whereglue . ' (';
$this->whereglueGroup = $this->whereglue;
$this->whereglue = '';
return $this;
}

public function whereGroupEnd() {
$this->where .= ')';
$this->whereglue = $this->whereglueGroup;
return $this;
}

Expand Down
62 changes: 43 additions & 19 deletions src/Utility/CRUD/CRUDEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,28 @@ public function put() {

return [
'success' => true,
'goto' => route(static::class, $pk_route)
'goto' => $this->getPutReturnURL($pk_route)
];
});
}

public function getPutReturnURL($pk) {
return route(static::class, $pk);
}

public function post() {
$this->cleanInput($_REQUEST);

return $this->validate($_REQUEST, function($data) {
$gate = $this->getGateway();

// just in case
$data[$gate->getAI()] = null;

$class = $gate::MODEL;
$this->model = new $class();
$this->populateModel($this->model, $data);

// just in case
unset($data[$gate->getAI()]);


if (!$gate->persist($this->model)) {
throw new Exception('Unexpected error.', 500);
Expand All @@ -222,14 +227,14 @@ public function post() {
];
});
}

public function getPostReturnURL($pk) {
return route(static::class, $pk);
}

public function getLabels() {
return [
// field => label
// field => label
];
}

Expand Down Expand Up @@ -413,7 +418,7 @@ public function getSorter(): Sorter {
public function getColumns() {
return $this->fetchColumns();
}

public function fetchColumns() {
return $this->getGateway()->getFields();
}
Expand Down Expand Up @@ -460,7 +465,7 @@ protected function getQueryFields($searchable) {

return $fields;
}

protected function getDefaultSearchComparator() {
return 'like';
}
Expand All @@ -476,7 +481,7 @@ public function getListView() {
// CRUDView automatically calls CRUDListView internally.
return CRUDListView::class;
}

/**
* Whether to display actions in listview or not
* @return boolean
Expand Down Expand Up @@ -510,20 +515,39 @@ public function getParsedRelations($alwaysList = false) {

$gate = new $rel[0];
$pk = $gate->getPK();
$relations[$field] = route($class, $alwaysList || count($pk) > 1 ? [
'fields' => $data[2] ?? $pk[0],
'query' => ''
] : [
$pk[0] => ''
]);

if (is_array($field)) {
$values = [];
$fields = [];
$model = $this->getModel();
foreach ($field as $f) {
$values[$f] = $model->$f ?? $_REQUEST[$f] ?? null;
if ($values[$f]) {
$fields[$f] = 'equals';
}
}
$relations[$key] = route($class, $alwaysList || count($pk) > 1 ? [
'fields' => is_array($data) ? $data[2] : $fields,
'query' => $values
] : [
$pk[0] => ''
]);
} else {
$relations[$field] = route($class, $alwaysList || count($pk) > 1 ? [
'fields' => $data[2] ?? $pk[0],
'query' => ''
] : [
$pk[0] => ''
]);
}
}

return $relations;
}

public function getListData() {
$definedColumns = $this->fetchColumns();

if (isset($_GET['columns'])) {
$columns = [];
foreach (is_array($_GET['columns']) ? $_GET['columns'] : explode(',', $_GET['columns']) as $col) {
Expand Down Expand Up @@ -551,7 +575,7 @@ public function getListData() {
$queryCriteria->$comparator($field, $query[$field] ?? $query);
}
}

$gate = $this->getGateway();
$gatedefaultcols = $gate->getFields();
$gatecols = [];
Expand All @@ -561,7 +585,7 @@ public function getListData() {
}
}
$gate->setFields($gatecols);

return [
$query || !$this->requireQuery() ? $gate->sort($this->getSorter())->findBy($criteria) : [],
$this->getColumns(),
Expand Down
72 changes: 40 additions & 32 deletions src/Utility/CRUD/CRUDList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ trait CRUDList {
];
public $search = true;
public $actions = true;
public $tableHead = true;

function __construct($data, HTMLGenerator $html) {
list($data, $this->fields, $this->controllerInstance, $this->pk, $this->sort, $this->order, $this->query, $this->sortable, $this->search, $this->relations) = $data;
Expand Down Expand Up @@ -63,43 +64,46 @@ public function body() {
<?php endif; ?>
<div class="table-responsive">
<table class="<?= $this->getTableClass() ?> table-crud m-0">
<thead>
<?php if (!$noQuery && !$noResults): ?>
<tr>
<?php foreach ($this->fields as $field): ?>
<th class="<?= in_array($field, $this->pk) ? 'pk' : '' ?>">
<?php if (isset($sortable[$field])): $asc = $this->sort === $field && $this->order === 'asc'; ?>
<a href="<?= $this->getSortURL($field, $asc) ?>" class="d-flex justify-content-between align-items-center">
<span><?= $this->renderColumnLabel($field) ?></span>
<?php
if ($this->sort === $field && isset($sortable[$field])):
if ($asc):
?>
<i class="fa fa-chevron-up"></i>
<?php else: ?>
<i class="fa fa-chevron-down"></i>
<?php if ($this->tableHead): ?>
<thead>
<?php if (!$noQuery && !$noResults): ?>
<tr>
<?php foreach ($this->fields as $field): ?>
<th>
<?php if (isset($sortable[$field])): $asc = $this->sort === $field && $this->order === 'asc'; ?>
<a href="<?= $this->getSortURL($field, $asc) ?>" class="d-flex justify-content-between align-items-center">
<span><?= $this->renderColumnLabel($field) ?></span>
<?php
if ($this->sort === $field && isset($sortable[$field])):
if ($asc):
?>
<i class="fa fa-chevron-up"></i>
<?php else: ?>
<i class="fa fa-chevron-down"></i>
<?php
endif;
endif;
endif;
?>
</a>
<?php else: ?>
<span><?= $this->renderColumnLabel($field) ?></span>
<?php endif; ?>
</th>
<?php endforeach; ?>
<?php if ($actions): ?>
<th>
Actions
</th>
<?php endif; ?>
</tr>
<?php endif; ?>
</thead>
?>
</a>
<?php else: ?>
<span><?= $this->renderColumnLabel($field) ?></span>
<?php endif; ?>
</th>
<?php endforeach; ?>
<?php if ($actions): ?>
<th>
Actions
</th>
<?php endif; ?>
</tr>
<?php endif; ?>
</thead>
<?php endif; ?>

<?php if (!$noResults): ?>
<tbody>
<?php foreach ($this->data as $model): ?>
<tr class="<?= $this->getRowClass($model) ?>">
<tr class="<?= $this->getRowClass($model) ?>" data-pk="<?= html($this->getRelationKey($model)) ?>">
<?php foreach ($this->fields as $field): ?>
<td class="<?= $this->getColClass($model, $field) ?>">
<?= $this->renderColumnBody($model, $field) ?>
Expand Down Expand Up @@ -131,6 +135,10 @@ public function renderPageName() {

}

public function getRelationKey($model) {
return $model->{$this->pk[0]};
}

public function getSearchForm() {
$form = new Form();
$form->setMethod('GET')->setAction(route($this->controller));
Expand Down
18 changes: 16 additions & 2 deletions src/Utility/CRUD/CRUDViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public function renderForeignSelector($input) {

$view->labels[$name] = $labels[$name] ?? ucfirst(str_replace('_', ' ', $name));

if (isset($rel[$name])) {
if (isset($rel[$this->getRelationKey($name)])) {
// $view->labels[$name] = '<i class="fa fa-link"></i> ' . $view->labels[$name];

$input->readonly = 'readonly';
$input->class = 'btn btn-light form-control text-left';
$input->{'data-url'} = $rel[$name] . $input->value;
$input->{'data-url'} = $this->getRelationURL($rel, $input);
$input->onclick = 'openForeignSelector(this)';
// $view->setCustomRenderer($input->name, [$view, 'renderForeignSelector']);
}
Expand All @@ -99,6 +99,20 @@ public function renderForeignSelector($input) {

return $view;
}

/**
* Define alternate relation names for fields
*/
public function getRelationKey($field) {
switch ($field) {
default:
return $field;
}
}

public function getRelationURL($rel, $input) {
return $rel[$this->getRelationKey($input->name)] . $input->value;
}

public function form() {
echo $this->constructFormView($this->data['form']);
Expand Down
10 changes: 3 additions & 7 deletions src/Utility/CRUD/JavaScript/SelectValueForOpener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@ public function html() {
?>
<script>
if (window.opener || window.parent !== window) {
(function (pki, w, owner) {
(function (w, owner) {
if (owner.selectForeignValue.url.indexOf(location.href.replace(location.search, "")) !== 0) {
location.href = owner.selectForeignValue.url;
}

var pk = w.document.querySelector(".table-crud thead th.pk");
while ((pk = pk.previousElementSibling) !== null) {
pki++;
}
var records = w.document.querySelectorAll(".table-crud tbody tr");
var select = function (e) {
e.preventDefault();
owner.selectForeignValue(this.children[pki].textContent.trim());
owner.selectForeignValue(this.dataset.pk.trim());
w.close();
};
for (var i = 0, l = records.length; i < l; i++) {
records[i].onclick = select;
}
})(0, window, window.opener || window.parent);
})(window, window.opener || window.parent);


document.addEventListener("keypress", function (e) {
Expand Down

0 comments on commit 1cde48e

Please sign in to comment.