Friday, March 27, 2015

Dynamic Web Application - Part 3

The last post of Dynamic Web Application will show the implementation of the ORB within the index page. There are two things to take into account : Actions and Forms
The actions will do the CRUD operations based on your Request via the Criteria knowing that they have been setup with your displayers.


 switch ($action) {  
  case "delete" :  
   if($security->isAdmin())  
    $message = $helper->doDelete($security);   
   $sql_row = $helper->redirectTo($helper->getWebhome());  
   break;  
  case "update" :        
   $message = $helper->doUpdate($security);  
   $sql_row = $helper->getPageFromId();  
   break;  
  case "insert" :  
   $message = $helper->doInsert($security);  
   $sql_row = $helper->redirectTo($_REQUEST['bin_document_name']);        
   break;  
  case "redirect" :  
   $sql_row = $helper->redirectTo($_REQUEST['redirect']);  
   break;  
  default :  
   $sql_row = $helper->getPageFromRequest();        
 }  
 if ($action != "edit")     
    $action = "view";  

meanwhile your displayers can switch in various mode


 switch ($action) {  
   case "edit" :  
    echo $fields['content']->displayEdit ($table, $sql_row, $inline_model_class,"bin");  
    echo "\n  <div class=\"comment\">\n" . $fields['content']->getWikiHelp() . "  </div>\n";  
    break;  
   default :  
    echo $fields['content']->displayView ($table, $sql_row, $inline_model_class,"bin");  

Saturday, March 21, 2015

Dynamic Web Application - Part 2

After a brief introduction to the nature of a dynamic software most commonly named "Backend" based on the ORB, we will unleash the power of the backend with the Criteria. When you need information, you will access the database with the Criteria in the following way

 function getPageFromRequest() {  
   $criteria = new Criteria();  
   $sql_connect = $this->inline_model_class->connect();   
   $criteria->fromRequestWithPrefix($this->inline_model_class,"bin");  
   $criteria->setAllColumns('document',$this->inline_model_class);  
   if ($criteria->isRequest()) {  
    $sql_result = mysql_query($this->table->doSearch($criteria,$this->inline_model_class),   
                    $sql_connect);  
    $sql_row = mysql_fetch_assoc($sql_result);  
    return $sql_row;  
   } else  
    return $false;  
  }  

Obviously, Request contains expected fields previously setup with your displayers which produces a cycle between your Request and the previous Request. In another word, this is how you program the synchronization of your system. The dynamic system is the opposite to a stateless RESTful API. For your information, the function fromRequest can configure the whole SQL Request.
  /**  
   *  function set Criteria with Http Request values.  
   *  
   *  @param object      dbmodel  
   *  @param string      prefix  
   **/   
  function fromRequestWithPrefix($model_class,$prefix) {  
   if ($prefix != "")  
    $prefix .= "_";  
   // GET Search Criteria  
   $tables_array = $model_class->getTables();  
   foreach ($tables_array as $table_name => $table_class) {  
    $fields_array = $table_class->getFields();  
    foreach ($fields_array as $field_name => $field_class) {  
      $field_string = $table_class->getPrettyName() . "_" . $field_class->getPrettyName();   
      $prefix_field_string = $prefix . $field_string;  
      if ($_GET[$prefix_field_string]) {  
       $this->add ($field_string,$_GET[$prefix_field_string]);  
      $this->isRequest = true;  
     }  
      if ($_POST[$prefix_field_string]) {  
       $this->add ($field_string,$_POST[$prefix_field_string]);  
      $this->isRequest = true;   
     }  
      $field_types = $field_class->getTypes ();  
      foreach ($field_types as $type) {  
       if ($_GET[$prefix_field_string . "_" . $type]) {  
        $this->add ($field_string . "_" . $type,$_GET[$prefix_field_string . "_" . $type]);  
       $this->isRequest = true;  
      }  
       if ($_POST[$prefix_field_string . "_" . $type]) {  
        $this->add ($field_string . "_" . $type,$_POST[$prefix_field_string . "_" . $type]);  
       $this->isRequest = true;  
      }  
      }  
    }  
   }  
   // GET COLUMNS FROM REQUEST   
   if (isset($_GET[$prefix . 'search_columns']))  
    $this->setColumns ($_GET[$prefix . 'search_columns']);  
   if (isset($_POST[$prefix . 'search_columns']))  
    $this->setColumns ($_POST[$prefix . 'search_columns']);  
   if (isset($_GET[$prefix . 'search_columns_multiple']) || isset($_POST[$prefix . 'search_columns_multiple'])) {  
    if (isset($_GET[$prefix . 'search_columns_multiple']))  
      $str_search_columns = $_GET[$prefix . 'search_columns_multiple'];  
    if (isset($_POST[$prefix . 'search_columns_multiple']))  
      $str_search_columns = $_POST[$prefix . 'search_columns_multiple'];  
    $search_column_array = preg_split("/;/",$str_search_columns);  
    $count = 0;  
    foreach ($search_column_array as $search_column) {  
      $search_columns_multiple[$count] = $search_column;  
      $count++;  
    }  
    $this->setColumns ($search_columns_multiple);  
   }  
   //GET SEARCH FIELD : Use for default displayer  
   if (isset($_GET[$prefix . 'search_fields']) || isset($_POST[$prefix . 'search_fields'])) {  
    if (isset($_GET[$prefix . 'search_fields']))  
      $str_search_fields = $_GET[$prefix . 'search_fields'];  
    if (isset($_POST[$prefix . 'search_fields']))  
      $str_search_fields = $_POST[$prefix . 'search_fields'];  
    $search_field_array = preg_split("/;/",$str_search_fields);  
    $count = 0;  
    foreach ($search_field_array as $search_field) {  
      $search_fields[$count] = $search_field;  
      $count++;  
    }  
    $this->setSearchFields ($search_fields);  
   }  
   // GET JOIN FIELDS CONFIGURATION : Use for default displayer  
   $count = 0;  
   foreach ($tables_array as $table_name => $table_class) {  
    $join_fields_array = $table_class->getJoinFields();  
    if ($join_fields_array) {  
      foreach ($join_fields_array as $join_field) {  
       if (isset ($_GET[$prefix . $join_field['name'] . "_join"]) || isset ($_POST[$prefix . $join_field['name'] . "_join"])) {  
        if (($_GET[$prefix . $join_field['name'] . "_join"] != 0)|| ($_POST[$prefix . $join_field['name'] . "_join"] != 0)) {  
         $join_fields[$count] = $table_name . "_" . $join_field['name'];  
         $count++;  
        }  
       }  
      }  
    }  
   }  
   $this->join_fields = $join_fields;  
   //GET START ROW  
   if (isset($_GET[$prefix . 'start_row']))  
    $this->setStartRow ($_GET[$prefix . 'start_row']);  
   if (isset($_POST[$prefix . 'start_row']))  
    $this->setStartRow ($_POST[$prefix . 'start_row']);  
   //GET NB ROWS  
   if (isset($_GET[$prefix . 'nb_rows']))  
    $this->setNbRows ($_GET[$prefix . 'nb_rows']);  
   if (isset($_POST[$prefix . 'nb_rows']))  
    $this->setNbRows ($_POST[$prefix . 'nb_rows']);  
   //Get Orders  
   if (isset($_GET[$prefix . 'order']))  
    $this->setOrder ($_GET[$prefix . 'order']);  
   if (isset($_POST[$prefix . 'order']))  
    $this->setOrder ($_POST[$prefix .'order']);  
  }