<?php

/**
 * PluginOlark File
 *
 * @category LiveSupport
 * @package  Plugin
 * @author   Alberto Vasquez <alberto@clientexec.com>
 * @license  ClientExec License
 * @version  [someversion]
 * @link     http://www.clientexec.com
 */
require_once 'modules/admin/models/LiveSupportPlugin.php';

/**
 * PluginOlark Model Class
 *
 * @category LiveSupport
 * @package  Plugin
 * @author   Alberto Vasquez <alberto@clientexec.com>
 * @license  ClientExec License
 * @version  [someversion]
 * @link     http://www.clientexec.com
 * @abstract
 */
class PluginOlark extends LiveSupportPlugin {

    /**
     * Variables that make this plugin unique
     *
     * @return array
     */
    function getVariables() {
        /* Specification
          itemkey     - used to identify variable in your other functions
          type          - text,textarea,yesno,password
          description - description of the variable, displayed in ClientExec
         */

        $variables = array(
            /* T */"Plugin Name"/* /T */ => array(
                "type" => "hidden",
                "description" => /* T */"How CE sees this plugin (not to be confused with the Signup Name)"/* /T */,
                "value" => /* T */"Olark"/* /T */
            ),
            /* T */"Enabled"/* /T */ => array(
                "type" => "yesno",
                "description" => /* T */"When enabled, customer groups have additional functionality."/* /T */,
                "value" => "0"
            ),
            /* T */"Available To All Visitors"/* /T */ => array(
                "type" => "yesno",
                "description" => /* T */"Select Yes, if you want all visitors to your site to support Live Chat. Selecting No will limit it to the customer groups that have this feature enabled."/* /T */,
                "value" => "0"
            ),
            /* T */"Site Id"/* /T */ => array(
                "type" => "text",
                "description" => /* T */"Site identification with Olark, located in the Install tab."/* /T */,
                "value" => ""
            ),
            /* T */"Customer Group"/* /T */ => array(
                "type" => "text",
                "description" => /* T */"(Optional) Group you want these chats sent to."/* /T */,
                "value" => ""
            ),
            /* T */"PartnerLink"/* /T */ => array(
                "type" => "hidden",
                "description" => "",
                "value" => "http://www.olark.com/?r=152fzqdq"
            )
        );
        return $variables;
    }

    /**
     * Code to paste in footer for this particular plugin
     *
     * @return html;
     */
    public function getCode() 
    {

        $html = '<!-- begin olark code --> <script type=\'text/javascript\'>/*{literal}<![CDATA[*/ window.olark||(function(k){var g=window,j=document,a=g.location.protocol=="https:"?"https:":"http:",i=k.name,b="load",h="addEventListener";(function(){g[i]=function(){(c.s=c.s||[]).push(arguments)};var c=g[i]._={},f=k.methods.length;while(f--){(function(l){g[i][l]=function(){g[i]("call",l,arguments)}})(k.methods[f])}c.l=k.loader;c.i=arguments.callee;c.p={0:+new Date};c.P=function(l){c.p[l]=new Date-c.p[0]};function e(){c.P(b);g[i](b)}g[h]?g[h](b,e,false):g.attachEvent("on"+b,e);c.P(1);var d=j.createElement("script"),m=document.getElementsByTagName("script")[0];d.type="text/javascript";d.async=true;d.src=a+"//"+c.l;m.parentNode.insertBefore(d,m);c.P(2)})()})({loader:(function(a){return "static.olark.com/jsclient/loader1.js?ts="+(a?a[1]:(+new Date))})(document.cookie.match(/olarkld=([0-9]+)/)),name:"olark",methods:["configure","extend","declare","identify"]}); olark.identify(\''.trim($this->getVariable("Site Id")).'\');/*]]>{/literal}*/</script> <!-- end olark code -->';
        $html .= "<script type='text/javascript'>";

        if (trim($this->getVariable("Customer Group")) != "") {
            $html .= "olark.configure('system.group', '" . $this->getVariable("Customer Group") . "');";
        }
        $html .= "if (gCustomerId!=0) {
                    olark('api.chat.updateVisitorNickname', {snippet: gUserFirstName+' | #'+gCustomerId, hidesDefault: true});
                  } else {
                    olark('api.chat.updateVisitorNickname', {snippet: 'Visitor', hidesDefault: true});
                  }";
        $html .= "olark('api.chat.onCommandFromOperator', function(event) {";
        $html .= "  if (event.command.name == 'kb') {";
        $html .= "      if (isInteger(event.command.body)) window.location = \"" . $this->settings->get("ClientExec URL") . "/index.php?fuse=knowledgebase&view=KB_viewArticle&articleId=\"+event.command.body;";
        $html .= "  }";
        $html .= "});";
        //this can be replaced to your own partners link if you have one
        $html .= "$(document).ready(function() { 
                    setTimeout( function() {
                        $(\"a:contains('Olark')\").attr('href','http://www.olark.com/?r=152fzqdq');
                    }
                    , 1500);
                  });";
        $html .= "</script>";
        return $html;
    }

}