Skip to content

Commit

Permalink
Update Form and implement Sql Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Nov 30, 2024
1 parent 7eab94f commit b9423b3
Show file tree
Hide file tree
Showing 5 changed files with 592 additions and 3 deletions.
122 changes: 122 additions & 0 deletions src/Driver/Model/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use CrazyPHP\Library\Time\DateTime;
use CrazyPHP\Library\Model\Schema;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\Database\Operation\SqlOperation;
use CrazyPHP\Library\Form\Process;

/* use PhpMyAdmin\SqlParser\Statements\SelectStatement;
use PhpMyAdmin\SqlParser\Parser; */
Expand Down Expand Up @@ -59,6 +61,12 @@ class Mariadb implements CrazyDriverModel {
/** @var bool $attributesAsValues Indicate if attributes is set as values in current schema */
# private bool $attributesAsValues = false;

/** @var array|null $field to retrieve */
private $_fields = null;

/** @var null|array conditions */
private array|null $conditions = null;

/**
* Constructor
*
Expand Down Expand Up @@ -139,6 +147,10 @@ public function createCollection():self {
*/
public function parseId(string|int $id, ?array $options = null):self {

# Ingest options
$this->_ingestFields($options);
$this->_ingestPageStateProcess($options);

# Store id
$this->id = is_int($id)
? $id
Expand All @@ -159,6 +171,21 @@ public function parseId(string|int $id, ?array $options = null):self {
*/
public function parseFilter(?array $filters, ?array $options = null):self {

# Ingest options
$this->_ingestFields($options);
$this->_ingestPageStateProcess($options);

# Check filters
if(isset($filters) && is_array($filters)){

# Process Operations In Filters
$filters = $this->_processOperationsInFilters($filters);

# Push filters in filters
$this->conditions[] = $filters;

}

# Return self
return $this;

Expand All @@ -173,6 +200,10 @@ public function parseFilter(?array $filters, ?array $options = null):self {
*/
public function parseSort(null|array|string $sort, ?array $options = null):self {

# Ingest options
$this->_ingestFields($options);
$this->_ingestPageStateProcess($options);

# Return self
return $this;

Expand All @@ -186,6 +217,10 @@ public function parseSort(null|array|string $sort, ?array $options = null):self
*/
public function parseGroup(?array $group, ?array $options = null):self {

# Ingest options
$this->_ingestFields($options);
$this->_ingestPageStateProcess($options);

# Return self
return $this;

Expand All @@ -200,6 +235,10 @@ public function parseGroup(?array $group, ?array $options = null):self {
*/
public function parseSql(string $sql, ?array $options = null):self {

# Ingest options
$this->_ingestFields($options);
$this->_ingestPageStateProcess($options);

# Raw query
$this->rawQuery = $sql;

Expand All @@ -219,6 +258,22 @@ public function parseSql(string $sql, ?array $options = null):self {

}

/**
* Ingest pageStateProcess
*
* @param ?array $options
* @return void
*/
private function _ingestPageStateProcess(?array $options = null):void {

# Check options
if($options !== null && isset($options["pageStateProcess"]) && Process::bool($options["pageStateProcess"]) == true)

# Switch value in arguments
$this->arguments["pageStateProcess"] = true;

}

/** Public methods | Ingester
******************************************************
*/
Expand Down Expand Up @@ -469,6 +524,41 @@ public function _pageStateProcess(array $input):array {

}

/**
* Process Operations In Filters
*
* @param array $input
* @return array
*/
private function _processOperationsInFilters(array $filters = []):array {

# Set result
$result = $filters;

# Check filters
if(!empty($result)){

# New operations
$operation = new SqlOperation();

# Iteration filters
foreach($result as &$value)

# Check if value is string
if(is_string($value) && strpos($value, "*") !== false){

# Run operation
$value = $operation->run($value);

}

}

# Return result
return $result;

}

/**
* Ingest Parameters
*
Expand Down Expand Up @@ -563,6 +653,38 @@ private function isUpdate():bool {

}

/**
* Ingest Fields
*
* @param ?array $options
* @return void
*/
private function _ingestFields(?array $options = null):void {

# Check options
if($options !== null && isset($options["fields"]) && !empty($options["fields"]))

# check if string
if(is_string($options["fields"]) && ($this->_fields === null || !in_array($options["fields"], $this->_fields)))

# Push in fileds
$this->_fields[] = $options["fields"];

else
# Check if array
if(is_array($options["fields"]))

# Iteration values
foreach($options["fields"] as $field)

# If not already on fields
if(!in_array($field, $this->_fields))

# Push in fields
$this->_fields[] = $field;

}

/** Public constants
******************************************************
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Front/Library/Utility/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2027,12 +2027,32 @@ export default class Form {

// Fetch all

// Add options found
).then(
value => {

// Add options to tom
selectInstance.addOptions(value.results);

}
// Check default and set it
).then(
() => {

// Check default in input el
if(inputEl.hasAttribute("default")){

// Get default
let defaultValue = inputEl.getAttribute("default");

// Check type of default value
if(typeof defaultValue === "string")

// Set value
selectInstance.setValue(defaultValue);

}

}
);

Expand Down
Loading

0 comments on commit b9423b3

Please sign in to comment.