Programmer Tips

Ubuntu Command: Edit Crontab

env EDITOR=nano crontab -e

PHP: Simple method to retrieve POSTed data

All the posted data can be retrieved  by a single line code; using the file_get_contents() method.

$poestedData = file_get_contents('php://input');

However; this will not work with the encryption,’multipart/form-data’.


Singleton Pattern

Singleton pattern make use of unique resource for the entire application; the very simple example for model is a database connection. Here database  connection occurs only once; never get duplicated.

class DB {

private static $_singleton;

private $_connection;

private function __construct()

{

$this->_connection = mysql_connect();

}

public static function getInstance()

{

if (is_null (self::$_singleton)) {

self::$_singleton = new DB();

}

return self::$_singleton;

}

}

$db = DB::getInstance(); 

Affect of reference variables

Consider the array: $a and the below script:

$a = array (’A’,’B’,’C’);
foreach ($a as &$v) {
}
foreach ($a as $v) {
}
print_r ($a);


After the first loop, values of the array will remain intact; but it is looping through the reference variable &$v; but this is still storing address of the last element of the array after complete execution of the first loop. In the next loop we are using the variable $v as value, hence whatever assigned to it will be copied to the last element of the array. So the output will be:

Array
(
[0] => A
[1] => B
[2] => B
)

Creating custom view for SugarCRM modules
  1. Create view file inside modulename/views folder: view.custom.php

require_once(‘include/MVC/View/SugarView.php’);

class Viewcustom extends SugarView {

public function __construct() {

parent::SugarView();

}

public function preDisplay() {

$this->dv->tpl = ‘modules/modulename/tpl/custom.tpl’;

}

public function display() {

global $db;

global $sugar_custom;

$cUser = $GLOBALS[‘current_user’];

if(!$cUser->is_admin) {

echo ‘You do not have permission to view this page.’;

return;

}

// $this->ss->assign(‘myname’, ‘Tester Tester’);

$smarty = new Sugar_Smarty();

parent::display();

$smarty->display($this->dv->tpl);

}

}

  1. Create a Smarty based tpl file inside tpl folder: custom.tpl

3.Inside Controller.php for that module, write function for handling custom action public function action_custom()

{

$this->view = ‘custom’;

}//End: function action_custom()

4. This view can be accessed by: index.php?module=modulename&action=custom

SugarCRM: Custom Module creation without using admin panel

SugarCRM: Create Custom module without using Admin Studio, use following steps:

1. Copy any of the working module folder and rename it our new module name ‘custmod’

   Renam class file names and class names accordingly

   custmod/Controller.php

   custmod/vardef.php

   custmod/custmod.php

   Remove unwanted fields from vardefs.php

   Place this folder inside ‘modules’ folder

2. Create database table for customod; add fields if there is any extra field added to vardefs.php

