4

How to create a custom module without using existing module designers or other same stuff. For example, I want to create a custom module that will save the name and last name of a certain person. Actually, I researched on the internet, and I didn't find a clear tutorial on how to create a custom module specifically in vtiger 6.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Hope
  • 644
  • 2
  • 6
  • 21

3 Answers3

6

First create a new folder(module name without space) in modules and copy files from vtlib/ModuleDir/6.0.0 file to the folder created in modules/newmodule

Change the name of ModuleName.php with your module name(with no space) and one more ModuleName.php file in languages\en_us.

open the modulename.php to change the class name, $table_name , $table_index & also available in languages\en_us with your modulename.

Create new file with any name under your root directory. Insert the code below to add the fields and also module.

<?php
include_once 'vtlib/Vtiger/Module.php';

$Vtiger_Utils_Log = true;

$MODULENAME = 'Persons';

$moduleInstance = Vtiger_Module::getInstance($MODULENAME);
if ($moduleInstance || file_exists('modules/'.$MODULENAME)) {
    echo "Module already present - choose a different name.";
} else {
    $moduleInstance = new Vtiger_Module();
    $moduleInstance->name = $MODULENAME;
    $moduleInstance->parent= 'Tools';
    $moduleInstance->save();

    // Schema Setup
    $moduleInstance->initTables();

    // Field Setup
    $block = new Vtiger_Block();
    $block->label = 'LBL_'. strtoupper($moduleInstance->name) . '_INFORMATION';
    $moduleInstance->addBlock($block);

    $blockcf = new Vtiger_Block();
    $blockcf->label = 'LBL_CUSTOM_INFORMATION';
    $moduleInstance->addBlock($blockcf);

    $field1  = new Vtiger_Field();
    $field1->name = 'lastname';
    $field1->label= 'Last Name';
    $field1->uitype= 2;
    $field1->column = $field1->name;
    $field1->columntype = 'VARCHAR(255)';
    $field1->typeofdata = 'V~M';
    $block->addField($field1);

    $moduleInstance->setEntityIdentifier($field1);

    $field2  = new Vtiger_Field();
    $field2->name = 'firstname';
    $field2->label= 'First Name';
    $field2->uitype= 1;
    $field2->column = $field2->name;
    $field2->columntype = 'VARCHAR(255)';
    $field2->typeofdata = 'V~M';
    $block->addField($field2);

    // Recommended common fields every Entity module should have (linked to core table) 
    $mfield1 = new Vtiger_Field();
    $mfield1->name = 'assigned_user_id';
    $mfield1->label = 'Assigned To';
    $mfield1->table = 'vtiger_crmentity';
    $mfield1->column = 'smownerid';
    $mfield1->uitype = 53;
    $mfield1->typeofdata = 'V~M';
    $block->addField($mfield1);

    $mfield2 = new Vtiger_Field();
    $mfield2->name = 'CreatedTime';
    $mfield2->label= 'Created Time';
    $mfield2->table = 'vtiger_crmentity';
    $mfield2->column = 'createdtime';
    $mfield2->uitype = 70;
    $mfield2->typeofdata = 'T~O';
    $mfield2->displaytype= 2;
    $block->addField($mfield2);

    $mfield3 = new Vtiger_Field();
    $mfield3->name = 'ModifiedTime';
    $mfield3->label= 'Modified Time';
    $mfield3->table = 'vtiger_crmentity';
    $mfield3->column = 'modifiedtime';
    $mfield3->uitype = 70;
    $mfield3->typeofdata = 'T~O';
    $mfield3->displaytype= 2;
    $block->addField($mfield3);

    // Filter Setup
    $filter1 = new Vtiger_Filter();
    $filter1->name = 'All';
    $filter1->isdefault = true;
    $moduleInstance->addFilter($filter1);
    $filter1->addField($field1)->addField($field2, 1)->addField($field3, 2)->addField($mfield1, 3);

    // Sharing Access Setup
    $moduleInstance->setDefaultSharing();

    // Webservice Setup
    $moduleInstance->initWebservice();

    mkdir('modules/'.$MODULENAME);
    echo "OK\n";
}
?>
Shah
  • 505
  • 1
  • 5
  • 12
  • Thank you so much I just want to clarify something. In ModuleName.php there are lots variable like $list_fields do I have to change it ? and what will I do after I've done all of this. Is there any script to run in order to make it work ?Thanks for the time. – Hope Jun 23 '14 at 03:46
  • Dont change any variable name. (Replace only with your modulename). You put this shared code with any filename(like abc.php) in your vtiger6 root directory and run this file from browser eg: http://vtigercrmurl/abc.php – Shah Jun 24 '14 at 17:56
  • This is what I did. 1. I create Persons folder inside the module coz it is the name of my module and inside in it are the things i've copy in 6.0.0,then i edit those things you've said. I also edit , and other things similar to it. 2. I put the code you've given in root directory and I've run it as what you've said.But the warning show."Module already present - choose a different name." . what i did is i run the code first before i put the persons folder inside the modules. No error occur but when I try to view person module, error "Permission denied by name". – Hope Jun 25 '14 at 01:44
  • looks like some thing went wrong. the module you are tried to create, it has been created already. try it on a new vtiger6 instance. if you are on live server, provide full permission to files. – Shah Jun 30 '14 at 06:01
