From 51f6dbc7fbc28bd1e69c3a9dc0cccb50912785c4 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Tue, 16 Nov 2010 15:01:33 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B0=205.2=20=E6=AD=A3?= =?UTF-8?q?=E5=BC=8F=E7=89=88=E5=90=8E=E7=AC=AC=E4=B8=80=E6=AC=A1=20SVN=20?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit yuchenghu@hawebs.net git-svn-id: https://svn.code.sf.net/p/hawebs/svn@627 a2543c7e-f6e9-4f8a-8bff-1ffc34733512 --- .../modules/PBXManager/AsteriskUtils.php | 221 +++++++++++ .../modules/PBXManager/CallRelatedList.php | 11 + .../trunk/modules/PBXManager/CustomView.php | 13 + .../trunk/modules/PBXManager/Delete.php | 26 ++ .../trunk/modules/PBXManager/DetailView.php | 11 + .../modules/PBXManager/DetailViewAjax.php | 11 + .../trunk/modules/PBXManager/EditView.php | 35 ++ .../modules/PBXManager/ExportRecords.php | 13 + .../trunk/modules/PBXManager/Import.php | 13 + .../trunk/modules/PBXManager/ListView.php | 180 +++++++++ .../modules/PBXManager/ListViewPagging.php | 11 + .../trunk/modules/PBXManager/PBXManager.js | 9 + .../trunk/modules/PBXManager/PBXManager.php | 347 ++++++++++++++++++ .../modules/PBXManager/PBXManagerAjax.php | 11 + oss/vtiger/trunk/modules/PBXManager/Save.php | 43 +++ .../trunk/modules/PBXManager/Settings.php | 46 +++ .../trunk/modules/PBXManager/StartCall.php | 61 +++ .../trunk/modules/PBXManager/TagCloud.php | 11 + .../modules/PBXManager/TraceIncomingCall.php | 127 +++++++ .../modules/PBXManager/UnifiedSearch.php | 11 + .../modules/PBXManager/UpdatePBXDetails.php | 32 ++ oss/vtiger/trunk/modules/PBXManager/index.php | 14 + .../PBXManager/language/en_us.lang.php | 20 + .../PBXManager/language/zh_cn.lang.php | 33 ++ .../trunk/modules/PBXManager/schema.xml | 15 + .../PBXManager/utils/AsteriskClass.php | 176 +++++++++ 26 files changed, 1501 insertions(+) create mode 100644 oss/vtiger/trunk/modules/PBXManager/AsteriskUtils.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/CallRelatedList.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/CustomView.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/Delete.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/DetailView.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/DetailViewAjax.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/EditView.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/ExportRecords.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/Import.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/ListView.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/ListViewPagging.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/PBXManager.js create mode 100644 oss/vtiger/trunk/modules/PBXManager/PBXManager.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/PBXManagerAjax.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/Save.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/Settings.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/StartCall.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/TagCloud.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/TraceIncomingCall.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/UnifiedSearch.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/UpdatePBXDetails.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/index.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/language/en_us.lang.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/language/zh_cn.lang.php create mode 100644 oss/vtiger/trunk/modules/PBXManager/schema.xml create mode 100644 oss/vtiger/trunk/modules/PBXManager/utils/AsteriskClass.php diff --git a/oss/vtiger/trunk/modules/PBXManager/AsteriskUtils.php b/oss/vtiger/trunk/modules/PBXManager/AsteriskUtils.php new file mode 100644 index 00000000..71425f83 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/AsteriskUtils.php @@ -0,0 +1,221 @@ +pquery($sql, array()); + if($adb->num_rows($result)>0){ + $data = array(); + $data['server'] = $adb->query_result($result,0,"server"); + $data['port'] = $adb->query_result($result,0,"port"); + $data['username'] = $adb->query_result($result,0,"username"); + $data['password'] = $adb->query_result($result,0,"password"); + $data['version'] = $adb->query_result($result,0,"version"); + return $data; + }else{ + $log->debug("Asterisk server settings not specified.\n". + "Change the configuration from vtiger-> Settings-> Softphone Settings\n"); + return false; + } +} + + +/** + * this function will authorize the first user from the database that it finds + * this is required as some user must be authenticated into the asterisk server to + * receive the events that are being generated by asterisk + * + * @param string $username - the asterisk username + * @param string $password - the asterisk password + * @param object $asterisk - asterisk type object + */ +function authorizeUser($username, $password, $asterisk){ + echo "Trying to login to asterisk\n"; + + if(!empty($username) && !empty($password)){ + $asterisk->setUserInfo($username, $password); + if( !$asterisk->authenticateUser() ) { + echo "Cannot login to asterisk using\n + User: $username\n + Password: $password\n + Please check your configuration details.\n"; + exit(0); + }else{ + echo "Logged in successfully to asterisk server\n\n"; + return true; + } + }else{ + return false; + } +} + +/** + * this function logs in a user so that he can make calls + * @param string $username - the asterisk username + * @param string $password - the asterisk password + * @param object $asterisk - asterisk type object + */ +function loginUser($username, $password, $asterisk){ + if(!empty($username) && !empty($password)){ + $asterisk->setUserInfo($username, $password); + if( !$asterisk->authenticateUser() ) { + echo "Cannot login to asterisk using\n + User: $username\n + Password: $password\n + Please check your configuration details.\n"; + exit(0); + }else{ + return true; + } + }else{ + echo "Missing username and/or password"; + return false; + } +} + + +/** + * this function returns the channel for the current call + * @param object $asterisk - the asterisk object + * @return :: on success - string $value - the channel for the current call + * on failure - false + */ +function getChannel($asterisk){ + $res = array(); + while(true){ + $res = $asterisk->getAsteriskResponse(false); + if(empty($res)){ + continue; + } + foreach($res as $action => $value) { + if($action == 'Channel'){ + return $value; + } + } + } + return false; +} + +/** + * this function accepts a asterisk extension and returnsthe userid for which it is associated to + * in case of multiple users having the extension, it returns the first find + * @param string $extension - the asterisk extension for the user + * @param object $adb - the peardatabase object + * @return integer $userid - the user id with the extension + */ +function getUserFromExtension($extension, $adb){ + $userid = false; + $sql = "select userid from vtiger_asteriskextensions where asterisk_extension=?"; + $result = $adb->pquery($sql, array($extension)); + if($adb->num_rows($result) > 0){ + $userid = $adb->query_result($result, 0, "userid"); + } + return $userid; +} + + +/** + * this function adds the call information to the actvity history + * @param string $callerName - the caller name + * @param string $callerNumber - the callers' number + * @param string $callerType - the caller type (SIP/PSTN...) + * @param object $adb - the peardatabase object + * @param object $current_user - the current user + * @return string $status - on success - string success + * on failure - string failure + */ +function asterisk_addToActivityHistory($callerName, $callerNumber, $callerType, $adb, $userid, $relcrmid, $callerInfo=false){ + global $log, $current_user; + + // Reset date format for a while + $old_userdate_format = $current_user->date_format; + $current_user->date_format = 'yyyy-mm-dd'; + + require_once 'modules/Calendar/Activity.php'; + $focus = new Activity(); + $focus->column_fields['subject'] = "Incoming call from $callerName ($callerNumber)"; + $focus->column_fields['activitytype'] = "Call"; + $focus->column_fields['date_start'] = date('Y-m-d'); + $focus->column_fields['due_date'] = date('Y-m-d'); + $focus->column_fields['time_start'] = date('H:i'); + $focus->column_fields['time_end'] = date('H:i'); + $focus->column_fields['eventstatus'] = "Held"; + $focus->column_fields['assigned_user_id'] = $userid; + $focus->save('Calendar'); + $focus->setActivityReminder('off'); + + // Restore dateformat + $current_user->date_format = $old_userdate_format; + + if(empty($relcrmid)) { + if(empty($callerInfo)) { + $callerInfo = getCallerInfo($callerNumber); + } + } else { + $callerInfo = array(); + $callerInfo['module'] = getSalesEntityType($relcrmid); + $callerInfo['id'] = $relcrmid; + } + + if($callerInfo != false){ + $tablename = array('Contacts'=>'vtiger_cntactivityrel', 'Accounts'=>'vtiger_seactivityrel', 'Leads'=>'vtiger_seactivityrel'); + $sql = "insert into ".$tablename[$callerInfo['module']]." values (?,?)"; + $params = array($callerInfo['id'], $focus->id); + $adb->pquery($sql, $params); + } + + return $focus->id; +} + +/* Function to add an outgoing call to the History + * Params Object $current_user - the current user + * string $extension - the users extension number + * int $record - the activity will be attached to this record + * object $adb - the peardatabase object + */ +function addOutgoingcallHistory($current_user,$extension, $record ,$adb){ + global $log; + require_once 'modules/Calendar/Activity.php'; + $focus = new Activity(); + $focus->column_fields['subject'] = "Outgoing call from $current_user->user_name ($extension)"; + $focus->column_fields['activitytype'] = "Call"; + $focus->column_fields['date_start'] = date('Y-m-d'); + $focus->column_fields['due_date'] = date('Y-m-d'); + $focus->column_fields['time_start'] = date('H:i'); + $focus->column_fields['time_end'] = date('H:i'); + $focus->column_fields['eventstatus'] = "Held"; + $focus->column_fields['assigned_user_id'] = $current_user->id; + $focus->save('Calendar'); + $focus->setActivityReminder('off'); + $setype = $adb->pquery("SELECT setype FROM vtiger_crmentity WHERE crmid = ?",array($record)); + $rows = $adb->num_rows($setype); + + if($rows > 0){ + $module = $adb->query_result($setype,0,'setype'); + $tablename = array('Contacts'=>'vtiger_cntactivityrel', 'Accounts'=>'vtiger_seactivityrel', 'Leads'=>'vtiger_seactivityrel'); + $sql = "insert into ".$tablename[$module]." values (?,?)"; + $params = array($record, $focus->id); + $adb->pquery($sql, $params); + $status = "success"; + }else{ + $status = "failure"; + } + + return $status; +} +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/CallRelatedList.php b/oss/vtiger/trunk/modules/PBXManager/CallRelatedList.php new file mode 100644 index 00000000..367602e7 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/CallRelatedList.php @@ -0,0 +1,11 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/CustomView.php b/oss/vtiger/trunk/modules/PBXManager/CustomView.php new file mode 100644 index 00000000..d38a86e5 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/CustomView.php @@ -0,0 +1,13 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/Delete.php b/oss/vtiger/trunk/modules/PBXManager/Delete.php new file mode 100644 index 00000000..54247456 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/Delete.php @@ -0,0 +1,26 @@ + \ No newline at end of file diff --git a/oss/vtiger/trunk/modules/PBXManager/DetailView.php b/oss/vtiger/trunk/modules/PBXManager/DetailView.php new file mode 100644 index 00000000..367602e7 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/DetailView.php @@ -0,0 +1,11 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/DetailViewAjax.php b/oss/vtiger/trunk/modules/PBXManager/DetailViewAjax.php new file mode 100644 index 00000000..367602e7 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/DetailViewAjax.php @@ -0,0 +1,11 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/EditView.php b/oss/vtiger/trunk/modules/PBXManager/EditView.php new file mode 100644 index 00000000..e8ad1013 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/EditView.php @@ -0,0 +1,35 @@ +assign('APP', $app_strings); +$smarty->assign('MOD', $mod_strings); +$smarty->assign('MODULE', $currentModule); + +$smarty->assign('SINGLE_MOD', "CallHistory"); +$smarty->assign('CATEGORY', $category); +$smarty->assign("THEME", $theme); +$smarty->assign('IMAGE_PATH', "themes/$theme/images/"); + +$smarty->assign('CHECK', array()); + +$smarty->display(vtlib_getModuleTemplate('PBXManager', 'EditView.tpl')); + +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/ExportRecords.php b/oss/vtiger/trunk/modules/PBXManager/ExportRecords.php new file mode 100644 index 00000000..835622a1 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/ExportRecords.php @@ -0,0 +1,13 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/Import.php b/oss/vtiger/trunk/modules/PBXManager/Import.php new file mode 100644 index 00000000..eb64d9ea --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/Import.php @@ -0,0 +1,13 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/ListView.php b/oss/vtiger/trunk/modules/PBXManager/ListView.php new file mode 100644 index 00000000..9b5e84fe --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/ListView.php @@ -0,0 +1,180 @@ +"yes",); +$tool_buttons["moduleSettings"] = isModuleSettingPermitted($currentModule); +$list_buttons = Array(); +if(isPermitted($currentModule,'Delete','') == 'yes') $list_buttons['del'] = $app_strings[LBL_MASS_DELETE]; + +$focus = new $currentModule(); +$sorder = $focus->getSortOrder(); +$order_by = $focus->getOrderBy(); + +$_SESSION[$currentModule."_Order_By"] = $order_by; +$_SESSION[$currentModule."_Sort_Order"]=$sorder; + +$smarty = new vtigerCRM_Smarty(); + +// Identify this module as custom module. +$smarty->assign('CUSTOM_MODULE', true); + +$smarty->assign('MOD', $mod_strings); +$smarty->assign('APP', $app_strings); +$smarty->assign("THEME", $theme); +$smarty->assign('MODULE', $currentModule); +$smarty->assign('SINGLE_MOD', "CallHistory"); +$smarty->assign('CATEGORY', $category); +$smarty->assign('BUTTONS', $list_buttons); +$smarty->assign('CHECK', $tool_buttons); +$smarty->assign('IMAGE_PATH', "themes/$theme/images/"); + +$smarty->assign('CHANGE_OWNER', getUserslist()); +$smarty->assign('CHANGE_GROUP_OWNER', getGroupslist()); + +// Custom View +$customView = new CustomView($currentModule); +$viewid = $customView->getViewId($currentModule); +$customview_html = $customView->getCustomViewCombo($viewid); +$viewinfo = $customView->getCustomViewByCvid($viewid); + +// Feature available from 5.1 +if(method_exists($customView, 'isPermittedChangeStatus')) { + // Approving or Denying status-public by the admin in CustomView + $statusdetails = $customView->isPermittedChangeStatus($viewinfo['status']); + + // To check if a user is able to edit/delete a CustomView + $edit_permit = $customView->isPermittedCustomView($viewid,'EditView',$currentModule); + $delete_permit = $customView->isPermittedCustomView($viewid,'Delete',$currentModule); + + $smarty->assign("CUSTOMVIEW_PERMISSION",$statusdetails); + $smarty->assign("CV_EDIT_PERMIT",$edit_permit); + $smarty->assign("CV_DELETE_PERMIT",$delete_permit); +} +// END + +$smarty->assign("VIEWID", $viewid); + +if($viewinfo['viewname'] == 'All') $smarty->assign('ALL', 'All'); + +global $current_user; +$queryGenerator = new QueryGenerator($currentModule, $current_user); +if ($viewid != "0") { + $queryGenerator->initForCustomViewById($viewid); +} else { + $queryGenerator->initForDefaultCustomView(); +} + +// Enabling Module Search +$url_string = ''; +if($_REQUEST['query'] == 'true') { + $queryGenerator->addUserSearchConditions($_REQUEST); + $ustring = getSearchURL($_REQUEST); + $url_string .= "&query=true$ustring"; + $smarty->assign('SEARCH_URL', $url_string); +} + +$list_query = $queryGenerator->getQuery(); +// Sorting +if(!empty($order_by)) { + if($order_by == 'smownerid') $list_query .= ' ORDER BY user_name '.$sorder; + else { + $tablename = getTableNameForField($currentModule, $order_by); + $tablename = ($tablename != '')? ($tablename . '.') : ''; + $list_query .= ' ORDER BY ' . $tablename . $order_by . ' ' . $sorder; + } +} + +if(PerformancePrefs::getBoolean('LISTVIEW_COMPUTE_PAGE_COUNT', false) === true){ + $count_result = $adb->query( mkCountQuery( $list_query)); + $noofrows = $adb->query_result($count_result,0,"count"); +}else{ + $noofrows = null; +} + +$queryMode = (isset($_REQUEST['query']) && $_REQUEST['query'] == 'true'); +$start = ListViewSession::getRequestCurrentPage($currentModule, $list_query, $viewid, $queryMode); + +$navigation_array = VT_getSimpleNavigationValues($start,$list_max_entries_per_page,$noofrows); + +$limit_start_rec = ($start-1) * $list_max_entries_per_page; + +if( $adb->dbType == "pgsql") + $list_result = $adb->pquery($list_query. " OFFSET $limit_start_rec LIMIT $list_max_entries_per_page", array()); +else + $list_result = $adb->pquery($list_query. " LIMIT $limit_start_rec, $list_max_entries_per_page", array()); + +$recordListRangeMsg = getRecordRangeMessage($list_result, $limit_start_rec,$noofrows); +$smarty->assign('recordListRange',$recordListRangeMsg); + +$smarty->assign("CUSTOMVIEW_OPTION",$customview_html); + +// Navigation +$start = $_SESSION['lvs'][$currentModule]['start']; +$navigationOutput = getTableHeaderSimpleNavigation($navigation_array, $url_string, $currentModule, 'index', $viewid); +$smarty->assign("NAVIGATION", $navigationOutput); + +$controller = new ListViewController($adb, $current_user, $queryGenerator); +$listview_header = $controller->getListViewHeader($focus,$currentModule,$url_string,$sorder, + $order_by); +$listview_entries = $controller->getListViewEntries($focus,$currentModule,$list_result, + $navigation_array); +$listview_header_search = $controller->getBasicSearchFieldInfoList(); + +// Convert field value to DetailView Link +if(isset($focus->detailview_links) && count($focus->detailview_links)) { + foreach($listview_entries as $listview_recid=>$listview_row) { + foreach($listview_row as $listview_key=>$listview_val) { + $listview_key_header = $listview_header[$listview_key]; + preg_match('/(<[^>]+>)([^<]+)(<[^>]+>)/', $listview_key_header, $matches); + $linktext = array_search(trim($matches[2], '  \t\r\n'), $mod_strings); + if(in_array($linktext, $focus->detailview_links)) { + $listview_row[$listview_key] = + "".$listview_val.""; + } + } + $listview_entries[$listview_recid] = $listview_row; + } +} +$smarty->assign('LISTHEADER', $listview_header); +$smarty->assign('LISTENTITY', $listview_entries); +$smarty->assign('SEARCHLISTHEADER',$listview_header_search); + +// Module Search +$alphabetical = AlphabeticalSearch($currentModule,'index',$focus->def_basicsearch_col,'true','basic','','','','',$viewid); +$fieldnames = $controller->getAdvancedSearchOptionString(); +$criteria = getcriteria_options(); +$smarty->assign("ALPHABETICAL", $alphabetical); +$smarty->assign("FIELDNAMES", $fieldnames); +$smarty->assign("CRITERIA", $criteria); + +// Gather the custom link information to display +include_once('vtlib/Vtiger/Link.php'); +$customlink_params = Array('MODULE'=>$currentModule, 'ACTION'=>vtlib_purify($_REQUEST['action']), 'CATEGORY'=> $category); +$smarty->assign('CUSTOM_LINKS', Vtiger_Link::getAllByType(getTabid($currentModule), Array('LISTVIEWBASIC','LISTVIEW'), $customlink_params)); +// END + +if(isset($_REQUEST['ajax']) && $_REQUEST['ajax'] != '') + $smarty->display("ListViewEntries.tpl"); +else + $smarty->display('ListView.tpl'); + +?> \ No newline at end of file diff --git a/oss/vtiger/trunk/modules/PBXManager/ListViewPagging.php b/oss/vtiger/trunk/modules/PBXManager/ListViewPagging.php new file mode 100644 index 00000000..333f1768 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/ListViewPagging.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/oss/vtiger/trunk/modules/PBXManager/PBXManager.js b/oss/vtiger/trunk/modules/PBXManager/PBXManager.js new file mode 100644 index 00000000..be76e7a5 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/PBXManager.js @@ -0,0 +1,9 @@ +/********************************************************************************* +** The contents of this file are subject to the vtiger CRM Public License Version 1.0 +* ("License"); You may not use this file except in compliance with the License +* The Original Code is: vtiger CRM Open Source +* The Initial Developer of the Original Code is vtiger. +* Portions created by vtiger are Copyright (C) vtiger. +* All Rights Reserved. +********************************************************************************/ + diff --git a/oss/vtiger/trunk/modules/PBXManager/PBXManager.php b/oss/vtiger/trunk/modules/PBXManager/PBXManager.php new file mode 100644 index 00000000..3adabe98 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/PBXManager.php @@ -0,0 +1,347 @@ + 'crmid', + 'vtiger_pbxmanager' => 'pbxmanagerid', + ); + + // Mandatory for Listing + var $list_fields = Array ( + // Field Label=> Array(tablename, columnname) + 'Call To'=> Array('pbxmanager', 'callto'), + 'Call From'=>Array('pbxmanager', 'callfrom'), + ); + var $list_fields_name = Array( + // Field Label=>columnname + 'Call To'=> 'callto', + 'Call From' => 'callfrom' + ); + var $sortby_fields = Array('callto', 'callfrom', 'callid', 'timeofcall', 'status'); + // Should contain field labels + var $detailview_links = Array(); + + // For alphabetical search + var $def_basicsearch_col = 'callid'; + + // Column value to use on detail view record text display. + var $def_detailview_recname = ''; + + // Required information for enabling Import feature + var $required_fields = Array(); + + // Callback function list during Importing + var $special_functions = array(); + + var $default_order_by = 'timeofcall'; + var $default_sort_order='DESC'; + + function PBXManager() { + global $log; + $this->column_fields = getColumnFields('PBXManager'); + $this->db = PearDatabase::getInstance(); + $this->log = $log; + } + + function getSortOrder() { + global $currentModule; + + $sortorder = $this->default_sort_order; + if($_REQUEST['sorder']) $sortorder = $this->db->sql_escape_string($_REQUEST['sorder']); + else if($_SESSION[$currentModule.'_Sort_Order']) + $sortorder = $_SESSION[$currentModule.'_Sort_Order']; + + return $sortorder; + } + + function getOrderBy() { + global $currentModule; + + $use_default_order_by = ''; + if(PerformancePrefs::getBoolean('LISTVIEW_DEFAULT_SORTING', true)) { + $use_default_order_by = $this->default_order_by; + } + + $orderby = $use_default_order_by; + if($_REQUEST['order_by']) $orderby = $this->db->sql_escape_string($_REQUEST['order_by']); + else if($_SESSION[$currentModule.'_Order_By']) + $orderby = $_SESSION[$currentModule.'_Order_By']; + return $orderby; + } + + function save_module($module) { + } + + /** + * Get list view query. + */ + function getListQuery($module) { + $query = "SELECT $this->table_name.*, vtiger_crmentity.*"; + $query .= " FROM $this->table_name"; + + $query .= " INNER JOIN vtiger_crmentity + ON vtiger_crmentity.crmid = $this->table_name.$this->table_index + LEFT JOIN vtiger_groups + ON vtiger_groups.groupid = vtiger_crmentity.smownerid"; + + // Consider custom table join as well. + if(!empty($this->customFieldTable)) { + $query .= " INNER JOIN ".$this->customFieldTable[0]." ON ".$this->customFieldTable[0].'.'.$this->customFieldTable[1] . + " = $this->table_name.$this->table_index"; + } + $query .= " LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid "; + + $query .= " WHERE vtiger_crmentity.deleted = 0"; + $query .= $this->getListViewSecurityParameter($module); + return $query; + } + + /** + * Apply security restriction (sharing privilege) query part for List view. + */ + function getListViewSecurityParameter($module) { + global $current_user; + require('user_privileges/user_privileges_'.$current_user->id.'.php'); + require('user_privileges/sharing_privileges_'.$current_user->id.'.php'); + + $sec_query = ''; + $tabid = getTabid($module); + + if($is_admin==false && $profileGlobalPermission[1] == 1 && $profileGlobalPermission[2] == 1 + && $defaultOrgSharingPermission[$tabid] == 3) { + + $sec_query .= " AND (vtiger_crmentity.smownerid in($current_user->id) OR vtiger_crmentity.smownerid IN + ( + SELECT vtiger_user2role.userid FROM vtiger_user2role + INNER JOIN vtiger_users ON vtiger_users.id=vtiger_user2role.userid + INNER JOIN vtiger_role ON vtiger_role.roleid=vtiger_user2role.roleid + WHERE vtiger_role.parentrole LIKE '".$current_user_parent_role_seq."::%' + ) + OR vtiger_crmentity.smownerid IN + ( + SELECT shareduserid FROM vtiger_tmp_read_user_sharing_per + WHERE userid=".$current_user->id." AND tabid=".$tabid." + ) + OR + ( + vtiger_crmentity.smownerid in (0)"; + + if(!empty($this->groupTable)) { + $sec_query .= " AND + ("; + + // Build the query based on the group association of current user. + if(sizeof($current_user_groups) > 0) { + $sec_query .= " vtiger_groups.groupid IN (". implode(",", $current_user_groups) .") OR "; + } + $sec_query .= " vtiger_groups.groupid IN + ( + SELECT vtiger_tmp_read_group_sharing_per.sharedgroupid + FROM vtiger_tmp_read_group_sharing_per + WHERE userid=".$current_user->id." and tabid=".$tabid." + )"; + $sec_query .= ") "; + } + + $sec_query .= ") + )"; + } + return $sec_query; + } + + /** + * Create query to export the records. + */ + function create_export_query($where) + { + global $current_user; + $thismodule = $_REQUEST['module']; + + include("include/utils/ExportUtils.php"); + + //To get the Permitted fields query and the permitted fields list + $sql = getPermittedFieldsQuery($thismodule, "detail_view"); + + $fields_list = getFieldsListFromQuery($sql); + + $query = "SELECT $fields_list, 'vtiger_groups_groupname as Assigned To Group', + CASE WHEN (vtiger_users.user_name NOT LIKE '') THEN vtiger_users.user_name ELSE vtiger_groups.groupname END + AS user_name FROM vtiger_crmentity INNER JOIN $this->table_name ON vtiger_crmentity.crmid=$this->table_name.$this->table_index"; + + if(!empty($this->customFieldTable)) { + $query .= " INNER JOIN ".$this->customFieldTable[0]." ON ".$this->customFieldTable[0].'.'.$this->customFieldTable[1] . + " = $this->table_name.$this->table_index"; + } + + $query .= + //"LEFT JOIN " . $this->groupTable[0] . " ON " . $this->groupTable[0].'.'.$this->groupTable[1] . " = $this->table_name.$this->table_index + "LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid"; + $query .= " LEFT JOIN vtiger_users ON vtiger_crmentity.smownerid = vtiger_users.id and vtiger_users.status='Active'"; + + $where_auto = " vtiger_crmentity.deleted=0"; + + if($where != '') $query .= " WHERE ($where) AND $where_auto"; + else $query .= " WHERE $where_auto"; + + require('user_privileges/user_privileges_'.$current_user->id.'.php'); + require('user_privileges/sharing_privileges_'.$current_user->id.'.php'); + + // Security Check for Field Access + if($is_admin==false && $profileGlobalPermission[1] == 1 && $profileGlobalPermission[2] == 1 && $defaultOrgSharingPermission[7] == 3) + { + //Added security check to get the permitted records only + $query = $query." ".getListViewSecurityParameter($thismodule); + } + return $query; + } + + /** + * Initialize this instance for importing. + */ + function initImport($module) { + $this->db = PearDatabase::getInstance(); + $this->initImportableFields($module); + } + + /** + * Create list query to be shown at the last step of the import. + * Called From: modules/Import/UserLastImport.php + */ + function create_import_query($module) { + global $current_user; + $query = "SELECT vtiger_crmentity.crmid, vtiger_users.user_name, $this->table_name.* FROM $this->table_name + INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = $this->table_name.$this->table_index + LEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=vtiger_crmentity.crmid + LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid + WHERE vtiger_users_last_import.assigned_user_id='$current_user->id' + AND vtiger_users_last_import.bean_type='$module' + AND vtiger_users_last_import.deleted=0 + AND vtiger_users.status = 'Active'"; + return $query; + } + + /** + * Delete the last imported records. + */ + function undo_import($module, $user_id) { + global $adb; + $count = 0; + $query1 = "select bean_id from vtiger_users_last_import where assigned_user_id=? AND bean_type='$module' AND deleted=0"; + $result1 = $adb->pquery($query1, array($user_id)) or die("Error getting last import for undo: ".mysql_error()); + while ( $row1 = $adb->fetchByAssoc($result1)) + { + $query2 = "update vtiger_crmentity set deleted=1 where crmid=?"; + $result2 = $adb->pquery($query2, array($row1['bean_id'])) or die("Error undoing last import: ".mysql_error()); + $count++; + } + return $count; + } + + /** + * Function which will set the assigned user id for import record. + */ + function set_import_assigned_user() + { + global $current_user, $adb; + $record_user = $this->column_fields["assigned_user_id"]; + + if($record_user != $current_user->id){ + $sqlresult = $adb->pquery("select id from vtiger_users where id = ?", array($ass_user)); + if($this->db->num_rows($sqlresult)!= 1) { + $this->column_fields["assigned_user_id"] = $current_user->id; + } else { + $row = $adb->fetchByAssoc($sqlresult, -1, false); + if (!empty($row['id']) && $row['id'] != -1) { + $this->column_fields["assigned_user_id"] = $row['id']; + } else { + $this->column_fields["assigned_user_id"] = $current_user->id; + } + } + } + } + + /** + * Handle getting related list information. + * NOTE: This function has been added to CRMEntity (base class). + * You can override the behavior by re-defining it here. + */ + //function get_related_list($id, $cur_tab_id, $rel_tab_id, $actions=false) { } + + /** + * Handle saving related module information. + * NOTE: This function has been added to CRMEntity (base class). + * You can override the behavior by re-defining it here. + */ + // function save_related_module($module, $crmid, $with_module, $with_crmid) { } + + /** + * Invoked when special actions are performed on the module. + * @param String Module name + * @param String Event Type + */ + function vtlib_handler($moduleName, $eventType) { + require_once('include/utils/utils.php'); + global $adb; + $tabid = getTabid("Users"); + if($eventType == 'module.postinstall') { + // Add a block and 2 fields for Users module + $blockid = $adb->getUniqueID('vtiger_blocks'); + $adb->query("insert into vtiger_blocks(blockid,tabid,blocklabel,sequence,show_title,visible,create_view,edit_view,detail_view,display_status)" . + " values ($blockid,$tabid,'Asterisk Configuration',6,0,0,0,0,0,1)"); + + $adb->query("insert into vtiger_field(tabid,fieldid,columnname,tablename,generatedtype,uitype,fieldname,fieldlabel,readonly," . + " presence,selected,maximumlength,sequence,block,displaytype,typeofdata,quickcreate,quickcreatesequence,info_type) " . + " values ($tabid,".$adb->getUniqueID('vtiger_field').",'asterisk_extension','vtiger_asteriskextensions',1,1,'asterisk_extension'," . + " 'Asterisk Extension',1,0,0,30,1,$blockid,1,'V~O',1,NULL,'BAS')"); + + $adb->query("insert into vtiger_field(tabid,fieldid,columnname,tablename,generatedtype,uitype,fieldname,fieldlabel,readonly," . + " presence,selected,maximumlength,sequence,block,displaytype,typeofdata,quickcreate,quickcreatesequence,info_type) " . + " values ($tabid,".$adb->getUniqueID('vtiger_field').",'use_asterisk','vtiger_asteriskextensions',1,56,'use_asterisk'," . + "' Receive Incoming Calls',1,0,0,30,2,$blockid,1,'C~O',1,NULL,'BAS')"); + + // Mark the module as Standard module + $adb->pquery('UPDATE vtiger_tab SET customized=0 WHERE name=?', array($moduleName)); + + } else if($eventType == 'module.disabled') { + // TODO Handle actions when this module is disabled. + } else if($eventType == 'module.enabled') { + // TODO Handle actions when this module is enabled. + } else if($eventType == 'module.preuninstall') { + // TODO Handle actions when this module is about to be deleted. + } else if($eventType == 'module.preupdate') { + // TODO Handle actions before this module is updated. + } else if($eventType == 'module.postupdate') { + // TODO Handle actions after this module is updated. + } + } +} + +?> \ No newline at end of file diff --git a/oss/vtiger/trunk/modules/PBXManager/PBXManagerAjax.php b/oss/vtiger/trunk/modules/PBXManager/PBXManagerAjax.php new file mode 100644 index 00000000..29d30aeb --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/PBXManagerAjax.php @@ -0,0 +1,11 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/Save.php b/oss/vtiger/trunk/modules/PBXManager/Save.php new file mode 100644 index 00000000..09e4deb3 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/Save.php @@ -0,0 +1,43 @@ +mode = $mode; +if($record)$focus->id = $record; + +setObjectValuesFromRequest($focus); + +$mode = $_REQUEST['mode']; +$record=$_REQUEST['record']; + +if($_REQUEST['assigntype'] == 'U') { + $focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_user_id']; +} elseif($_REQUEST['assigntype'] == 'T') { + $focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_group_id']; +} +$focus->save($currentModule); +$return_id = $focus->id; + +$parenttab = getParentTab(); +if($_REQUEST['return_module'] != '') $return_module = vtlib_purify($_REQUEST['return_module']); +else $return_module = $currentModule; + +if($_REQUEST['return_action'] != '') $return_action = vtlib_purify($_REQUEST['return_action']); +else $return_action = "DetailView"; + +if($_REQUEST['return_id'] != '') $return_id = vtlib_purify($_REQUEST['return_id']); + +header("Location: index.php?action=$return_action&module=$return_module&record=$return_id&parenttab=$parenttab&start=".vtlib_purify($_REQUEST['pagenumber'])); + +?> \ No newline at end of file diff --git a/oss/vtiger/trunk/modules/PBXManager/Settings.php b/oss/vtiger/trunk/modules/PBXManager/Settings.php new file mode 100644 index 00000000..670abab4 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/Settings.php @@ -0,0 +1,46 @@ +assign("MOD", return_module_language($current_language, 'Settings')); +$smarty->assign("CMOD", $mod_strings); +$smarty->assign("THEME", $theme); +$smarty->assign("APP", $app_strings); +$smarty->assign("IMAGE_PATH", $image_path); + +$result = $adb->pquery("select * from vtiger_asterisk", array ()); +if($adb->num_rows($result) > 0){ + $asterisk_server_ip = $adb->query_result($result, 0, 'server'); + $asterisk_port = $adb->query_result($result, 0, 'port'); + $asterisk_username = $adb->query_result($result, 0, 'username'); + $asterisk_password = $adb->query_result($result, 0, 'password'); + $asterisk_version = $adb->query_result($result, 0, 'version'); +} + +$smarty->assign("ASTERISK_SERVER_IP", $asterisk_server_ip); +$smarty->assign("ASTERISK_PORT", $asterisk_port); +$smarty->assign("ASTERISK_USERNAME", $asterisk_username); +$smarty->assign("ASTERISK_PASSWORD", $asterisk_password); +$smarty->assign("ASTERISK_VERSION", $asterisk_version); + +$smarty->display(vtlib_getModuleTemplate('PBXManager', 'Settings.tpl')); +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/StartCall.php b/oss/vtiger/trunk/modules/PBXManager/StartCall.php new file mode 100644 index 00000000..7f5ca899 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/StartCall.php @@ -0,0 +1,61 @@ +id; + $number = $_REQUEST['number']; + $record = $_REQUEST['recordid']; + $result = $adb->query("select * from vtiger_asteriskextensions where userid=".$current_user->id); + $extension = $adb->query_result($result, 0, "asterisk_extension"); + $data = getAsteriskInfo($adb); + $server = $data['server']; + $port = $data['port']; + $username = $data['username']; + $password = $data['password']; + $version = $data['version']; + $errno = $errstr = NULL; + $sock = fsockopen($server, $port, $errno, $errstr, 1); + stream_set_blocking($sock, false); + if( $sock === false ) { + echo "Socket cannot be created due to error: $errno: $errstr\n"; + $log->debug("Socket cannot be created due to error: $errno: $errstr\n"); + exit(0); + } + $asterisk = new Asterisk($sock, $server, $port); + + loginUser($username, $password, $asterisk); + + $asterisk->transfer($extension,$number); + + $callerModule = getSalesEntityType($record); + $entityNames = getEntityName($callerModule, array($record)); + $callerName = $entityNames[$record]; + $callerInfo = array('id'=>$record, 'module'=>$callerModule, 'name'=>$callerName); + + //adds to pbx manager + addToCallHistory($extension, $extension, $number, "outgoing", $adb, $callerInfo); + + // add to the records activity history + addOutgoingcallHistory($current_user ,$extension,$record ,$adb); + +} +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/TagCloud.php b/oss/vtiger/trunk/modules/PBXManager/TagCloud.php new file mode 100644 index 00000000..f44416be --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/TagCloud.php @@ -0,0 +1,11 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/TraceIncomingCall.php b/oss/vtiger/trunk/modules/PBXManager/TraceIncomingCall.php new file mode 100644 index 00000000..c8fdbd18 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/TraceIncomingCall.php @@ -0,0 +1,127 @@ +column_fields)) { + $asterisk_extension = $current_user->column_fields['asterisk_extension']; + } else { + $sql = "select asterisk_extension from vtiger_asteriskextensions where userid = ?"; + $result = $adb->pquery($sql, array($current_user->id)); + $asterisk_extension = $adb->query_result($result, 0, "asterisk_extension"); + } + + $query = "select * from vtiger_asteriskincomingcalls where to_number = ?"; + $result = $adb->pquery($query, array($asterisk_extension)); + + if($adb->num_rows($result)>0){ + $flag = $adb->query_result($result,0,"flag"); + $oldTime = $adb->query_result($result,0,"timer"); + $callerNumber = $adb->query_result($result,0,"from_number"); + $callerName = $adb->query_result($result,0,"from_name"); + $callerType = $adb->query_result($result,0,"callertype"); + $refuid = $adb->query_result($result, 0, "refuid"); + + if(!empty($callerNumber)){ + $tracedCallerInfo = getTraceIncomingCallerInfo($callerNumber); + } + + $callerLinks = $tracedCallerInfo['callerLinks']; + $firstCallerInfo = false; + if(!empty($tracedCallerInfo['callerInfos'])) { + $firstCallerInfo = $tracedCallerInfo['callerInfos']; + } + + $newTime = time(); + if(($newTime-$oldTime)>=3 && $flag == 1){ + $adb->pquery("delete from vtiger_asteriskincomingcalls where to_number = ?", array($asterisk_extension)); + }else{ + if($flag==0){ + $flag=1; + + // Trying to get the Related CRM ID for the Event (if already desired by popup click) + $relcrmid = false; + if(!empty($refuid)) { + $refuidres = $adb->pquery('SELECT relcrmid FROM vtiger_asteriskincomingevents WHERE uid=?',array($refuid)); + if($adb->num_rows($refuidres)) $relcrmid = $adb->query_result($refuidres, 0, 'relcrmid'); + } + $adb->pquery("update vtiger_asteriskincomingcalls set flag = ? where to_number = ?", array($flag, $asterisk_extension)); + $activityid = asterisk_addToActivityHistory($callerName, $callerNumber, $callerType, $adb, $current_user->id, $relcrmid, $firstCallerInfo); + + } + //prepare the div for incoming calls + $status = " + + + +
".$app_strings['LBL_INCOMING_CALL']."
+ + + +
".$app_strings['LBL_CALLER_INFORMATION']." +
".$app_strings['LBL_CALLER_NUMBER']." $callerNumber +
".$app_strings['LBL_CALLER_NAME']." $callerName +
".$app_strings['LBL_INFORMATION_VTIGER']." +
$callerLinks +
"; + } + }else{ + $status = "failure"; + } + return $status; +} + +//functions for asterisk integration start +/** + * this function returns the caller name based on the phone number that is passed to it + * @param $from - the number which is calling + * returns caller information in name(type) format :: for e.g. Mary Smith (Contact) + * if no information is present in database, it returns :: Unknown Caller (Unknown) + */ +function getTraceIncomingCallerInfo($from) { + global $adb; + // Grab all possible caller informations (lookup for number as well stripped number) + $callerInfos = getCallerInfo($from); + $callerLinks = ''; + + if($callerInfos !== false){ + $callerName = decode_html($callerInfos['name']); + $module = $callerInfos['module']; + $callerModule = " [$module]"; + $callerID = $callerInfos['id']; + $callerLinks = $callerLinks."$callerName$callerModule
"; + }else{ + $callerLinks = $callerLinks."
+ ".getTranslatedString('LBL_CREATE_LEAD')."
+ ".getTranslatedString('LBL_CREATE_CONTACT')."
+ ".getTranslatedString('LBL_CREATE_ACCOUNT').""; + } + return array( + 'callerInfos' => $callerInfos, + 'callerLinks' => $callerLinks + ); +} + +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/UnifiedSearch.php b/oss/vtiger/trunk/modules/PBXManager/UnifiedSearch.php new file mode 100644 index 00000000..9a37320c --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/UnifiedSearch.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/oss/vtiger/trunk/modules/PBXManager/UpdatePBXDetails.php b/oss/vtiger/trunk/modules/PBXManager/UpdatePBXDetails.php new file mode 100644 index 00000000..94459c31 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/UpdatePBXDetails.php @@ -0,0 +1,32 @@ +query($sql); //delete older records (if any) + + $sql = "insert into vtiger_asterisk (server, port, username, password, version) values (?,?,?,?,?)"; + $params = array($server,$port, $username, $password, $version); + $adb->pquery($sql, $params); + } +} +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/index.php b/oss/vtiger/trunk/modules/PBXManager/index.php new file mode 100644 index 00000000..b4be9fc0 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/index.php @@ -0,0 +1,14 @@ + diff --git a/oss/vtiger/trunk/modules/PBXManager/language/en_us.lang.php b/oss/vtiger/trunk/modules/PBXManager/language/en_us.lang.php new file mode 100644 index 00000000..91cf4647 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/language/en_us.lang.php @@ -0,0 +1,20 @@ + 'Asterisk', + 'LBL_ASTERISK_INFORMATION' => 'ASTERISK Information', + + 'Call From'=>'Call From', + 'Call To'=>'Call To', + 'Time Of Call'=>'Time Of Call', +); + +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/language/zh_cn.lang.php b/oss/vtiger/trunk/modules/PBXManager/language/zh_cn.lang.php new file mode 100644 index 00000000..b0931ef3 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/language/zh_cn.lang.php @@ -0,0 +1,33 @@ + 'Asterisk', + 'LBL_ASTERISK_INFORMATION' => 'ASTERISK 信息', + + 'Call From'=>'来电', + 'Call To'=>'去电', + 'Time Of Call'=>'通话时间', +); + +?> diff --git a/oss/vtiger/trunk/modules/PBXManager/schema.xml b/oss/vtiger/trunk/modules/PBXManager/schema.xml new file mode 100644 index 00000000..12362ba5 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/schema.xml @@ -0,0 +1,15 @@ + + + + + vtiger_pbxmanager + +
+
+
diff --git a/oss/vtiger/trunk/modules/PBXManager/utils/AsteriskClass.php b/oss/vtiger/trunk/modules/PBXManager/utils/AsteriskClass.php new file mode 100644 index 00000000..831f9391 --- /dev/null +++ b/oss/vtiger/trunk/modules/PBXManager/utils/AsteriskClass.php @@ -0,0 +1,176 @@ +sock = $sock; + $this->address = $server; + $this->port = $port; + $this->db = PearDatabase::getInstance(); + $this->log = LoggerManager::getLogger('asterisk'); + $this->queue = array(); + } + + /** + * this function sets the username and password for the asterisk object + * @param string $userName - asterisk username + * @param string $password - password for the user + */ + function setUserInfo($userName, $password){ + $this->userName = $userName; + $this->password = $password; + } + + /** + * this function authenticates the user + * @return - true on success else false + */ + function authenticateUser(){ + $request = "Action: Login\r\n". + "Username: ".$this->userName."\r\n". + "Secret: ".$this->password. + "\r\n\r\n"; + if( !fwrite($this->sock, $request) ) { + echo "in function authenticateUser() Socket error.Cannot send.(function: fwrite)"; + $this->log->debug("in function authenticateUser() Socket error.Cannot send.(function: fwrite)"); + exit(0); + } + sleep(1); //wait for the response to come + $response = fread($this->sock, 4096); //read the response + + if(strstr($response,"Response") && (strstr($response,"Error") || strstr($response,"failed"))) { + print_r($response); + $this->log->debug($response); + return false; + } else { + return true; + } + } + + /** + * create a call between from and to + * @param string $from - the from number + * @param sring $to - the to number + * this function prepares the parameter $context and calls the createCall() function + */ + function transfer($from,$to){ + $this->log->debug("in function transfer($from, $to)"); + if(empty($from) || empty($to)) { + echo "Not sufficient parameters to create the call"; + $this->log->debug("Not sufficient parameters to create the call"); + return false; + } + + //the caller would always be a SIP phone in our case + if(!strstr($from,"SIP")){ + $from = "SIP/$from"; + } + if(strpos($to, ":")!==FALSE){ + $arr = explode(":", $to); + if(is_array($arr)){ + $typeCalled = $arr[0]; + $to = trim($arr[1]); + } + } + + switch($typeCalled){ + case "SIP": + $context = "default"; + break; + case "PSTN": + $context = "from-inside";//"outbound-dialing"; + break; + default: + $context = "default"; + } + $this->createCall($from, $to, $context); + } + + /** + * creates a call between $from and $to + * @param string $from -the number from which to call + * @param string $to - the number to which to call + * @param string $context - the context of the call (e.g. local-extensions for local calls) + */ + function createCall($from, $to, $context){ + $arr = explode("/", $from); + $request = "Action: Originate\r\n". + "Channel: $from\r\n". + "Exten: ".preg_replace('~[^0-9]~', "", $to)."\r\n". + "Context: $context\r\n". + "Priority: 1\r\n". + "Callerid: $arr[1]\r\n". + "Async: yes\r\n\r\n"; + if( !fwrite($this->sock, $request) ) { + echo "in function createcall() Socket error.Cannot send.(function: fwrite)"; + $this->log->debug("in function authenticateUser() Socket error.Cannot send.(function: fwrite)"); + exit(0); + } + } + + /** + * this is the destructor for the class :: it closes the opened socket + */ + function __destruct(){ + fclose($this->sock); + } + + /** + * this function reads the socket for asterisk events and + * creates a queue with the response arrays + * + * @param boolean $echoFlag - if set no echos are performed (added since some ajax requests might use the function) + * @return the event array present in the queue + * if no array is present it returns a null + */ + function getAsteriskResponse($echoFlag = true){ + if(sizeof($this->queue)==0){ + $this->strData.=fread($this->sock, 4096); + + if($echoFlag){ + echo $this->strData; + } + + $this->log->debug($this->strData); + $arr = explode("\r\n\r\n", $this->strData); + + for($i=0;$iqueue[] = $obj; + } + $this->strData = $arr[$i]; + } + return array_shift($this->queue); + } +} +?>