ED php framework :: Tips
Tips, Tricks and Best practices
Here you'll find many tips and tricks how to deal with particular problems and scenarios. In the tutorial you'll find valuable advices how to use many help classes, methods and functions.
 
There is a javascript function called "elem", which serves as a short alias for "document.getElementById"
function OnPageLoad(){
    var domObject1 = document.getElementById("elemID");
    var domObject2 = elem("elemID"); // do the same thing
}
 
On the javascript side, the "setValue" and "getValue" methods of any page component can be invoked.
function btnSubmit_OnClick(){
    var val = page.startDate.getValue();
    page.endDate.setValue(val);
}
 
There are a couple PHP encrypt/decrypt global functions. If no password is provided, then the default framework password will be used. It is stored in "system/constants.inc" under the name "SYS_INTERNAL_ENCRYPT_PASSWORD". It is advisable to change it for every application you create. The default encryption method is XXTEA. The encrypted string is also "base64" encoded.
public function btnSubmit_OnClick(){
    $str = encryptString("My sensitive text");
    $str = encryptString("My sensitive text", "pass1");
    $str = encryptArray(Array());
    $str = encryptArray(Array(), "pass1");
    ...
    $str2 = decryptString($str);
    $str2 = decryptString($str, "pass1");
    $arr = decryptArray($str);
    $arr = decryptArray($str, "pass1");
}
 
Taking the above sample in mind, this time we want directly to use the security classes.
public function btnSubmit_OnClick(){
    $secretKey = "pass1";

    $security = TSecurityFactory::createInstance("XXTEA", $secretKey); // or
    $security = TSecurityFactory::createInstance("MCRYPT", $secretKey);

    $str = $security->encrypt("My sensitive text");
    ...
    $str2 = $security->decrypt($str);
}
 
Sometimes you may want to do something based on the browser type.
// js
function btnSubmit_OnClick(){
    if (page.agent.isIE()) {
        // code for IE
    }
    else if (page.agent.isFirefox()) {
        // code for Firefox
    }
    else {
        // handle the rest
    }
}

// php
public function btnSubmit_OnClick(){
    if ($this->agent->isIE()) {
        // code for IE
    }
    else if ($this->agent->isFirefox()) {
        // code for Firefox
    }
    else {
        // handle the rest
    }
}
 
You redirect your site visitor to http://www.mysite.com/login.php using $this->redirect("/login.php"); but then you want to make sure he is redirected to the "https://" version and not the "http://" one. Use this code on the server side:
public function OnPageLoad() {
    if (!$this->isPostBack() && !request()->isHTTPS()) {
        return $this->loadHTTPS();
    }
}
 
After successful login your site visitor is back to let's say "index.php". Now you want to show him different content depending whether he is logged-in or just a guest. We have 2 choices, the 1st one is less secure and all content is placed in the file and the 2nd option is more secure and content is loaded depending on state:

// index.php
<div id="idLoggedUser" style="display:none">content for logged user</div>
<div id="idGuest" style="display:none">content for guest</div>
<div id="idContent"><?php echo $page->model->getProperty("content") ?></div>

// javascript function
public function manageContent(loggedState) {
    if (loggedState) {
        elem("idLoggedUser").style.display = "";
    }
    else {
        elem("idGuest").style.display = "";
    }
}

// index.php.script
public function OnPageLoad() {
    // option 1
    $this->agent->call("manageContent", $this->user->isLogged());

    // option 2
    if ($this->user->isLogged()) {
        $this->model->setProperty("content", TContent::load("logged.html")->toString());
    }
    else {
        $this->model->setProperty("content", TContent::load("guest.html")->toString());
    }
}

logged.html and guest.html can be placed either in the same directory, where index.php is located or under "protected/content".

 
We have seen in the above snippet the method "call" of the PHP class THttpAgent. It can receive as arguments "js function name" + up to 3 function parameters.

// javascript function
public function jsFunction(married, name, weight) {
    // married = true, name = "John Myers" and weight = 190
}

// php
public function OnPageLoad() {
    $this->agent->call("jsFunction", true, "John Myers", 190);
}

 
There is a PHP global function called "randomString", which produces random strings with variable length consisting only of A-Z, a-z, 0-9 characters.

// php
public function OnPageLoad() {
    $str1 = randomString(5); // f9uR7
    $str1 = randomString(7); // sS1r5Ty
}

 
There is a PHP global function called "filesizeToString", which converts bytes length in human readable text.

// php
public function OnPageLoad() {
    $str1 = filesizeToString(8*1024*1024 + 20*1024); // 8mb 20kb
}

 
If you have installed the extension "GD", which provides functions for manipulating images then you can create a thumbnail in 1 line:
    // php
    public function btnCreateThumbnail() {
        $binaryThumbImage = TImage::createThumbnail($binaryOriginalImage, 32, 32);
    }
On image upload you can check the dimension like this:
    // php
    public function btnSave() {
        $dim = TImage::getDimensionFromBinaryData($file->getBinaryData());
        if ($dim['width'] > 700 || $dim['height'] > 500) {
            $this->showError("Image is too large");
            return;
        }
    }