5

There's a simple Console tool for creating new custom modules:

open the cmd prompt, go to the vtlib/tools folder and execute the Console.php file:

>cd C:\{$YourPathToVtiger}\apache\htdocs\vtigerCRM\vtlib\tools
>php -f Console.php

Type '1' for Creating a new module and set "Module name" and "Entity field" (the Entity field is the field that will be shown in bold at the top of the window, when you open an instance of that module).

Now the new module is created and installed, and you can easily customize it (adding new fields, workflows and whatever) from the vTiger GUI.

T30
  • 11,422
  • 7
  • 53
  • 57
4

Create the bootstrap vtlib script in root folder of Vtiger to activate the module entry with some specific file name.

create_expense.php

<?php
include_once 'vtlib/Vtiger/Module.php';

$Vtiger_Utils_Log = true;

$MODULENAME = 'Expenses';

$moduleInstance = Vtiger_Module::getInstance($MODULENAME);
if ($moduleInstance || file_exists('modules/'.$MODULENAME)) {
    echo "Module already present - choose a different name.";
} else {
    $moduleInstance = new Vtiger_Module();
    $moduleInstance->name = $MODULENAME;
    $moduleInstance->parent= 'Tools';
    $moduleInstance->save();

    // Schema Setup
    $moduleInstance->initTables();

    // Field Setup
    $block = new Vtiger_Block();
    $block->label = 'LBL_'. strtoupper($moduleInstance->name) . '_INFORMATION';
    $moduleInstance->addBlock($block);

    $blockcf = new Vtiger_Block();
    $blockcf->label = 'LBL_CUSTOM_INFORMATION';
    $moduleInstance->addBlock($blockcf);

    $field1  = new Vtiger_Field();
    $field1->name = 'summary';
    $field1->label= 'Summary';
    $field1->uitype= 2;
    $field1->column = $field1->name;
    $field1->columntype = 'VARCHAR(255)';
    $field1->typeofdata = 'V~M';
    $block->addField($field1);

    $moduleInstance->setEntityIdentifier($field1);

    $field2  = new Vtiger_Field();
    $field2->name = 'expenseon';
    $field2->label= 'Expense On';
    $field2->uitype= 5;
    $field2->column = $field2->name;
    $field2->columntype = 'Date';
    $field2->typeofdata = 'D~O';
    $block->addField($field2);

    $field3  = new Vtiger_Field();
    $field3->name = 'expenseamount';
    $field3->label= 'Amount';
    $field3->uitype= 71;
    $field3->column = $field3->name;
    $field3->columntype = 'VARCHAR(255)';
    $field3->typeofdata = 'V~M';
    $block->addField($field3);

    $field3  = new Vtiger_Field();
    $field3->name = 'description';
    $field3->label= 'Description';
    $field3->uitype= 19;
    $field3->column = 'description';
    $field3->table = 'vtiger_crmentity';
    $blockcf->addField($field3);

    // Recommended common fields every Entity module should have (linked to core table)
    $mfield1 = new Vtiger_Field();
    $mfield1->name = 'assigned_user_id';
    $mfield1->label = 'Assigned To';
    $mfield1->table = 'vtiger_crmentity';
    $mfield1->column = 'smownerid';
    $mfield1->uitype = 53;
    $mfield1->typeofdata = 'V~M';
    $block->addField($mfield1);

    $mfield2 = new Vtiger_Field();
    $mfield2->name = 'CreatedTime';
    $mfield2->label= 'Created Time';
    $mfield2->table = 'vtiger_crmentity';
    $mfield2->column = 'createdtime';
    $mfield2->uitype = 70;
    $mfield2->typeofdata = 'T~O';
    $mfield2->displaytype= 2;
    $block->addField($mfield2);

    $mfield3 = new Vtiger_Field();
    $mfield3->name = 'ModifiedTime';
    $mfield3->label= 'Modified Time';
    $mfield3->table = 'vtiger_crmentity';
    $mfield3->column = 'modifiedtime';
    $mfield3->uitype = 70;
    $mfield3->typeofdata = 'T~O';
    $mfield3->displaytype= 2;
    $block->addField($mfield3);

    // Filter Setup
    $filter1 = new Vtiger_Filter();
    $filter1->name = 'All';
    $filter1->isdefault = true;
    $moduleInstance->addFilter($filter1);
    $filter1->addField($field1)->addField($field2, 1)->addField($field3, 2)->addField($mfield1, 3);

    // Sharing Access Setup
    $moduleInstance->setDefaultSharing();

    // Webservice Setup
    $moduleInstance->initWebservice();

    mkdir('modules/'.$MODULENAME);
    echo "OK\n";
}

