<?php
if (! defined('sugarEntry') || ! sugarEntry) die('Not A Valid Entry Point');

//At bottom of post_install - redirect to license validation page - CHANGE NAME BELOW - To your module name

function post_install() {
	global $db;
    // Creating table twilio_inbound_message_bean_bean_rel
    if (!$db->tableExists('tac_inbound_message_bean_rel')) {

        $qry="CREATE TABLE tac_inbound_message_bean_rel (
			  id char(36) NOT NULL,
			  message_id char(36) NOT NULL,
			  bean_id char(36) NOT NULL,
			  bean_module varchar(100) DEFAULT NULL,
			  date_modified datetime DEFAULT NULL,
			  deleted tinyint(1) DEFAULT '0',
			  PRIMARY KEY ( id )
			) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		
        $db->query($qry);
    }
    
    // Inserts Twilio Scheduler Column
    $result= $db->query("SELECT * from schedulers where id='dfc34d78-8beb-85ee-97b1-5a586797801e'");
	$exists = ($db->getRowCount($result))?TRUE:FALSE;

	if(!$exists) {
		$insert_qry = "INSERT INTO `schedulers` (`id`, `deleted`, `date_entered`, `date_modified`, `created_by`, `modified_user_id`, `name`, `job`, `date_time_start`, `date_time_end`, `job_interval`, `time_from`, `time_to`, `last_run`, `status`, `catch_up`) VALUES ('dfc34d78-8beb-85ee-97b1-5a586797801e', 0, '2018-01-17 07:44:05', '2018-01-15 04:24:50', '1', '1', 'Twilio Send SMS Scheduler', 'function::twilio_sms_scheduler', '2018-01-01 07:45:00', NULL, '*/5::*::*::*::*', '12:00:00', NULL, NULL, 'Active', 1);";
		$db->query($insert_qry);
		echo "Scheduler created successfully";
	}
	
	//install table for user management
    if (!$db->tableExists('so_users')) {

        $fieldDefs = array(
            'id' => array (
              'name' => 'id',
              'vname' => 'LBL_ID',
              'type' => 'id',
              'required' => true,
              'reportable' => true,
            ),
            'deleted' => array (
                'name' => 'deleted',
                'vname' => 'LBL_DELETED',
                'type' => 'bool',
                'default' => '0',
                'reportable' => false,
                'comment' => 'Record deletion indicator',
            ),
            'shortname' => array (
                'name' => 'shortname',
                'vname' => 'LBL_SHORTNAME',
                'type' => 'varchar',
                'len' => 255,
            ),
            'user_id' => array (
                'name' => 'user_id',
                'rname' => 'user_name',
                'module' => 'Users',
                'id_name' => 'user_id',
                'vname' => 'LBL_USER_ID',
                'type' => 'relate',
                'isnull' => 'false',
                'dbType' => 'id',
                'reportable' => true,
                'massupdate' => false,
            ),
        );
        
        $indices = array(
            'id' => array (
                'name' => 'so_userspk',
                'type' => 'primary',
                'fields' => array (
                    0 => 'id',
                ),
            ),
            'shortname' => array (
                'name' => 'shortname',
                'type' => 'index',
                'fields' => array (
                    0 => 'shortname',
                ),
            ),
        );
        $db->createTableParams('so_users',$fieldDefs,$indices);
    }
    
//redirect to license validation page 
    global $sugar_version;
    if(preg_match( "/^6.*/", $sugar_version)) {
        echo "
            <script>
            document.location = 'index.php?module=tac_sms_templates&action=license';
            </script>"
        ;
    } else {
        echo "
            <script>
            var app = window.parent.SUGAR.App;
            window.parent.SUGAR.App.sync({callback: function(){
                app.router.navigate('#bwc/index.php?module=tac_sms_templates&action=license', {trigger:true});
            }});
            </script>"
        ;
    }
}

