phpvtiger

Creating custom module in Vtiger CRM from scratch with table


I am new to Vtiger CRM and I have searched a lot to find how to create a custom module in Vtiger CRM with a table associated with it from scratch. I am not able to follow the documentation provided by Vtiger.


Solution

  • Refer this url for Creating New module and fields.

    https://wiki.vtiger.com/index.php/CreatingEntityModule

    Or if you having any problem then follow as per instructions.

    first of all do this.

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

    Change name of ModuleFile.js, ModuleFile.php, ModuleFileAjax.php with your module name(with no space).

    Keep in mind, During changing name of ModuleFileAjax.php just Replace ModuleFile with name of Module.

    Go to modulename.php to change the class name, $table_name (6values change), $table_index (4values change).

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

    <?php 
    
    // Turn on debugging level
    $Vtiger_Utils_Log = true;
    include_once('vtlib/Vtiger/Menu.php');
    include_once('vtlib/Vtiger/Module.php');
    
    $module = new Vtiger_Module();
    $module->name = 'Store';//(No space in module name)
    $module->save();
    
    $module->initTables();
    $module->initWebservice();
    
    $menu = Vtiger_Menu::getInstance('Support');
    $menu->addModule($module);
    
    $block1 = new Vtiger_Block();
    $block1->label = 'Organization Information';
    $module->addBlock($block1); //to create a new block
    
    $field0 = new Vtiger_Field();
    $field0->name = 'organization_name';
    $field0->label = 'Organization Name';
    $field0->table = $module->basetable; 
    $field0->column = 'organization_name';
    $field0->columntype = 'VARCHAR(100)';
    $field0->uitype = 2;
    $field0->typeofdata = 'V~M';
    $module->setEntityIdentifier($field0); //to insert values in entity folder
    $block1->addField($field0); //to add field in block
    
    
    $field1 = new Vtiger_Field();
    $field1->name = 'store_id_auto';
    $field1->label = 'Store ID';
    $field1->table = $module->basetable; 
    $field1->column = 'store_id_auto';
    $field1->columntype = 'VARCHAR(100)';
    $field1->uitype = 4;
    $field1->typeofdata = 'V~O';
    $block1->addField($field1);
    
    //Do not change any value for filed2.
    $field2 = new Vtiger_Field();
    $field2->name = 'assigned_user_id';
    $field2->label = 'Assigned To';
    $field2->table = 'vtiger_crmentity'; 
    $field2->column = 'smownerid';
    $field2->columntype = 'int(19)';
    $field2->uitype = 53;
    $field2->typeofdata = 'V~M';
    $block1->addField($field2);
    
    $filter1 = new Vtiger_Filter();
    $filter1->name = 'All';
    $filter1->isdefault = true;
    $module->addFilter($filter1);
    // Add fields to the filter created
    $filter1->addField($field0, 1);
    $filter1->addField($field1, 2);
    $filter1->addField($field2, 3);
    
    
    /** Set sharing access of this module */
    $module->setDefaultSharing('Private'); 
    /** Enable and Disable available tools */
    $module->enableTools(Array('Import', 'Export'));
    $module->disableTools('Merge');
    
    
    
    ?>
    

    Updates: Supported Scripts for Vtiger 7 & Vtiger 8

    Creating a custom module in Vtiger 7/Vtiger 8 CRM from scratch with a table

    <?php
    // Turn on debugging level
    $Vtiger_Utils_Log = true;
    include_once 'vtlib/Vtiger/Module.php';
    include_once 'vtlib/Vtiger/Package.php';
    include_once('vtlib/Vtiger/Menu.php');
    
    $MODULENAME     = 'Store';
    $moduleInstance = Vtiger_Module::getInstance($MODULENAME);
    
    if ($moduleInstance && file_exists('modules/' . $MODULENAME)) {    
        echo 'Store Module is exists';
    }else {
      $moduleInstance             = new Vtiger_Module(); 
      $moduleInstance->name       = $MODULENAME;
      $moduleInstance->tablabel   = 'LBL_STORE';
      $moduleInstance->parent     = 'Inventory';
      $moduleInstance->save();
      
      // Schema Setup
      $moduleInstance->initTables();
    
      // Webservice Setup
      $moduleInstance->initWebservice();
      
      $block        = new Vtiger_Block();
      $block->label = 'LBL_STORE_INFORMATION';
      $block        = $block->getInstance($block->label, $moduleInstance);
      if ($block == false) {
          $block        = new Vtiger_Block();
          $block->label = 'LBL_STORE_INFORMATION';
          $moduleInstance->addBlock($block);
          $block        = $block->getInstance($block->label, $moduleInstance);
      }
      
      $filter = Vtiger_Filter::getInstance('All', $moduleInstance);
      if (empty($filter)) {
          // Filter Setup
          $filter            = new Vtiger_Filter();
          $filter->name      = 'All';
          $filter->isdefault = true;
          $moduleInstance->addFilter($filter);
      }
        
      //Adding fields
      $field = Vtiger_Field::getInstance('store_name', $moduleInstance);
      if (empty($field)) {
        $field              = new Vtiger_Field();
        $field->name        = 'store_name';
        $field->label       = 'LBL_STORE_NAME';
        $field->uitype      = 2;      
        $field->presence    = 1;
        $field->displaytype = 3;
        $field->column      = $field->name;
        $field->columntype  = 'VARCHAR(20)';
        $field->typeofdata  = 'V~M';
        $field->sequence    = 1;
        $field->table       = $module->basetable;
        $block->addField($field);
        
        $filter->addField($field, 1);
        
        $moduleInstance->setEntityIdentifier($field);
        echo '\n fieldname' .$field->name .'added successfully'."\n";
      }
    
    
      $field = Vtiger_Field::getInstance('store_number', $moduleInstance);
      if (empty($field)) {
        $field  = new Vtiger_Field();
        $field->name        = 'store_number';
        $field->label       = 'LBL_STORE_NUMBER';
        $field->uitype      = 4;       
        $field->presence    = 2;
        $field->displaytype = 3;
        $field->column      = $field->name;
        $field->columntype  = 'VARCHAR(20)';
        $field->typeofdata  = 'V~O';
        $field->sequence    = 2;
        $field->table       = $module->basetable;
        $block->addField($field);
        
        $filter->addField($field, 2);
        echo "\n Field ".$field->name." added.\n";
      }
    
      $field = Vtiger_Field::getInstance('store_address', $moduleInstance);
      if (empty($field)) {
        $field  = new Vtiger_Field();
        $field->name        = 'store_address';
        $field->label       = 'LBL_STORE_ADDRESS';
        $field->uitype      = 16;
        $field->presence    = 2;
        $field->displaytype = 1;
        $field->column      = $field->name;
        $field->columntype  = 'VARCHAR(20)';
        $field->typeofdata  = 'V~O';
        $field->sequence    = 3;
        $field->table       = $module->basetable;
        $block->addField($field);
    
        $filter->addField($field, 3);
        echo "\n Field ".$field->name." added.\n";
      }
    
      $field = Vtiger_Field::getInstance('assigned_to', $moduleInstance);
      if (empty($field)) {
        $field  = new Vtiger_Field();
        $field->name        = 'assigned_to';
        $field->label       = 'LBL_ASSIGNED_TO';
        $field->uitype      = 16;
        $field->presence    = 2;
        $field->displaytype = 1;
        $field->column      = $field->name;
        $field->columntype  = 'VARCHAR(20)';
        $field->typeofdata  = 'V~O';
        $field->sequence    = 5;
        $field->table       = 'vtiger_crmentity';
        $block->addField($field);
        
        $filter->addField($field, 4);
        echo "\n Field ".$field->name." added.\n";
      }
      
      Settings_MenuEditor_Module_Model::addModuleToApp($moduleInstance->name, $moduleInstance->parent);
      echo 'Store Module is Created';
    }
    ?>