Helpers

Helpers are classes which you can use that don't fall under the category of "controllers". These will usually be classes that provide extra functionality that you can use in your controllers.

PIP ships with a number of helper classes. Feel free to use, extend or ignore them as you please. You can pre-load the helpers you need in the config file (see set-up) and need to include use helpers\name at the top of the PHP file.

Url

The Url class provides some simple redirection and link classes:

  use helpers\url;
    
  // Redirect to Url
  Url::redirect('album/show/23');
    
  // return string url to action
  Url::action('album/show/23');
    
  // return string url to asset 
  Url::asset('images/photo.png');
    
  // return string url to view
  // Url::view('album/index');
  
  // return base url
  // Url::base_url();
  

Session

The Session class allows $_SESSION variables and Flash messages to be stored and retreived:

  use helpers\session;
    
  // Set session variable
  Session::set('key', $value);

  // Test session variable (returns True if set) 
  $result = Session::has('key');
  
  // Get session variable
  $value = Session::get('key');
  
  // Unset session variable
  Session::clear('key');

  // Flash messages
  // Set Flash Message
  Session::setFlash('key', $value);

  // Test Flash Message (returns True if set) 
  $result = Session::isFlash('key');
  
  // Get Flash Message (and clear message)
  $value = Session::getFlash('key');
  
  // Destroy Session
  $value = Session::destroy();
  

Auth

The Auth class validates a password against a database file (called Members as shipped) and provides functions for checking for 'member' Logged on, and retreiving the member's id, name and level.

An access level of 0=Member, 1=Author and 2=Admin can be used to restrict access to actions or to determine what information to display. Feel free to change the user filename and level descriptions - or replace with your favourite authentication class.

  use helpers\auth;

  // Attempt login
  if( attempt($email, $pass) {
      // log in OK (User now Logged on)
  }
  
  // Check logged in, and (optional) Access Level
  // returns True - if logged in and User Level >= $level
  // returns False - if not logged in or not required level
  //                 and set Session['requested_url']
  if (Auth::check(Auth::ADMIN)) {
    // logged in as Admin
  } else {
    // redirect to login
  }
  
  // get user Id
  $id = Auth::getUserId();

  // get user name
  $id = Auth::getUserName();

  // get user level
  $id = Auth::getUserLevel();
  
  // Log Out
  Auth::LogOut();
  
  // Get password hash
  $hash = Auth::hash('password');
      

If you need to prevent access to an action - and redirect the user to the login page - you can save the requested Url, and then use it again after the successful login with setUrl(), hasUrl() and getUrl():

  // Before 
  // must be logged in as Admin
  if (!Auth::check(Auth::ADMIN)) {
      Auth::setUrl();
      Url::redirect('member/login');
  }

  // after successful login
  // If user was diverted to login page
  if (Auth::hasUrl()) {
      Url::redirect(Auth::getUrl());
  }
  

Validation

The Validation class provides a simple validation check using a set of rules and an array of data. The Model class has functions for validating and checking for errors.

The check() function returns an array of error messages for each field. Note, multiple errors for one field are concatinated together. Feel free to add additional rules, or change the message array to suit your own requirements.

  use helpers\validation;
        
  // Validate
  // $fields - array of fieldnames => values
  // $rules  - array of fieldnames => rules
  // returns - array of fieldnames => error messages
  $errors = Validate::check( array $fields, array $ruleset );
    

Date

The Date class provides some convenient ways to display dates:

  use helpers\date;

  //  asDate - returns date in format: 3rd June 2011
  echo Date::asDate($date);
    
  //  asLongDate - returns date in format: Friday, 3rd June 2011 at 4:35 pm
  echo Date::asLongDate($date);

  //  asDateTime - returns date in datetime format:  yyyy-mm-dd hh:mm:ss
  //            - returns current date/time if empty asDateTime()
  echo Date::asDatetime($date);

  //  asTime - returns time as: hh:mm:ss
  echo Date::to_time($date)
  
  //  since - returns friendly elepsed time:  xx days/weeks/etc ago
  echo Date::to_time($date)