CREATE TABLE IF NOT EXISTS `custmod` (

  `assigned_user_id` char(36) DEFAULT NULL,

  `team_id` char(36) DEFAULT NULL,

  `team_set_id` char(36) DEFAULT NULL,

  `id` char(36) NOT NULL,

  `date_entered` datetime DEFAULT NULL,

  `date_modified` datetime DEFAULT NULL,

  `modified_user_id` char(36) DEFAULT NULL,

  `created_by` char(36) DEFAULT NULL,

  `name` varchar(255) DEFAULT NULL,

  `description` text,

  `deleted` tinyint(1) DEFAULT ‘0’,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

3. Add ‘custmod’ in to modules list:

custom/application/Ext/Include/modules.ext.php

$beanList[‘custmod’] = ‘custmod’;

$beanFiles[‘custmod’] = ‘modules/custmod/custmod.php’;

$moduleList[] = ‘custmod’;

4. Insert entries in to ACL actions table (ids are primary key; they must be unique)

INSERT INTO `acl_actions` (`id`, `date_entered`, `date_modified`, `modified_user_id`, `created_by`, `name`, `category`, `acltype`, `aclaccess`, `deleted`) VALUES

(‘2368e6f0-d560-c9b6-8a2f-4f154g467t83’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘access’, ‘custmod’, ‘module’, 89, 0),

(‘230d8a49-0717-09bc-84be-4f15ga1e1h2a’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘admin’, ‘custmod’, ‘module’, 1, 0),

(‘24cc531b-bf0f-1d48-6faf-4f16ga5b8fea’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘delete’, ‘custmod’, ‘module’, 90, 0),

(‘2476ee4e-817a-3ef3-d015-4f1g4aeft467’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘edit’, ‘custmod’, ‘module’, 90, 0),

(‘2584e97f-75d6-3d21-08ed-4fyg4a1a0127’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘export’, ‘custmod’, ‘module’, 90, 0),

(‘2532a115-c9cd-c641-d2a7-4f65ga0c50e8’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘import’, ‘custmod’, ‘module’, 90, 0),

(‘2418ce73-d87b-bb50-3a93-4fdg4a0efa55’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘list’, ‘custmod’, ‘module’, 90, 0),

(‘25cc26b9-8285-cdff-3215-4f1g4a62d499’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘massupdate’, ‘custmod’, ‘module’, 90, 0),

(‘23c1c4d3-d1fe-abd7-2cb4-4f1ghabc6960’, ‘2012-02-17 10:18:04’, ‘2012-02-17 10:18:04’, ‘1’, ‘1’, ‘view’, ‘custmod’, ‘module’, 90, 0);

5. Add in to bean list:custom/Extension/application/Ext/Include/packagename.php (use any of the existing packages there)

$beanList[‘custmod’] = ‘custmod’;

$beanFiles[‘custmod’] = ‘modules/custmod/custmod.php’;

$moduleList[] = ‘custmod’;

6. Add name in to langugage file:

/custom/Extension/application/Ext/Language/en_us.packagename.php

$app_list_strings[‘moduleList’][‘custmod’] = ‘Custom Module Name’;

7. ‘Repair and Rebuild’ rebuild from Admin Panel

8.Add custmod in to main menu from Admin panel ‘Display Module Tabs and Subpanels’

also this module can be accessed as:

/index.php?module=custmod

Adding value in to SugarCRM configuration array using script

function add_to_config($key,$value)

   {

        global $sugar_config;

        $configurator   = new Configurator();

        $overrideArray  = $configurator->readOverride();

        $configurator->previous_sugar_override_config_array = $overrideArray;

        $diffArray      = deepArrayDiff($configurator->config, $sugar_config);

        $overrideArray  = sugarArrayMerge($overrideArray, $diffArray);

        $overrideArray[$key] = $value;

        $overideString = “<?php\n/***CONFIGURATOR***/\n”;

        sugar_cache_put(‘sugar_config’, $configurator->config);

        $GLOBALS[‘sugar_config’] = $configurator->config;

        foreach($overrideArray as $key => $val) {

            if (in_array($key, $configurator->allow_undefined) || isset ($sugar_config[$key])) {

                if (strcmp(“$val”, ‘true’) == 0) {

                    $val = true;

                    $conf->config[$key] = $val;

                }

                if (strcmp(“$val”, ‘false’) == 0) {

                    $val = false;

                    $conf->config[$key] = false;

                }

            }

            $overideString .= override_value_to_string_recursive2(‘sugar_config’, $key, $val);

        }

        $overideString .= ‘/***CONFIGURATOR***/’;

        $configurator->saveOverride($overideString);

   }//End: add_to_config($key,$value)

SugarCRM: Package: Sample manifest.php

$manifest = array (

  0 => 

  array (

    ‘acceptable_sugar_versions’ => 

    array (

      0 => ”,

    ),

  ),

  1 => 

  array (

    ‘acceptable_sugar_flavors’ => 

    array (

      0 => ‘PRO’,

    ),

  ),

  ‘readme’ => ”,

  ‘key’ => ‘XXX’,

  ‘author’ => ‘XXXXXXXXXX’,

  ‘description’ => ‘XXXXXXXXXXXXX Package’,

  ‘icon’ => ”,

  ‘is_uninstallable’ => true,

  ‘name’ => ‘XXX’,

  ‘published_date’ => ‘2012-02-07 09:31:30’,

  ‘type’ => ‘module’,

  ‘version’ => 1328607090,

  ‘remove_tables’ => ‘prompt’,

);

$installdefs = array (

  ‘id’ => ‘XXX’,

  ‘beans’ => 

  array (

    0 => 

    array (

      ‘module’ => ‘XXX_yyyy’,

      ‘class’ => ‘XXX_yyyy’,

      ‘path’ => ‘modules/XXX_yyyy/XXX_yyyy.php’,

      ‘tab’ => true,

    ),

    1 => 

    array (

      ‘module’ => ‘XXX_ccccy’,

      ‘class’ => ‘XXX_ccccy’,

      ‘path’ => ‘modules/XXX_ccccy/XXX_ccccy.php’,

      ‘tab’ => true,

    ),

  ),

  ‘layoutdefs’ => 

  array (

  ),

  ‘relationships’ => 

  array (

  ),

  ‘image_dir’ => ‘<basepath>/icons’,

  ‘copy’ => 

  array (

    0 => 

    array (

      ‘from’ => ‘<basepath>/SugarModules/modules/XXX_yyyy’,

      ‘to’ => ‘modules/XXX_yyyy’,

    ),

    1 => 

    array (

      ‘from’ => ‘<basepath>/SugarModules/modules/XXX_ccccy’,

      ‘to’ => ‘modules/XXX_ccccy’,

    ),

   ),

  ‘relationships’ =>

    array(

        array(

            ‘meta_data’ => ‘<basepath>/custom/modules/relationships/relationships/XXX_yyyy_XXX_ccccyMetaData.php’,

        ),

    ),

    ‘logic_hooks’ => array(

        array(

            ‘module’ => ‘Contacts’,

            ‘hook’ => ‘before_save’,

            ‘order’ => 99,

            ‘description’ => ‘xxxxxxxxxxxxxxx’,

            ‘file’ => ‘custom/modules/Contacts/on_contact_save.php’,

            ‘class’ => ‘save_contact’,

            ‘function’ => ‘before_save_contact’,

        ),

    ),

  ‘language’ => 

  array (

    0 => 

    array (

      ‘from’ => ‘<basepath>/SugarModules/language/application/en_us.lang.php’,

      ‘to_module’ => ‘application’,

      ‘language’ => ‘en_us’,

    ),

  ),

);

Creating Admin Link and Page in SugarCRM

1. Created page:

/custom/Extension/modules/Administration/Ext/Administration/aaa_bbb_admin.php

 $admin_option_defs=array();

    $admin_option_defs[‘Administration’][‘aaa_bbb_admin’]= array(

    $image_path . ‘aaa_bbb_admin’,’LBL_aaa_bbb_ADMIN_CONFIG1_TITLE’,’LBL_aaa_bbb_ADMIN_CONFIG1_DESC’,

    ‘./index.php?module=aaa_bbb_admin&action=config’);

    $admin_group_header[]= array(‘LBL_aaa_bbb_ADMIN_CONFIG_HEADER’,”,false,$admin_option_defs, ”);

    $config_categories[] = ‘aaa_bbb_admin’;

2. /custom/Extension/modules/Administration/Ext/Language/en_us.lang.php
‘LBL_SMC_bbb_ADMIN_CONFIG_HEADER’ => “bbb ddd Configuration”,
‘LBL_SMC_bbb_ADMIN_CONFIG1_TITLE’ => “bbb ddd Configuration “,
‘LBL_SMC_bbb_ADMIN_CONFIG1_DESC’ => “Manage Settings for bbb ddd”,
‘LBL_SMC_bbb_ADMIN_CONFIG1_DESC’ => “Settings for bbb Sugar hhhh ddd”,
‘LBL_SMC_bbb_ADMIN_CONFIG1_TITLE’ => “Manage Settings for SUMA ddd”
3.Create Admin Moduule
4. Inside views folder; created view.config.php
require_once(‘include/MVC/View/SugarView.php’);
class ViewConfig extends SugarView {
    public function __construct() {
        parent::SugarView();
    }
    public function preDisplay() {
$this->dv->tpl = ‘modules/aaa_bbb_admin/tpl/config.tpl’;
    }
    public function display() {
        global $db;
global $sugar_config;
        $cUser = $GLOBALS[‘current_user’];
        if(!$cUser->is_admin) {
            echo ‘You do not have permission to view this page.’;
            return;            
        }
        $smarty = new Sugar_Smarty();
       $smarty->assign(‘mage_db_name’, $sugar_config[‘mage_db_name’]);
$smarty->assign(‘mage_db_pwd’, $sugar_config[‘mage_db_pwd’]);
        parent::display();
$smarty->display($this->dv->tpl);//$this->dv->tpl);
}
}
5.Created tpl file: in modulefolder/tpl/ 
config.tpl
SMarty content and js files if needed.
6.Create action in controller file:
Modulefolder/controller.php
 public function action_config()
    {
        $this->view = ‘config’;
    }
configuraiton saving aciton:
 public function action_config_save()
    {  
/////
///
}
this can acceess by :
index.php?module=modulename&action=config