<?php
require_once('data/SugarBean.php');
class Iframeapp extends SugarBean {

  // corresponds with $manifest['version'] in manifest.php
  public static $module_version = '5.0.2';

  var $id;
  var $name;
  var $date_entered;
  var $date_modified;
  var $modified_user_id;
  var $created_by;
  var $description;
  var $deleted;
  var $iframe_src;
  var $iframe_height;
  var $team_id;
  var $team_set_id;
  var $assigned_user_id;

  var $created_by_name;
  var $modified_by_name;
  var $assigned_user_name;

  var $module_dir = 'Iframeapp';
  var $table_name = "iframeapp";
  var $object_name = "Iframeapp";
  var $importable = FALSE;
  var $new_schema = TRUE;

  public function bean_implements($interface) {
    switch($interface){
      case 'ACL':
        return TRUE;
    }
    return FALSE;
  }

  public function get_summary_text() {
    return $this->$module_dir;
  }

  public static function get_module_version() {
    return '&mv=' . self::$module_version;
  }

  /**
   * Build the encrypted timestamp for the INBOX25 API calls
   * @return string
   */
  public static function get_url_ts() {
    $ts = '&ts=' . time() . '&rfr=' . urlencode(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
    return $ts;
  }

  /**
   * Load current INBOX25 URL
   * @returns mixed string|null
   */
  public static function get_config_url() {
    global $sugar_config;
    return isset($sugar_config['inbox25']['url']) ? $sugar_config['inbox25']['url'] : NULL;
  }

  /**
   * Load current INBOX25 config iframe height
   * @returns mixed string|null
   */
  public static function get_config_height() {
    global $sugar_config;
    return isset($sugar_config['inbox25']['height']) ? $sugar_config['inbox25']['height'] : '800';
  }

  /**
   * Does the iFrame URL tell us we going to enable the logic hook sync.
   * functionality with the INBOX25 API?
   * @returns bool
   */
  public static function get_logic_hook_enabled() {
    $iframe_src = self::get_config_url();
    if(is_null($iframe_src)) {
      return FALSE;
    }

    $domain_details = parse_url($iframe_src);
    $domain = $domain_details['host'];
    $query_string = $domain_details['query'];
    $query_part = explode('&', $query_string);
    foreach( $query_part as $part ) {
      if( $part == 'ma=0') {
        return FALSE;
      }
    }

    return TRUE;
  }

}
