PHP: Difference between revisions

From nuBuilderForte
Jump to navigation Jump to search
Line 566: Line 566:
Returns the IP Address.
Returns the IP Address.
|example=<!-- EXAMPLE -->
|example=<!-- EXAMPLE -->
See [https://forums.nubuilder.com/viewtopic.php?f=19&t=10834#p23180 Forum Post]
|alsosee=<!-- SEE ALSO -->
|alsosee=<!-- SEE ALSO -->



Revision as of 00:57, 27 September 2021


Each of the purple diamonds represents PHP code that can be run in nuBuilder Forte and when.


Click to view larger


eg. You might add this in AS - After Save of an invoice Browse and Edit Form.

  $s = "UPDATE stock SET sto_units = sto_units - '$units' WHERE stock_id = '$stock_id';"
  
  nuRunQuery($s);
  • All of the AS BS BB and BE code blocks are for PHP only and are executed on the server-side.
  • You can create and call a JavaScript routine ( nuJavascriptCallback($string1) )
  • All Javascript to run on the client side should be done in the Custom Code of the object (most likely for the onclick that you're trying to do).
  • Each field or label on the form has a Custom Code (click the OBJ button to see all the objects on your form) and so does the form itself (click the Prop button).

db_fetch_array

$object = db_fetch_array($object1)

Parameters

$object1  : nuRunQuery() result.

Return Value

$object   : PDO object. 

Description

A function for looping through the result of a SELECT query returned from nuRunQuery.

Example


$s = "SELECT cus_lastname FROM customer WHERE cus_city = ?";
$t = nuRunQuery($s, array('New York'));

while($r = db_fetch_array($t)){

    nuDebug($r['cus_lastname']. ' lives in New York.');

}

nuDebug()

[0]: Robert Paulson lives in New York.


Also See : db_field_names, db_fetch_row, db_fetch_object, db_num_rows, nuRunQuery


db_fetch_object

$object = db_fetch_object($object1)

Parameters

$object1 : nuRunQuery() result.

Return Value

$object  : PDO object

Description

A function for looping through the result of a SELECT query returned from nuRunQuery.

Example


$s = "SELECT in_death_name FROM member WHERE nickname = 'bob'";
$t = nuRunQuery($s);

while($r = db_fetch_object($t)){

    nuDebug('His name is ' . $r->in_death_name);

}

nuDebug()

[0]: His name is Robert Paulson


Also See : db_field_names, db_fetch_array, db_fetch_row, db_num_rows, nuRunQuery


db_fetch_row

$object = db_fetch_row($object1)

Parameters

$object1  : nuRunQuery() result.

Return Value

$object   : PDO object. 

Description

A function for looping through the result of a SELECT query returned from nuRunQuery.

Example


$s = "SELECT in_death_name FROM member WHERE nickname = 'bob'";
$t = nuRunQuery($s);

while($r = db_fetch_row($t)){

    nuDebug('His name is ' . $r[0]);

}

nuDebug()

[0]: His name is Robert Paulson


Also See : db_field_names, db_fetch_array, db_fetch_object, db_num_rows, nuRunQuery


db_field_names

$array = db_field_names($string1)

Parameters

$string1 : Database table name.

Return Value

$array   : Field names.

Description

Returns an array of field names from a database table.

Example

$a = db_field_names('customer');
nuDebug($a);

nuDebug()

[0]: Array
(
    [0] => customer_id
    [1] => cus_name
    [2] => cus_phone
    [3] => cus_address
)


Also See : db_fetch_array, db_fetch_object, db_fetch_row, db_num_rows


db_num_columns

$integer = db_num_columns($object1)

Parameters

$object1  : PDO object. 

Return Value

$integer  : Number of columns returned.

Description

The number of fields in the result from nuRunQuery.

Example


	$s = "SELECT * FROM customer";
	$t = nuRunQuery($s);
	
	nuDebug(db_num_columns($t));

nuDebug()

[0]: 6


Also See : db_field_names, db_fetch_array, db_fetch_object, db_fetch_row, nuRunQuery, db_num_rows


db_num_rows

$integer = db_num_rows($object1)

Parameters

$object1  : PDO object. 

Return Value

$integer  : Number of records returned.

Description

The number of records in the result from nuRunQuery.

Example


	$s = "SELECT * FROM customer";
	$t = nuRunQuery($s);
	
	nuDebug(db_num_rows($t));

nuDebug()

[0]: 3453


Also See : db_field_names, db_fetch_array, db_fetch_object, db_fetch_row, nuRunQuery, db_num_columns


nuAddJavascript

nuAddJavascript($string1)

Parameters

$string1 : Javascript code.

Return Value


Description

Adds JavaScript to the beginning of a Form's JavaScript before it is run.

Example


$j = "console.log('Hello from nuBuilder!');";

nuAddJavascript($j);

nuDebug()



Also See : Before Browse, Before Edit


nuDebug

nuDebug($anytype1, $anytype2..)

Parameters

$anytypes : string, object or array.

Return Value


Description

This function takes multiple parameters and creates a record that can be viewed by clicking nuDebug Results in the Options menu.


Each nuDebug record's first line will explain when or why it was created. There are 3 types...

  1. Procedure - 2017-11-03 06:52:02 - Procedure ABC line 1
  2. Event - 2017-11-03 06:58:08 - Before Edit of Form nuhome line 5
  3. SQL Error - 2017-11-03 06:58:09 - SQL Error in nuRunQuery

Example


$s = 'Hello world';
$a = [1,2,3,4];
$o = ['name' => 'Bob', 'phone' => '555 123456'];
nuDebug($s, $a, $o);

nuAddJavascript($j);

nuDebug()

[0] : Hello world

[1] : Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)


[2] : Array
(
    [name] => Bob
    [phone] => 555 123456
)



Also See : Before Browse, Before Edit, Before Save,After Save, Before Delete, After Delete


nuDisplayError

nuDisplayError($string1)

Parameters

$string1 : error message.

Return Value


Description

This function adds an error message that will stop the Edit Form from saving or deleting the current record but will then display all generated error messages.


If any error messages are created in Before Save or Before Delete the current record will not be saved or deleted. eg.

  • No messages - Continue to Save or Delete record.
  • One or more messages - No not Save or Delete record.


If any messages are created in After Save or After Delete the current record will be saved or deleted and will display this message once saved.


(The green Yes or red No elements in the flowchart below.)


Click to view larger

Example

nuDisplayError('That did not work!');

nuDebug()



Also See : Before Save, Before Delete, nuMessage



nuGlobalAccess

$boolean = nuGlobalAccess()

Parameters


Return Value

$boolean  :  true or false

Description

Returns true if a user with global access is logged in, otherwise false

Example

if(!nuGlobalAccess()){
    nuDisplayError('Access denied!');
}



Also See :



nuSendEmail

$object = nuSendEmail($to, $from, $fromname, $content, $subject, $filelist, $html = false, $cc = "", $bcc = "", $reply_to_addresses = array())

Parameters


$to : The email address you want the message to go to.

$from : The email address you want to appear in the email header. 
If this parameter is left empty, the email address from Setup -> Email Settings is used.

$fromname : The name you want to appear in the email header. 
If this parameter is left empty, the email address from Setup -> Email Settings is used.

$content : The content of the message, if the parameter $html is set true, then you can include html content

$subject : The email subject you want to appear in the email header

$filelist : If you do not want to send attachments, you must still supply an empty array here. 
If you do want to send attachments, this array needs in the following format $filename=>$filesource

$html : true or false

$cc : Carbon copy: When you CC people on an email, the CC list is visible to all other recipients.

$bcc: Blind Carbon Copy: Other recipients will not see the Bcc recipient's email address in the mail.

$reply_to_addresses: It is the email address that the reply message is sent when you want the reply to go to 
an email address that is different than the From: address.

Return Value

$object: If an error has occurred the function returns false, as well as the error message.
The function will return true and a confirmation message of "Message sent successfully" will be returned.

Description

Sends an email using the settings stored in Setup -> Email Settings

Example

$r = nuSendEmail('to@test.com','from@test.com','From','Content','Subject', [], true, 'cc_email@test.com', 'bcc_email@test.com')

nuDebug()

(
[0] => 1
[1] => Message sent successfully
[2] =>
[3] =>
)


Also See :


nuHash

$array = nuHash()

Parameters


Return Value

$array  : list of Hash Cookies.

Description

Returns a list of Hash Cookies available to use in PHP.

Example

nuDebug(nuHash());

nuDebug()

[0] : Array
(
    [USER_ID] => globeadmin
    [USER_GROUP_ID] => 
    [HOME_ID] => 
    [GLOBAL_ACCESS] => 1
    [invoice_id] => s14919516899490
    [inv_company_id] => 
    [inv_number] => 53
    [inv_total] => 215.3
    [inv_date] => 2017-03-19 00:00:00
    [record_id] => s14919516899490
    [title] => Invoice
    [call_type] => getlookupid
    [iframe] => 0
    [lookup_id] => 
    [object_id] => s14967319482165
    [page_number] => 0
    [password] => 
    [rows] => 20
    [row_height] => 25
    [session_id] => s15001161980910
    [sort] => -1
    [sort_direction] => desc
    [subforms] => 0
    [username] => 
    [user_id] => globeadmin
    [58a08a1abc4782c] => s14919516899490
    [FORM_ID] => 58a08a1abc4782c
    [PREVIOUS_RECORD_ID] => s14919516899490
    [RECORD_ID] => s14919516899490
    [form_id] => 58a08a1abc4782c
    [redirect_form_id] => 58a08a1abc4782c
    [browse_sql] => 
    [browse_rows] => 
    [pages] => 0
    [prefix] => 
    [primary_key] => 57295aa5660f48b
    [inv_gst] => 0
    [inv_gst2] => 0
    [test] => 
    [SUBFORM_ID] => s14967319482165
    [ID] => 57295aa5660f48b
    [CODE] => 
    [nuFORMdata] => 
    [TABLE_ID] => ___nu15969f4ed5b744___
    [SESSION_ID] => s15001161980910
    [data] => 
    [LOOKUP_RECORD_ID] => 57295aa5660f48b
)



Also See : nuDebug()


nuHasNewRecordID

$boolean = nuHasNewRecordID()

Parameters


Return Value

$boolean  :  true or false

Description

Matches #PREVIOUS_RECORD_ID# with #RECORD_ID# and returns true or false.

So that the user can tell if a record is being cloned.

Example

if(nuHasNewRecordID()){
    nuUpdateCounter($r->zzzzsys_object_id);
}



Also See :


nuHasNoRecordID

$boolean = nuHasNoRecordID()

Parameters


Return Value

$boolean  : true or false

Description

Returns true if #RECORD_ID# = '-1' so that the user can tell if this is a new reecord.

Example

if(nuHasNoRecordID()){
    nuUpdateCounter($r->zzzzsys_object_id);
}



Also See :


nuGetFormProperties

$object = nuGetFormProperties($string1)

Parameters

$string1 = Primary Key

Return Value

$object  : Form record.

Description

Returns a Form record as an object.

Example

nuDebug(nuGetFormProperties('nuuser'));
[0] : stdClass Object
(
    [zzzzsys_form_id] => nuuser
    [sfo_type] => browseedit
    [sfo_code] => nuuser
    [sfo_description] => User
    [sfo_table] => zzzzsys_user
    [sfo_primary_key] => zzzzsys_user_id
    [sfo_browse_redirect_form_id] => 
    [sfo_browse_row_height] => 0
    [sfo_browse_rows_per_page] => 15
    [sfo_browse_sql] => SELECT * 
FROM zzzzsys_user 
LEFT JOIN zzzzsys_access ON zzzzsys_access_id = sus_zzzzsys_access_id
ORDER BY sus_name
    [sfo_javascript] => 
)


Also See :


nuGetIPAddress

$myIP = nuGetIPAddress();

Parameters


Return Value


Description

Returns the IP Address.

Example

See Forum Post

{{{result}}}


Also See :


nuGetNuDataValue

$object = nuGetNuDataValue($nudata, $formId, $field)

Parameters

$nudata = array of form fields
$formId = Form's ID
$field = the field in the form

Return Value

$value  : Form field value.

Description

Returns a Form's field value.

Example

{{{result}}}


Also See :


nuGlobalAccess

$isGlobeadmin = nuGlobalAccess()

Parameters


Return Value

$value  : Form field value.

Description

Returns a Form's field value.

Example

See Forum Post

{{{result}}}


Also See :


nuSetNuDataValue

$bool = nuSetNuDataValue(&$nudata, $formId, $field, $value)

Parameters

$nudata = array of form fields
$formId = Form's ID
$field = the field in the form
$value = value to be set in the field of the form

Return Value

Boolean success or failure of the setting of the form field value

Description

Returns the success or failure of the field value set operation

Example

$nuMainForm   = nuHash()['nuFORMdata'][0]->id;

// Get the value of the object "not_title"
$title = nuGetNuDataValue($nudata, $nuMainForm, 'not_title');

// Replace the value of the object "not_title" by adding a -test postfix:
nuSetNuDataValue($nudata, $nuMainForm, 'not_title', $title . "-test");


Also See :


nuID

$string = nuID()

Parameters


Return Value

$string  : random string. 

Description

Creates a string from

  • 1502690897383 - Date.now().
  • 1012 - An incrementing number looping between 1000 and 9999.

Example

nuDebug(nuID());
[0] : 14998774211012


Also See : nuTT, nuID Before Save,After Save


nuJavascriptCallback

nuJavascriptCallback($string1)

Parameters

$string  : JavaScript Code

Return Value


Description

Runs after nuRunPHPHidden() has returned from the server.

Example


$js = " nuMessage(['<h1>It worked!</h1>']);";
nuJavascriptCallback($js);



Also See : nuRunPHPHidden()


nuLookupRecord

$object = nuLookupRecord()

Parameters


Return Value

$object  : every field from the selected (Browse Form's) record.

Description

Returns all fields from a selected Lookup record.

Example

nuDebug(nuLookupRecord()->ID);   //-- the id of the chosen Lookup record.
nuDebug(nuLookupRecord());       //-- all fields from then chosen Lookup's record - as well as ID.

nuDebug()

[0] : 5a67739f464cd96

[1] : stdClass Object
(
    [company_id] => 58ade54dac2128b
    [com_code] => 777
    [com_name] => 7 Up
    [com_business_id] => 568a00c77f8f709
    [com_company_id] => 
    [com_color_id] => 568ef5a6968322b#nuSep#568ef920846a089
    [com_age] => 3
    [com_notes] => 
    [com_test] => 
)


Also See : After Browse, nuSetFormValue


nuProcedure

$string = nuProcedure($string1)

Parameters

$string1 : Procedure Code

Return Value

$string  : PHP Code with all Hash Cookies replaced

Description

Returns PHP code from a Procedure with all Hash Cookies replaced, ready to eval().

Example

$p = nuProcedure('HW');
eval($p);

nuDebug()



Also See : Before Browse, After Browse, Before Edit, Before Save, After Save, Before Delete, After Delete



nuReturnNewRecord

nuReturnNewRecord($string1 = -1)

Parameters

$string1  : record_id  (if left blank this will return a blank record.)

Return Value


Description

When placed in the After Save Event, this sets the Edit Form to the record passed by $string1, ready for editing.

Example

nuReturnNewRecord();
   or
nuReturnNewRecord('5a72a0fce65fea2');

nuDebug()



Also See : After Save


nuRunQuery

This function is to be used to access the database. If it is passed a SQL statement it will prepare the statement and then execute it. The results of the query can be fetched using the db_fetch_ family of functions below.

If the SQL statement is not defined or no parameters are passed to the function, then an array containing information about the database is used such as:

  • The database host
  • The name of the database
  • The username for the database.
  • The password for the user to access the database.
Array (

  [0] => 127.0.0.1
  [1] => site_c
  [2] => root
  [3] => root123

)

The statement template can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed. The variables array must have the values to be used for each (:name) or ? in the query.


$object = nuRunQuery([$sql = $string1] [, $variables = array()] [,$isInsert = false]);

Parameters

$string1 : The SQL query to run.
$variables : An array of values that will replace ?s.
$isInsert : Set to true for insert queries.

Return Value

If $isInsert is set to true and the primary key is set to auto-increment, it returns the id of the last inserted entry. 
If $isInsert is set to true and no auto-increment primary key is used, it returns 0 if the insert was successful, otherwise -1 
(in case of a duplicate entry, invalid syntax etc.)
Else, returns the results of the SQL query as a $object  : PDO object.

Description

Runs an SQL query and returns an Object (for SELECT Statements).

This Object can be used by db_fetch_object, db_fetch_array, db_fetch_row, db_num_rows

Example


// 1. Select statement
$s  = "SELECT cus_address FROM customer WHERE customer_id = ?";
$t  = nuRunQuery($s, array('#LOOKUP_RECORD_ID#'));

if (db_num_rows($t) == 1) {
    $r  = db_fetch_object($t);
    nuSetFormValue('inv_address', $r->cus_address);
}

// 2. Insert statement
$insertSql = "
    INSERT INTO `message` 
      (`subject`, `body`, `date_sent`)
    VALUES
      (:subject, :body, :date_sent)
";


$data = array(
    "subject" => 'nuBuilder!',
    "body" => 'Testing insert statement',
    "date_sent" => date("Y-m-d H:i:s")
);

$r = nuRunQuery($insertSql, $data, true);
if ($r == 0) {
   nuDebug('Insert sucessful!');
}

nuDebug()



Also See : db_fetch_object, db_fetch_array, db_fetch_row, db_num_rows


nuSetFormValue

nuSetFormValue($string1, $string2);

Parameters

$string1 = id of nuBuilder Object on current Edit Form.
$string2 = value used to update $string1's Object.

Return Value


Description

Updates an Object on an Edit Form After Browse after a Lookup value is chosen.

Example


$lu = nuLookupRecord();

nuSetFormValue('inv_address1', $lu->cus_address1);
nuSetFormValue('inv_address2', $lu->cus_address2);
nuSetFormValue('inv_address3', $lu->cus_address3);

nuDebug()



Also See : After Browse, nuLookupRecord


nuSubformObject

$object = nuSubformObject($string1)

Parameters

$string1  : Subform id  **Passing an empty string will return the main Edit Form as a Subform object.

Return Value

$object   : subform properties

Description

Returns a PHP object that can be looped through.

All Subform values in this object will have any formatting removed. eg $ 1,234.50 will be 1234.5

Example

nuDebug(nuSubformObject('invoice_item_sf'));
[0] : stdClass Object
(
    [id] => invoice_item
    [foreign_key] => ite_invoice_id
    [primary_key] => invoice_item_id
    [object_id] => 58a09180222faae
    [table] => invoice_item
    [action] => save
    [rows] => Array
        (
            [0] => Array
                (
                    [0] => s14941671441215
                    [1] => 6
                    [2] => thing1
                    [3] => 3
                    [4] => 18
                    [5] => 0
                )

            [1] => Array
                (
                    [0] => s14998769369058
                    [1] => 4
                    [2] => thing2
                    [3] => 2
                    [4] => 8
                    [5] => 0
                )

            [2] => Array
                (
                    [0] => -1
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 1
                )

        )

    [edited] => Array
        (
            [0] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [1] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

            [2] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                )

        )

    [deleted] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 1
        )

    [fields] => Array
        (
            [0] => ID
            [1] => ite_units
            [2] => ite_thing
            [3] => ite_unit_price
            [4] => ite_total
            [5] => nuDelete
        )

)


Also See : nuSubformObject


nuTT

$string = nuTT()

Parameters


Return Value

$string  : name for temp table.

Description

Creates a unique ID starting with __nu.

Example

$tmp = nuTT();

$s   = "CREATE TABLE $tmp SELECT * FROM customer";

nuRunQuery($tmp);

nuDebug()



Also See : nuID


nuUser

$object = nuUser()

Parameters


Return Value

$object  : object containing User details

Description

Returns all the information from the zzzzsys_user table for the current, logged in User.

Example

nuDebug(nuUser());

nuDebug()

[0] : stdClass Object
(
    [zzzzsys_user_id] => 5a62ebae2a6994b
    [sus_zzzzsys_access_id] => 5a62eb4e09ffd46
    [sus_language] => 
    [sus_name] => Robert
    [sus_email] => bob@fclub.com
    [sus_login_name] => bob
    [sus_login_password] => c81e728d9d4c2f636f067f89cc14862c
)



Also See :