<?php

class AtomsMSmtpCustomFile{
	function __construct(){
		error_log( "AtomsMSmtpCustomFile __construct" , 3 , "/tmp/AtomsMSmtp.log" );

	}

	function postInstall(){
		$this->createCustomTables();
		$this->SettingsLink();
	}

	function postEnable(){
	}

	function postDisable(){
	}

	function postUpdate(){
		error_log( "createCustomTables - SettingsLink" , 3 , "/tmp/AtomsMSmtp.log" );
		$this->createCustomTables();
		$this->SettingsLink();
	}

	function createCustomTables(){
		global $adb;

		$table_sql['vtiger_atoms_msmtp'] = "CREATE TABLE `vtiger_atoms_msmtp` (
			`id` int(19) NOT NULL,
			`config_type` varchar(255) DEFAULT NULL,
			`userids` varchar(255) DEFAULT NULL,
			`server` varchar(100) DEFAULT NULL,
			`server_port` int(19) DEFAULT NULL,
			`server_username` varchar(100) DEFAULT NULL,
			`server_password` varchar(100) DEFAULT NULL,
			`server_type` varchar(20) DEFAULT NULL,
			`smtp_auth` varchar(5) DEFAULT NULL,
			`server_path` varchar(256) DEFAULT NULL,
			`from_email_field` varchar(50) DEFAULT NULL,
			PRIMARY KEY (`id`)
				);";
		foreach($table_sql as $table_name => $sql){
			$table_exist_result = $adb->pquery("SHOW TABLES LIKE '$table_name'",array());
			$num_rows = $adb->num_rows($table_exist_result);

			if($num_rows == 0){
				$adb->pquery($sql,array());
			}

		}
		$adb->pquery("UPDATE vtiger_tab SET customized = 0 WHERE name = 'AtomsMSmtp'",array());
	}

	function SettingsLink(){
                global $adb;
                $name = "LBL_ATOMS_MSMTP_SETTINGS";
                $description = "Configure Multiple SMTP";
                $linkto = "index.php?parent=Settings&module=AtomsMSmtp&view=List";
                $result = $adb->pquery("SELECT * FROM vtiger_settings_field WHERE name= ?",array($name));
		error_log( "SELECT * FROM vtiger_settings_field WHERE name= '{$name}'" , 3 , "/tmp/AtomsMSmtp.log" );
                $num_rows = $adb->num_rows($result);
                if($num_rows == 0) {
                        $otherSettingsBlock = $adb->pquery('SELECT * FROM vtiger_settings_blocks WHERE label=?', array('LBL_CONFIGURATION'));
                        $otherSettingsBlockCount = $adb->num_rows($otherSettingsBlock);

                        if ($otherSettingsBlockCount > 0) {
                                $blockid = $adb->query_result($otherSettingsBlock, 0, 'blockid');
                                $sequenceResult = $adb->pquery("SELECT max(sequence) as sequence FROM vtiger_settings_blocks WHERE blockid=?", array($blockid));
                                if ($adb->num_rows($sequenceResult)) {
                                        $sequence = $adb->query_result($sequenceResult, 0, 'sequence');
                                }
                        }

                        $fieldid = $adb->getUniqueID('vtiger_settings_field');

                        $adb->pquery("INSERT INTO vtiger_settings_field(fieldid, blockid, name, iconpath, description, linkto, sequence, active , pinned) VALUES(?,?,?,?,?,?,?,?,?)", array($fieldid, $blockid, $name, '',$description, $linkto, $sequence++, 0 , 1));

                        $adb->pquery("UPDATE vtiger_settings_field_seq SET id = ?",array($fieldid));
                        echo("$name Settings Added <br/>");
                }else{

                        echo("$name Already present <br/>");
                }
        }
}

?>
