Santosh Kumar
Santosh Kumar

Reputation: 19

How can i export & Import custom modules as Package using PHP Script (Vtlib Function) from my CRM vtiger 7

I hope all of you are doing well. I have created a custom module used this script.we have run this script in root folder.All field are working fine and list view also perfect.

```
<?php
include_once 'vtlib/Vtiger/Module.php';
include_once 'vtlib/Vtiger/Package.php';
include_once 'includes/main/WebUI.php';
include_once 'include/Webservices/Utils.php';
$Vtiger_Utils_Log = true;

$MODULENAME = 'Laptop';

$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);

// Text Area1 Item_Name

$itemName  = new Vtiger_Field();
    $itemName->name = 'itemname';
    $itemName->label= 'Item Name';
    $itemName->uitype= 1;
    $itemName->column = $itemName->name;
    $itemName->columntype = 'VARCHAR(100)';
    $itemName->typeofdata = 'V~M';
    $block->addField($itemName);

     // Text Area2 Item_Deatils
    $itemdeatils  = new Vtiger_Field();
    $itemdeatils->name = 'itemdeatils';
    $itemdeatils->label= 'Item Deatils';
    $itemdeatils->uitype= 1;
    $itemdeatils->column = $itemdeatils->name;
    $itemdeatils->columntype = 'VARCHAR(100)';
    $itemdeatils->typeofdata = 'V~O';
    $block->addField($itemdeatils);

    $moduleInstance->setEntityIdentifier($itemdeatils);
    
  // Text Area3 Item_Company.

    $companyname  = new Vtiger_Field();
    $companyname->name = 'companyname';
    $companyname->label= 'Company Name.';
    $companyname->uitype= 15;
    $companyname->column = $companyname->name;
    $companyname->columntype = 'VARCHAR(100)';
    $companyname->typeofdata = 'V~O';
    $block->addField($companyname);  

    $companyname->setPicklistValues( Array ('Sumsung','HP','Dell', 'Lenovo','Apple') ); 


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

    // 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 = 'DT~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 = 'DT~O';
    $mfield3->displaytype= 2;
    $block->addField($mfield3);

    /* NOTE: Vtiger 7.1.0 onwards */
    $mfield4 = new Vtiger_Field();
    $mfield4->name = 'source';
    $mfield4->label = 'Source';
    $mfield4->table = 'vtiger_crmentity';
    $mfield4->displaytype = 2; // to disable field in Edit View
    $mfield4->quickcreate = 3;
    $mfield4->masseditable = 0;
    $block->addField($mfield4);

    $mfield5 = new Vtiger_Field();
    $mfield5->name = 'starred';
    $mfield5->label = 'starred';
    $mfield5->table = 'vtiger_crmentity_user_field';
    $mfield5->displaytype = 6;
    $mfield5->uitype = 56;
    $mfield5->typeofdata = 'C~O';
    $mfield5->quickcreate = 3;
    $mfield5->masseditable = 0;
    $block->addField($mfield5);

    $mfield6 = new Vtiger_Field();
    $mfield6->name = 'tags';
    $mfield6->label = 'tags';
    $mfield6->displaytype = 6;
    $mfield6->columntype = 'VARCHAR(1)';
    $mfield6->quickcreate = 3;
    $mfield6->masseditable = 0;
    $block->addField($mfield6);
    /* End 7.1.0 */

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

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

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

  $targetpath = 'modules/' . $moduleInstance->name;

      if (! is_file($targetpath)) {
      mkdir($targetpath);

    $templatepath = 'vtlib/ModuleDir/6.0.0';

   $moduleFileContents = file_get_contents($templatepath . '/ModuleName.php');
    $replacevars = array(
    'ModuleName' => $moduleInstance->name,
    '<modulename>' => strtolower($moduleInstance->name),
    '<entityfieldlabel>' => $field1->label,
    '<entitycolumn>' => $field1->column,
    '<entityfieldname>' => $field1->name
    );

        foreach ($replacevars as $key => $value) {
    $moduleFileContents = str_replace($key, $value, $moduleFileContents);
       }
      file_put_contents
      ($targetpath . '/' . $moduleInstance->name . '.php',$moduleFileContents);
          }

            if (! file_exists('languages/en_us/ModuleName.php')) {
       $ModuleLanguageContents = file_get_contents
       ($templatepath . '/languages/en_us  /ModuleName.php');

     $replaceparams = array(
    'Module Name' => $moduleInstance->name,
    'Custom' => $moduleInstance->name,
    'ModuleBlock' => $moduleInstance->name,
    'ModuleFieldLabel Text' => $field1->label
     );

     foreach ($replaceparams as $key => $value) {
     $ModuleLanguageContents = str_replace($key, $value, $ModuleLanguageContents);
   }

$languagePath = 'languages/en_us';
file_put_contents
    ($languagePath . '/' . $moduleInstance->name . '.php',$ModuleLanguageContents);
      }
    
        }
    Settings_MenuEditor_Module_Model::addModuleToApp
            ($moduleInstance->name, $moduleInstance->parent);
         ?>
     ```

how can i export & Import custom modules as Package using PHP Script (Vtlib Function) from my CRM vtiger 7, Please help me.

Upvotes: 0

Views: 812

Answers (1)

Manish Kumar
Manish Kumar

Reputation: 1

I was stuck on How to create a custom module in Vtiger using VTlib library. The code you shared is perfect for creating custom module in vtiger.

To Export your custom module in vtiger, you need to run the code below -

require_once('vtlib/Vtiger/Package.php');
require_once('vtlib/Vtiger/Module.php');
$package = new Vtiger_Package();
$package->export(
Vtiger_Module::getInstance('Payslip'),
'test/vtlib',
'Payslip-Export.zip',
true
);

Don't forget to change the module name

Upvotes: 0

Related Questions