Now create new folder as per below folder structure.

Directory Structure

vtigercrm/
    modules/
        Expenses/
            Expenses.php - class Expenses
    languages/
        en_us/
            Expenses.php

Next create a Expenses.php class file

<?php

include_once 'modules/Vtiger/CRMEntity.php';

class Expenses extends Vtiger_CRMEntity {
    var $table_name = 'vtiger_expenses';
    var $table_index= 'expensesid';

    var $customFieldTable = Array('vtiger_expensescf', 'expensesid');

    var $tab_name = Array('vtiger_crmentity', 'vtiger_expenses', 'vtiger_expensescf');

    var $tab_name_index = Array(
            'vtiger_crmentity' => 'crmid',
            'vtiger_expenses' => 'expensesid',
            'vtiger_expensescf'=>'expensesid');

    var $list_fields = Array (
            /* Format: Field Label => Array(tablename, columnname) */
            // tablename should not have prefix 'vtiger_'
            'Summary' => Array('expenses', 'summary'),
            'Assigned To' => Array('crmentity','smownerid')
    );
    var $list_fields_name = Array (
            /* Format: Field Label => fieldname */
            'Summary' => 'summary',
            'Assigned To' => 'assigned_user_id',
    );

    // Make the field link to detail view
    var $list_link_field = 'summary';

    // For Popup listview and UI type support
    var $search_fields = Array(
            /* Format: Field Label => Array(tablename, columnname) */
            // tablename should not have prefix 'vtiger_'
            'Summary' => Array('expenses', 'summary'),
            'Assigned To' => Array('vtiger_crmentity','assigned_user_id'),
    );
    var $search_fields_name = Array (
            /* Format: Field Label => fieldname */
            'Summary' => 'summary',
            'Assigned To' => 'assigned_user_id',
    );

    // For Popup window record selection
    var $popup_fields = Array ('summary');

    // For Alphabetical search
    var $def_basicsearch_col = 'summary';

    // Column value to use on detail view record text display
    var $def_detailview_recname = 'summary';

    // Used when enabling/disabling the mandatory fields for the module.
    // Refers to vtiger_field.fieldname values.
    var $mandatory_fields = Array('summary','assigned_user_id');

    var $default_order_by = 'summary';
    var $default_sort_order='ASC';
}
Milan Malani
  • 1,818
  • 1
  • 22
  • 34