Difference between revisions of "Vtigercrm update date field default behavior"

From Studiosg
Jump to navigationJump to search
(Created page with 'Welcome to Simone Giustetti's wiki pages. Languages: '''English''' - [http://www.giustetti.net/wiki/index.php?title=vtigercrm_modificare_il_comportamento_dei_campi_data Italian…')
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Welcome to Simone Giustetti's wiki pages.
+
{{header_en|title=Customizing VtigerCRM date fields| keyword={{Template:keyword_en_vtigercrm}}| description=A guide explaining how to customize the standard behavior of VtigerCRM date fields | link_page=vtigercrm_modificare_il_comportamento_dei_campi_data}}
  
 +
== '''datefield''' Standard Behavior ==
  
Languages: '''English''' - [http://www.giustetti.net/wiki/index.php?title=vtigercrm_modificare_il_comportamento_dei_campi_data Italiano]
+
Dates are managed through a '''dedicated calendar control''' in VtigerCRM. The control '''automatically suggests the current date''' while inserting new data or updating old ones. The suggestion is usually useful, but could prove annoying in specific contexts: forms containing a lot of date fields for example. Were the form filled with many date fields the user would be pressed to enter each one to correct or remove the unwanted suggestion. A similar scenario requires '''to update the calendar control standard behavior into suggesting a null value'''. The current date should be suggested only upon control explicit selection. Let's see how to update the standard date field behavior in '''release 5.2.1 of VtigerCRM'''.
  
----
 
  
== '''datefield''' standard behavior ==
+
== VtigerCRM Internals ==
Dates are managed through a '''dedicated calendar control''' in VtigerCRM. The control '''automatically suggests the current date''' while inserting new data or updating old ones. The suggestion is usually useful, but could prove annoying in specific contexts: forms containing a lot of date fields for example. Were the form filled with many date fields the user would be pressed to enter each one to correct or remove the unwanted suggestion. A similar scenario requires '''to update the calendar control standard behavior into suggesting a null value'''. The current date should be suggested only upon control explicit selection. Let's see how to update the standard date field behavior in '''release 5.2.1 of VtigerCRM'''.
 
  
 
== VtigerCRM internals ==
 
 
Data processing code is split apart from presentation one in VtigerCRM. Information are retrieved from database, configuration files and other sources, processed then passed to sections of code dedicated to HTML page formatting. '''All web pages provided by the software are based on template models'''. VtigerCRM uses template engine '''Smarty''' to manage templates. All templates consist of text files with a syntax reminiscent of '''HTML''' with the addition of variables and control structures. All template files are saved in directory '''Smarty/templates''' a sub-directory of the VtigerCRM installation tree. The directory contains:
 
Data processing code is split apart from presentation one in VtigerCRM. Information are retrieved from database, configuration files and other sources, processed then passed to sections of code dedicated to HTML page formatting. '''All web pages provided by the software are based on template models'''. VtigerCRM uses template engine '''Smarty''' to manage templates. All templates consist of text files with a syntax reminiscent of '''HTML''' with the addition of variables and control structures. All template files are saved in directory '''Smarty/templates''' a sub-directory of the VtigerCRM installation tree. The directory contains:
   bash-4.1# ls -la ./Smarty
+
   bash-4.1# '''ls''' ''-la'' ./Smarty
 
   total 64
 
   total 64
 
   drwxr-xr-x  8 apache apache  4096 Nov 15  2010 .
 
   drwxr-xr-x  8 apache apache  4096 Nov 15  2010 .
Line 42: Line 39:
 
The function source code will be updated to change calendar controls standard behavior throughout the software modules. The purpose of the update is to ensure that date fields are initialized with a null value instead of the current date. The original source code and the updated one are listed below.
 
The function source code will be updated to change calendar controls standard behavior throughout the software modules. The purpose of the update is to ensure that date fields are initialized with a null value instead of the current date. The original source code and the updated one are listed below.
  
=== Original source code ===
+
=== Original Source Code ===
 +
<syntaxhighlight lang="php">
 
   /** This function returns the date in user specified format.
 
   /** This function returns the date in user specified format.
 
   * Takes no param, receives the date format from current user object
 
   * Takes no param, receives the date format from current user object
Line 76: Line 74:
 
       return $display_date;
 
       return $display_date;
 
   }
 
   }
 +
</syntaxhighlight>
  
=== Updated source code ===
+
=== Updated Source Code ===
 +
<syntaxhighlight lang="php">
 
   /** This function returns the date in user specified format.
 
   /** This function returns the date in user specified format.
 
   * Takes no param, receives the date format from current user object
 
   * Takes no param, receives the date format from current user object
Line 91: Line 91:
 
       return $display_date;
 
       return $display_date;
 
   }
 
   }
 +
</syntaxhighlight>
  
=== Restrict the update to some modules only ===
+
=== Restrict the Update to Some Modules Only ===
 
The updated code will affect all of VtigerCRM modules. Some could prefer a more confined approach: update the behavior of the calendar controls '''for some modules only''', for example. Suppose the update should affect two modules only: standard module "Project" and custom one "Policy". The updated source code is available below:
 
The updated code will affect all of VtigerCRM modules. Some could prefer a more confined approach: update the behavior of the calendar controls '''for some modules only''', for example. Suppose the update should affect two modules only: standard module "Project" and custom one "Policy". The updated source code is available below:
 +
<syntaxhighlight lang="php">
 
   /** This function returns the date in user specified format.
 
   /** This function returns the date in user specified format.
 
   * Takes no param, receives the date format from current user object
 
   * Takes no param, receives the date format from current user object
Line 129: Line 131:
 
       return $display_date;
 
       return $display_date;
 
   }
 
   }
 
+
</syntaxhighlight>
 
Line:
 
Line:
 +
<syntaxhighlight lang="php">
 
       $module_name = $_REQUEST['module'];
 
       $module_name = $_REQUEST['module'];
 +
</syntaxhighlight>
 
saves the active module name into a local variable for later filtering. Control structure "case" filters modules based on their name and executes specific code for each entry. Lines
 
saves the active module name into a local variable for later filtering. Control structure "case" filters modules based on their name and executes specific code for each entry. Lines
 +
<syntaxhighlight lang="php">
 
       case "Policy":
 
       case "Policy":
 
         $display_date = '';
 
         $display_date = '';
Line 139: Line 144:
 
         $display_date = '';
 
         $display_date = '';
 
       break;
 
       break;
 +
</syntaxhighlight>
 
initialize modules "Policy" and "Project" date fields with a null value during data inserts or updates.
 
initialize modules "Policy" and "Project" date fields with a null value during data inserts or updates.
 
The last code lines
 
The last code lines
 +
<syntaxhighlight lang="php">
 
       default:
 
       default:
 
         global $current_user;
 
         global $current_user;
Line 155: Line 162:
 
         }
 
         }
 
       }
 
       }
 +
</syntaxhighlight>
 
set calendar control standard behavior for each other module in VtigerCRM.
 
set calendar control standard behavior for each other module in VtigerCRM.
  
Line 163: Line 171:
  
  
----
+
{{footer_en | link_page=vtigercrm_modificare_il_comportamento_dei_campi_data}}
 
 
Languages: '''English''' - [http://www.giustetti.net/wiki/index.php?title=vtigercrm_modificare_il_comportamento_dei_campi_data Italiano]
 

Latest revision as of 13:55, 21 March 2022

Welcome to Simone Giustetti's wiki pages.


Languages: English - Italiano


datefield Standard Behavior

Dates are managed through a dedicated calendar control in VtigerCRM. The control automatically suggests the current date while inserting new data or updating old ones. The suggestion is usually useful, but could prove annoying in specific contexts: forms containing a lot of date fields for example. Were the form filled with many date fields the user would be pressed to enter each one to correct or remove the unwanted suggestion. A similar scenario requires to update the calendar control standard behavior into suggesting a null value. The current date should be suggested only upon control explicit selection. Let's see how to update the standard date field behavior in release 5.2.1 of VtigerCRM.


VtigerCRM Internals

Data processing code is split apart from presentation one in VtigerCRM. Information are retrieved from database, configuration files and other sources, processed then passed to sections of code dedicated to HTML page formatting. All web pages provided by the software are based on template models. VtigerCRM uses template engine Smarty to manage templates. All templates consist of text files with a syntax reminiscent of HTML with the addition of variables and control structures. All template files are saved in directory Smarty/templates a sub-directory of the VtigerCRM installation tree. The directory contains:

  bash-4.1# ls -la ./Smarty
  total 64
  drwxr-xr-x  8 apache apache  4096 Nov 15  2010 .
  drwxr-xr-x 28 apache apache  4096 Sep  7 01:32 ..
  -rw-r--r--  1 apache apache 24389 Nov 15  2010 COPYING.lib
  drwxrwxr-x  2 apache apache  4096 May  5  2011 cache
  drwxr-xr-x  2 apache apache  4096 Nov 15  2010 configs
  drwxr-xr-x  4 apache apache  4096 Nov 15  2010 libs
  drwxr-xr-x  2 apache apache  4096 Nov 15  2010 misc
  drwxr-xr-x  8 apache apache  4096 Nov 15  2010 templates
  drwxrwxr-x  2 apache apache 12288 Sep 16 00:25 templates_c

Directory templates contains source code files for standard controls, forms and web pages.
Directory templates_c contains pre-compiled template documents ready to be used by the software engine.

File Smarty/templates/EditViewUI.tpl contains templates used by the software forms while inserting or updating data.

File Smarty/templates/DetailView.tpl contains templates for standard controls. Templates are ordered by module.

File include/utils/utils.php contains function get_textdateField. The function sets default formatting / value for date fields.

File include/utils/CommonUtils.php contains function getNewDisplayDate(). The function assigns the current date value to date fields. The function is intended for variable initialization and as such responsible for date field behavior during insert and update operations.

Function getNewDisplayDate() is called in some other source files:

  • include/utils/EditViewUtils.php
  • include/utils/utils.php
  • modules/Emails/Save.php
  • modules/com_vtiger_workflow/VTSimpleTemplate.inc

The function source code will be updated to change calendar controls standard behavior throughout the software modules. The purpose of the update is to ensure that date fields are initialized with a null value instead of the current date. The original source code and the updated one are listed below.

Original Source Code

   /** This function returns the date in user specified format.
   * Takes no param, receives the date format from current user object
   */
   
   function getNewDisplayDate()
   {
      global $log;
      $log->debug("Entering getNewDisplayDate() method ...");
         $log->info("in getNewDisplayDate ");
   
      global $current_user;
      $dat_fmt = $current_user->date_format;
      if($dat_fmt == '')
         {
                  $dat_fmt = 'dd-mm-yyyy';
         }
      $display_date='';
      if($dat_fmt == 'dd-mm-yyyy')
      {
         $display_date = date('d-m-Y');
      }
      elseif($dat_fmt == 'mm-dd-yyyy')
      {
         $display_date = date('m-d-Y');
      }
      elseif($dat_fmt == 'yyyy-mm-dd')
      {
         $display_date = date('Y-m-d');
      }
         
      $log->debug("Exiting getNewDisplayDate method ...");
      return $display_date;
   }

Updated Source Code

   /** This function returns the date in user specified format.
   * Takes no param, receives the date format from current user object
   */
   
   function getNewDisplayDate()  {
      global $log;
      $log->debug("Entering getNewDisplayDate() method ...");
      $log->info("in getNewDisplayDate ");
   
      $display_date='';
      $log->debug("Exiting getNewDisplayDate method ...");
      return $display_date;
   }

Restrict the Update to Some Modules Only

The updated code will affect all of VtigerCRM modules. Some could prefer a more confined approach: update the behavior of the calendar controls for some modules only, for example. Suppose the update should affect two modules only: standard module "Project" and custom one "Policy". The updated source code is available below:

   /** This function returns the date in user specified format.
   * Takes no param, receives the date format from current user object
   */
   
   function getNewDisplayDate()  {
      global $log;
      $log->debug("Entering getNewDisplayDate() method ...");
      $log->info("in getNewDisplayDate ");
   
      $display_date='';
      $module_name = $_REQUEST['module'];
      switch($module_name)  {
      case "Policy":
         $display_date = '';
      break;
      case "Project":
         $display_date = '';
      break;
      default:
         global $current_user;
         $dat_fmt = $current_user->date_format;
         if($dat_fmt == '')  {
            $dat_fmt = 'dd-mm-yyyy';
         }
         if($dat_fmt == 'dd-mm-yyyy')  {
            $display_date = date('d-m-Y');
         }  elseif($dat_fmt == 'mm-dd-yyyy')  {
            $display_date = date('m-d-Y');
         }  elseif($dat_fmt == 'yyyy-mm-dd')  {
            $display_date = date('Y-m-d');
         }
      }
      $log->debug("Exiting getNewDisplayDate method ...");
      return $display_date;
   }

Line:

       $module_name = $_REQUEST['module'];

saves the active module name into a local variable for later filtering. Control structure "case" filters modules based on their name and executes specific code for each entry. Lines

      case "Policy":
         $display_date = '';
      break;
      case "Project":
         $display_date = '';
      break;

initialize modules "Policy" and "Project" date fields with a null value during data inserts or updates. The last code lines

      default:
         global $current_user;
         $dat_fmt = $current_user->date_format;
         if($dat_fmt == '')  {
            $dat_fmt = 'dd-mm-yyyy';
         }
         if($dat_fmt == 'dd-mm-yyyy')  {
            $display_date = date('d-m-Y');
         }  elseif($dat_fmt == 'mm-dd-yyyy')  {
            $display_date = date('m-d-Y');
         }  elseif($dat_fmt == 'yyyy-mm-dd')  {
            $display_date = date('Y-m-d');
         }
      }

set calendar control standard behavior for each other module in VtigerCRM.

The above code update is a rather simple one, but can be further modified to execute checks for the suggested values, force a specific behavior for each module or execute some data processing. It is a starting point for further customizations.


For any feedback, questions, errors and such, please e-mail me at studiosg [at] giustetti [dot] net



Languages: English - Italiano