Account specifig PHP coding

Template

How to extends functionalities using PHP coding

How to create account specific classes or extends eKansio classes.

General

You need to have PHP privileges to make PHP code. If so, Table and Action has PHP script editing fields for scripts that are run in specific situation.

Account specific Classes

You can define account -specific classes in directory: ./[account]/mad/lib/Account[ClassName].php
Class name must be same as filename.
Name must start with 'Account'.
Classes are autoincluded when called for the first time.

Example class: ./[account]/mad/lib/AccountEmployee.php.

class AccountPayment {
    public function getEmployeeSalary($Employee){
        return $Employee->salary_total->value();
    }
}

Use example:

$salary = $this->get('AccountPayment')->getEmployeeSalary($Employee);

Extending Page class

You can Extend Page creating a class in in directory: ./[account]/mad/lib/AccountPage[FolderName].php

You can extend normal Page class functionalities. Creating a Employee::getWorkHours() -method, which counts employees work hours in given range.

Example class: ./[account]/mad/lib/AccountPageEmployee.php.

class AccountPageEmployee EXTENDS Sivu {

    /**
      * Calculate work hours
      * @param string $dateQuery Query clause for date field.
      * @return float
      */
    public function getWorkHours($dateQuery='annettu'){
       return $this->get('pages','workhour')
          ->addWhere('employee', $this->nr())
          ->addWhere('workday',$dateQuery)
          ->calculate('hours');
    }

    /**
      * Calculate and set total salary for employee.
      */
    public function updateTotalSalary(){
       $hourRate = $this->hour_rate->getNumber();
       $this->total_salary->calculateValue("[workhour__employee:hours * $hourRate]");

       // Return always $this if method does not return any specific value.
       // Allows method to be chained.
       return $this; 
    }

}

Use examples

Get Hours for this month:

$hours = $this->get('page', 'employee', $employeeId)->getWorkHours('This month');

Update total salary:

$this->get('page','employee',$employeeId)
   ->updateTotalSalary()
   ->saveChangesToServer();

Methods to extend

List of methods that are meant to be extended:

Method Return type Description
onSaveStart() This method is run when saving starts
onSave() This method is run just before values are saved to Server.
onSaveReady() This method is run just after values are saved to Server.
onDelete() This method is run before delete.
onDeleteReady() This method is run after delete.
onUndelete() This method is run before undelete
onUndeleteReady() This method is run after undelete
onRemove() This method is run before remove.
onRemoveReady() This method is run after remove.
onChangeStart() This method is run when any change starts
onChange() This method is run when any change is executed (save, delete, undelete, remove).
onChangeReady() This method is run after any change is executed (save, delete, undelete, remove).
isLocked() bool Return TRUE, if page is locked from ALL editing.

if onSave() or onSaveStart() return false, page saving is stopped.

Custom methods naming conventions

Method Return type Description
isSomething() bool Check either something exists or not.
updateFieldName() $this Update values to page. Do not execute save() -command!
saveSomething() $this Do stuff and execute save -command.
renderSomething() string Return string or HTML formatted content.