Skip to content

Commit

Permalink
Improve mariadb driver, form and partial
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Dec 2, 2024
1 parent 670ba70 commit 85bc537
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 21 deletions.
7 changes: 4 additions & 3 deletions resources/Hbs/Partials/form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{{!-- data-type="color" --}}
{{#if item.placeholder}}placeholder="{{item.placeholder}}"{{else}}placeholder=" "{{/if}}
{{#if item.readonly}}readonly=""{{/if}}
{{#if item.default includeZero=false}}value="{{item.default}}" default="{{item.default}}"{{/if}}
{{#if item.default includeZero=false}}value="{{#is item.default "randomHex()"}}{{colorHexRandom}}{{else}}{{item.default}}{{/is}}" default="{{item.default}}"{{/if}}
{{#if item.select.[0].value includeZero=false}}min="{{item.select.[0].value}}"{{/if}}
{{#if item.select.[1].value includeZero=false}}max="{{item.select.[1].value}}"{{/if}}
{{#if item.required}}required=""{{/if}}
Expand All @@ -64,14 +64,15 @@
autocomplete="off"
{{#if item.placeholder}}placeholder="{{item.placeholder}}"{{/if}}
{{#if item.disabled}}disabled=""{{else}}{{#if ../form.onready}}disabled="loading"{{/if}}{{/if}}
{{#if item._style.select.tag}}/>{{else}}>{{#if item.select}}
{{#if item.select.url}}data-select-remote="{{JSONstringify item.select}}"{{/if}}
{{#if item._style.select.tag}}/>{{else}}>{{#if item.select}}{{#if item.select.url}}{{else}}
{{#each item.select as |option|}}<option
{{#if option.value includeZero=false}}value="{{option.value}}"{{/if}}
{{#if item.default includeZero=false}}{{#is item.default option.value}}selected=""{{/is}}{{else}}{{#if option.default}}selected=""{{/if}}{{/if}}
{{#if option.default}}default=""{{/if}}
{{#if option.disabled}}disabled=""{{/if}}
>{{option.label}}</option>{{/each}}
{{/if}}</select>{{/if}}
{{/if}}{{/if}}</select>{{/if}}

{{!-- Password Input --}}
{{else}}{{#is item.type "password"}}
Expand Down
17 changes: 15 additions & 2 deletions src/Driver/Model/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class Mariadb implements CrazyDriverModel {
/** @var array|null $field to retrieve */
private $_fields = null;

/** @var bool $_delete */
private $_delete = false;

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

Expand Down Expand Up @@ -317,6 +320,9 @@ public function ingestData(array $data, ?array $options = null):self {
*/
public function pushToTrash(?array $options = null):self {

# Swith delete
$this->_delete = true;

# Return self
return $this;

Expand Down Expand Up @@ -347,7 +353,14 @@ public function run():array {
]);

}else
# Insert to mongo Check schema
# Delete item by id
if($this->_delete && $this->id !== null){

# Insert
$result[] = $this->mariadb->deleteOneToCollection($this->arguments["table"], $this->id);

}else
# Insert to maria db Check schema
if($this->schema !== null && $this->id !== null){

# Check collection
Expand Down Expand Up @@ -694,7 +707,7 @@ private function _ingestFields(?array $options = null):void {
"table" => "",
"schema" => [],
"database" => [],
"pageStateProcess" => false,
"pageStateProcess" => false,
];

}
54 changes: 53 additions & 1 deletion src/Front/Library/Crazypartial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ export default abstract class Crazypartial {
******************************************************
*/

/**
* Input
*/
public input:RegisterPartialScanned;

/**
* @param html:string
* Duplicate of the class name because build change name of class
*/
public static readonly html:string|null|CallableFunction = null;
public html:string|null|CallableFunction = null;

/**
* Constructor
Expand All @@ -48,12 +51,61 @@ export default abstract class Crazypartial {
// Set input
this.input = input;

// Check id and target
if(typeof this.input.id === "number" && this.input.target instanceof HTMLElement)

// Set id on target
this.input.target.dataset.partialId = this.input.id.toString();

}

/** Protected methods
******************************************************
*/

/**
* Reload
*
* @param state:Object|null
*/
public reload = (state:Object|null = null) => {

// Check target
if(this.input.target instanceof HTMLElement && this.html !== null){

// Set data
var htmlString = "";

// Check html
if(typeof state === "object" && typeof this.html === "function")

// Get string
htmlString = this.html(typeof state === "object" ? state : {});

// Get content dom
var contentDom = document.createRange().createContextualFragment(htmlString);

// Reload partial
this.input.target.replaceWith(contentDom);

}

// Execute on ready
this.onReady();

}

/** Public methods
******************************************************
*/

/**
* On Ready
*/
public onReady = () => {

}

/**
* Enable
*/
Expand Down
88 changes: 81 additions & 7 deletions src/Front/Library/Utility/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ export default class Form {
let currentName:string;
let currentType:string;


// Get all select and input on form el
let items = this._formEl.querySelectorAll("select, input");
let items = this._formEl.querySelectorAll("select[name], input[name]");

// Check items
if(items.length)
Expand All @@ -193,6 +192,14 @@ export default class Form {
// Get type
currentType = items[i]["type"] ?? "";

// Check if data type
// @ts-ignore
if("type" in items[i].dataset && items[i].dataset.type)

// Override type
// @ts-ignore
currentType = items[i].dataset.type;

// Check if in values
if(Object.keys(values).includes(currentName)){

Expand Down Expand Up @@ -458,9 +465,6 @@ export default class Form {
// Get formdata
let formData:FormData = this.getFormData(target);

console.log("--dev--");
console.log(formData);

// Lock form
this.lock();

Expand Down Expand Up @@ -1452,6 +1456,75 @@ export default class Form {

}

/**
* Set Select
*
* Set select in item
*
* @param itemEl:HTMLElement
* @param value:string
* @return void
*/
private selectSet = (itemEl:HTMLElement, value:string, valuesID:string|Object|null):void => {

console.log("toto");

// Check itemEl
if(["INPUT", "SELECT"].includes(itemEl.tagName)){

// Check if tomselect in item
if("tomselect" in itemEl && itemEl.tomselect instanceof TomSelect){

// Set value
itemEl.tomselect.setValue(value);

// Set id
this._setID(valuesID, itemEl);

}else{

// Set value
itemEl.setAttribute("value", value);

// Dispatch event change
itemEl.dispatchEvent(new Event("change"));

// Set id
this._setID(valuesID, itemEl);

}

}

}

/**
* Set Number
*
* Set number in item
*
* @param itemEl:HTMLElement
* @param value:string
* @return void
*/
private numberSet = (itemEl:HTMLElement, value:string, valuesID:string|Object|null):void => {

// Check itemEl
if(["INPUT", "SELECT"].includes(itemEl.tagName)){

// Set value
itemEl.setAttribute("value", value);

// Dispatch event change
itemEl.dispatchEvent(new Event("change"));

// Set id
this._setID(valuesID, itemEl);

}

}

/** Private methods | Set value | Set Custom Data
******************************************************
*/
Expand Down Expand Up @@ -1871,17 +1944,18 @@ export default class Form {
var currentValue = inputEl.getAttribute("value");

// Check current value
if(currentValue)
if(currentValue && currentValue != "randomHex()"){

// Set default on option
options.default = currentValue;

else
}else
// Check if input has default
if(inputEl.hasAttribute("default")){

// Get default
var currentDefault = inputEl.getAttribute("default");


// Check current value
if(currentDefault === "randomHex()"){
Expand Down
12 changes: 6 additions & 6 deletions src/Library/Database/Driver/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,18 +868,18 @@ public function deleteOneToCollection(string $table, int $id, string $database =
# Set result
$result = null;

# Check input
if(!$table || empty($value) || !$database)

# Return result
return $result;

# Check database
if(!$database)

# Get main database
$database = $this->_getDefaultDatabase();

# Check input
if(!$table || !$database)

# Return result
return $result;

# Use database
$this->client->exec("USE " . $database);

Expand Down
32 changes: 31 additions & 1 deletion src/Library/Form/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class Process {
private array $dispatch = [
"INT" => [
],
"SELECT" => [
"integer",
],
"VARCHAR" => [
"trim",
"clean",
Expand Down Expand Up @@ -134,11 +137,25 @@ public function __construct(array $formResult = []){
private function _actionInt(array &$input = []):void {

# Check value is same type
if(!is_int($input['value']) && !ctype_digit($input['value']))
if(!is_int($input['value']) && !ctype_digit($input['value'])){

# Check requierd
if(
(
isset($input['required']) &&
$input['required'] == false
) ||
!isset($input['required'])
)

# Set value to null
$input['value'] = null;

# Stop function
return;

}

# Parse the value
$input['value'] = intval($input['value']);

Expand Down Expand Up @@ -814,6 +831,19 @@ public static function strtolower(string $input):string {

}

/**
* Integer
*
* @param string
* @return int
*/
public static function integer(string $input):int {

# Return result
return intval($input);

}

/**
* Alphanumeric
*
Expand Down
18 changes: 17 additions & 1 deletion src/Library/Form/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,21 @@ public function __construct(array $formResult = []){
private function _actionInt(array &$input = []):void {

# Check value is same type
if(!is_int($input['value']) && !ctype_digit($input['value']))
if(
(
$input['value'] !== null && !is_int($input['value']) && !ctype_digit($input['value'])
) && (
(
isset($input['required']) &&
$input['required'] == false &&
$input['value'] !== null
) ||
(
!isset($input['required']) &&
$input['value'] !== null
)
)
){

# New Exception
throw new CrazyException(
Expand All @@ -157,6 +171,8 @@ private function _actionInt(array &$input = []):void {
]
);

}

}

/**
Expand Down

0 comments on commit 85bc537

Please sign in to comment.