Merge branch 'feature/update-warehouse' into 'master'
update for warehouse See merge request fronk/thetool!654
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
* Class TTCrud
|
||||
* @property string $headerTitle
|
||||
* @property string $createText
|
||||
* @property string|null $historyController
|
||||
* @property array $columns
|
||||
* @property array $additionalActions
|
||||
* @property array $infoMessages
|
||||
@@ -32,7 +33,7 @@ class TTCrud extends mfBaseController {
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
|
||||
$modelName = $this->mod . 'Model';
|
||||
$modelName = str_replace('Controller', 'Model', get_class($this));
|
||||
$this->model = new $modelName();
|
||||
$this->postData = json_decode(file_get_contents('php://input'), true);
|
||||
$this->checkArray = $this->getCheckArray();
|
||||
@@ -88,6 +89,20 @@ class TTCrud extends mfBaseController {
|
||||
return $newColumn;
|
||||
}, $this->columns);
|
||||
|
||||
// check if in columns array there is a column with key "actions" and if so, we set the priority of the first column to 20 and actions to 19
|
||||
$actionsColumn = array_filter($columns, function ($column) {
|
||||
return $column['key'] === 'actions';
|
||||
});
|
||||
if (count($actionsColumn) > 0) {
|
||||
$columns = array_map(function ($column) {
|
||||
if ($column['key'] === 'actions') {
|
||||
$column['priority'] = 119;
|
||||
}
|
||||
return $column;
|
||||
}, $columns);
|
||||
$columns[0]['priority'] = 120;
|
||||
}
|
||||
|
||||
return ['key' => $this->mod,
|
||||
'tableHeader' => $this->headerTitle,
|
||||
'createText' => $this->createText,
|
||||
@@ -105,7 +120,41 @@ class TTCrud extends mfBaseController {
|
||||
$filteredAvailable = $this->model::count($filter);
|
||||
$totalRows = $this->model::count();
|
||||
|
||||
// check if any column is a autocomplete to add the text to the row
|
||||
$autoCompleteColumns = array_filter($this->columns, function ($column) {
|
||||
return isset($column['type']) && isset($column['modal']) && isset($column['modal']['type']) && $column['type'] === 'autocomplete' && $column['modal']['type'] === 'autocomplete';
|
||||
|
||||
});
|
||||
$autocompleteData = [];
|
||||
foreach ($rows as $row) {
|
||||
$row = (array) $row;
|
||||
foreach ($autoCompleteColumns as $column) {
|
||||
if (isset($autocompleteData[$column['key']][$row[$column['key']]])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if function customAutoComplete"COLUMN_KEY" is defined, we call it instead of the default
|
||||
$data = null;
|
||||
if (method_exists($this, 'customAutoComplete' . ucfirst($column['key']))) {
|
||||
$data = $this->{'customAutoComplete' . ucfirst($column['key'])}($row[$column['key']]);
|
||||
} else {
|
||||
|
||||
$autoCompleteModelName = explode('/', $column['modal']['apiUrl'])[0] . 'Model';
|
||||
$autoCompleteModel = new $autoCompleteModelName();
|
||||
$data = $autoCompleteModel::get($row[$column['key']]);
|
||||
|
||||
// TODO: fix that keys can be anything
|
||||
if (isset($data->name) && !isset($data->title)) {
|
||||
$data->title = $data->name;
|
||||
}
|
||||
}
|
||||
|
||||
$autocompleteData[$column['key']][$row[$column['key']]] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
self::returnJson(["rows" => $rows,
|
||||
"autoCompleteData" => $autocompleteData,
|
||||
"pagination" => ["page" => $page,
|
||||
"total_pages" => ceil($filteredAvailable / $perPage),
|
||||
"per_page" => $perPage,
|
||||
@@ -114,6 +163,14 @@ class TTCrud extends mfBaseController {
|
||||
}
|
||||
|
||||
protected function createAction() {
|
||||
// if this->model has property createBy, set it to the current user id and create to current epoch time
|
||||
if (property_exists($this->model, 'createBy')) {
|
||||
$this->postData['createBy'] = $this->user->id;
|
||||
}
|
||||
if (property_exists($this->model, 'create')) {
|
||||
$this->postData['create'] = time();
|
||||
}
|
||||
|
||||
Helper::validateArray($this->postData, $this->checkArray);
|
||||
|
||||
if (method_exists($this, 'beforeCreate') && !$this->beforeCreate($this->postData)) {
|
||||
@@ -178,6 +235,18 @@ class TTCrud extends mfBaseController {
|
||||
return ['value' => $item->id, 'text' => $item->$textKey];
|
||||
}, $data));
|
||||
}
|
||||
|
||||
protected function getByIdAction() {
|
||||
$id = $_GET['id'] ?? null;
|
||||
if (!$id || !is_numeric($id)) {
|
||||
http_response_code(500);
|
||||
self::returnJson(['success' => false, 'message' => 'No ID provided.']);
|
||||
die();
|
||||
}
|
||||
|
||||
$data = (array) $this->model::get($id);
|
||||
self::returnJson($data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -19,7 +19,15 @@ class TTCrudBaseModel {
|
||||
$sqlValues = [];
|
||||
foreach ($data as $field => $value) {
|
||||
if (!property_exists(get_called_class(), $field)) {
|
||||
throw new Exception("Field $field does not exist in " . get_called_class());
|
||||
die(json_encode([
|
||||
"status" => "error",
|
||||
"error" => "Field $field does not exist in " . get_called_class(),
|
||||
"data" => $data
|
||||
]));
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value);
|
||||
}
|
||||
|
||||
$sqlValues[] = $value === null ? 'NULL' : "'" . $db->real_escape_string($value) . "'";
|
||||
@@ -62,7 +70,12 @@ class TTCrudBaseModel {
|
||||
}
|
||||
|
||||
if (!isset($data[$field])) {
|
||||
throw new Exception("Required field $field is missing in data array");
|
||||
http_response_code(500);
|
||||
die(json_encode([
|
||||
"status" => "error",
|
||||
"error" => "Required field $field is missing in data array",
|
||||
"data" => $data
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,6 +117,7 @@ class TTCrudBaseModel {
|
||||
$sql = "WHERE 1=1";
|
||||
foreach ($filter as $key => $value) {
|
||||
if (!property_exists(get_called_class(), $key)) {
|
||||
http_response_code(500);
|
||||
throw new Exception("Field $key does not exist in " . get_called_class());
|
||||
}
|
||||
$sql .= Helper::generateFilterCondition($value, $key, gettype($value) === "integer");
|
||||
@@ -161,6 +175,10 @@ class TTCrudBaseModel {
|
||||
$value = null;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value);
|
||||
}
|
||||
|
||||
$values[] = $value === null ? "`$field` = NULL" : "`$field` = '" . $db->real_escape_string($value) . "'";
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,9 @@ class mfExceptionhandler {
|
||||
public function __toString() {
|
||||
$str="[".$this->Time."] ";
|
||||
if(is_numeric($this->Code) && $this->Code > 0) {
|
||||
if($this->Code == 404 || $this->Code == 500) {
|
||||
http_response_code($this->Code);
|
||||
}
|
||||
$str.="(Error code ".$this->Code.") ";
|
||||
}
|
||||
|
||||
|
||||
@@ -168,7 +168,8 @@ class mfRouter {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
$baseurl = $baseurl ?? "";
|
||||
$baseurl = preg_replace('@/$@', '', $baseurl);
|
||||
define("MFFANCYBASEURL",$baseurl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user