PHP: Native Methods (Beta)
Common variables and dictionaries
Before exploring the native methods, it's important to note a crucial aspect of creating low-code triggers: the use of currentData, beforeSave and afterSave.
These three dictionaries contain valuable information about the data sent by the user and about the database itself.
- $currentData: this is the dictionary that contains the information the user has sent by performing an action. For example, if a user drags a card in a kanban to the "In Progress" stage, then currentData['stage'] will contain "In Progress".
- $beforeSave: this is the dictionary that contains the information about the record before it is changed (that is, before currentData is saved to the database). For example, if a user drags a card in a kanban from "New" to the "In Progress" stage, then currentData['stage'] will contain "In Progress", and beforeSave["stage"] will contain "New". Bonus: if you change the beforeData in a synchronous, before create/update trigger, this new information will be saved to the record.
- $afterSave: this is the dictionary that contains the information about the record after it is changed (that is, after currentData is saved to the database). For example, if a user drags a card in a kanban from "New" to the "In Progress" stage, then currentData['stage'] will contain "In Progress", as will afterSave["stage"]. Bonus: some information will only be available in afterSave. For example, in an After a record is created trigger, the record's id will only be contained in afterSave, as the id is only registered after the record is saved to the database.
- $currentUser: this is the dictionary that contains the information about user who has triggered the automation. Particularly useful for identification, as it contains id_user, email, and name.
There are also two other dictionaries that are only present when using low-code Functions instead of Triggers:
- $recordData: this holds the information of the record when the Function is invoked through a Button + Run a function combination. In this scenario, it should also be noted that N:1 connection fields will bring the first level of nested information. For example, if you have a "client" field that points to a record in the "Clients" table, you can access information such as recordData['client']['name'] without needing to run a separate search.
- $params: this holds the information you receive when activating the Function through a webhook. For example, if you send the JSON payload {"status":"New"} to the webhook, you can access that information through params['status'] in the Function.
Native Methods
This is a list of native methods and how to use them in low-code triggers.
Create Record
To create a record use the table method using the table name as a parameter, then the insert method using a Dictionary as a parameter. Example:
$obj = [
"name" => "new record",
"inicio" => new Datetime("now")
];
$results = $jestor->table("table_name")->insert($obj);
Edit Record
To edit a record use the table method using the table name as a parameter, then the update method using the id of the record that will be changed and a Dictionary as a parameter. Example:
$recordId = 1
$jestor->table("table_name")->update(1, ["name" => "John Doe"]);
Delete Record
To delete a record use the table method using the table name as a parameter, then the delete method using the record id as a parameter. Example:
$recordId = 1
$jestor->table("teste")->delete($recordId);
List Records
To list records use the table method using the table name as a parameter, then the get method. Example:
$filter = new Filter("name", "novo record", "==");
$filters = [
$filter->to_array()
];
$results = $jestor->table("table_name")->get($filters);
The Filter object receives the following parameters:
- field: str (field name).
- value: str (field value).
- operator: str (operators can be found in the Operators class).
- Equal : "=="
- Different : "!="
- Like : "like"
- Starts With : "startsWith"
- Not Like :"notLike"
- List: "in"
- Not in list: "not_in"
- Null : "isnull"
- Not Null: "is_not_null"
- Is null or Empty: "nullOrEmpty"
- Not Null or Empty: "filled"
- Greather than: ">"
- Greather or equal than: ">="
- Less Than: "<"
- Less or equal than: "<="
- Between : "between"
The get method receives the following parameters:
- filters:
- Type: List of filters
- Description: Filter records following the filters settings.
- limit:
- Type: int
- Description: Set how many records will return.
- page:
- Type: int
- Description: Set which page will return the records
- sort:
- Type: string
- Description: Choose the field will sort you results. Ex: "name ASC"
- fields_to_select
- Type: string
- Description: Choose which columns to return. Ex: "name,age,email"
- fetch_type
- Type: string
- Description: Choose if will return only the record data or the connect columns also.
- Options: single, full
- operator
- Type: string
- Description: Choose if your filters use AND or OR combination
- Options: AND, OR
Create User
To create a user use the user() method, then the createUser() method passing a list as a parameter. This list must contain the fields in order [email, password, profile_id, name]. Example:
$results = $jestor->user()->createUser("[email protected]","Test123", 18001, "John Doe", false, "member");
Inactive User
To inactive a user use the user() method, then the InactiveUser()_ method passing the user id. Example:
$results = $jestor->user()->inactiveUser($idUser);
List Users
To list users use the user() method, then the get method. The get method accepts the following parameters:
- filters: List[Filter] = None,
- limit: int = 100,
- page: int = 1,
- sort: str = None
Example:
$filter = new Filter("email", "[email protected]", "==");
$filters = [
$filter->to_array()
];
$results = $jestor->user()->get($filters);
Files
To manipulate files use the File object. It takes the following parameters:
- table: str
- recordId: int = None
- field: str = None
$tableName = 'table_name';
$recordId = 1;
$field = 'file_field';
$files = $jestor->file($tableName, $recordId, $field);
Adding, removing and editing files in an existing registry:
$tableName = 'table_name';
$recordId = 1;
$field = 'file_field';
$files = $jestor->file($tableName, $recordId, $field);
$_file = [
"name" => "teste",
"content" => "teste 123",
"contentType" => "txt"
];
$files->add($_file);
$idFile = '65525377a0201';
$results = $files->update($idFile, ["name" => "nome alterado"]);
$idFile = '65525377a02221';
$files->delete($idFile);
$results = $jestor->table($tableName)->update(1, [$field => $files->toJson()]);
stopCodeExecution()
During synchronous triggers, you can stop the code execution, rollback all changes and show the user a message with stopCodeExecution().
if ($currentData["stage"] == "Done" && $currentData["amount"] == 0){
stopCodeExecution("You can't close deals with no $ amount!");
}
showMessage()
During synchronous triggers or functions, you can show a message to the user after execution.
Parameters:
- message: str Required
- type: string = None
- success
- warning
showMessage("Record added successfully", 'warning');
Generate PDF
This method allows you to create a custom PDF from a string containing custom HTML, saving it locally to Jestor. The example below is a trigger creates a PDF file and updates a record, adding the file to a files field.
$tableName = 'table';
$files = $jestor->file($tableName);
$html = '<body>';
$html .='<div class="container">';
$html .=' <div class="card" id="makepdf">';
$html .=' <h2>Welcome to Jestor</h2>';
$html .=' <ul>';
$html .=' <li><h4>We are going to generate a pdf from the area inside the box</h4></li>';
$html .=' <li><h4>This is an example of generating pdf from HTML during runtime</h4></li>';
$html .=' </ul>';
$html .=' </div>';
$html .=' </div>';
$html .='</body>';
$pdf = $jestor->generatePDF([$html, "A4", "portrait"]);
$files.add({'name': 'file_test', 'localFile': $pdf});
$result = $jestor->table(tableName)->update($afterSave['id_table'],['attachments'=> $files->toJson()]);
Create New File
This function uses two arguments to create a file from some sort of content:
A variable containing the content.
The extension of the file to be created.
The example below will create a .txt file that contains the text "test file content" and put it on an attachment field.
$arg = [
"test file content",
"txt"
];
$results = $jestor->createNewFile( $arg);
Create New File Base64
This function uses two arguments to create a file from some sort of content:
A variable containing the content base in 64 format.
The extension of the file to be created.
The example below will create a .txt file that contains the text "test file content" and put it on an attachment field.
$tableName = "table";
$recordId = "id_table";
$newFile = $jestor->createNewFileFromBase64(["dGVzdCBmaWxlIGNvbnRlbnQ=","txt"]);
$jestor->table($tableName).update($recordData[recordId],[ "files" => $newFile]);
Get Content File
This function gets the content from a file inside Jestor itself. The example below will download the .jpg file from the picture field we passed as an argument and return in base64 format.
$base64Content = $jestor->getContentFile([$currentData["pictures"]])
Download File
This function downloads a file from a given URL. The example below would attempt to download the .jpg file from the fake URL we passed as an argument and return in base64 format.
$base64Content = $jestor->downloadFile(["https://example.com/file.jpg"]);
$newFile = $jestor->createNewFileFromBase64([$base64Content,"jpg"]);
$tableName = "table";
$recordId = "id_table";
$jestor->table(tableName)->update($recordData[recordId],[ "files" => $newFile]);
Updated 7 months ago