Files
crm.e5.pl/modules/Tasks.cp/Task.php

678 lines
22 KiB
PHP
Raw Permalink Normal View History

2024-04-27 09:23:34 +02:00
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
* SugarCRM is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004 - 2009 SugarCRM Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo. If the display of the logo is not reasonably feasible for
* technical reasons, the Appropriate Legal Notices must display the words
* "Powered by SugarCRM".
********************************************************************************/
/*********************************************************************************
* Description: TODO: To be written.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
* Contributor(s): ______________________________________..
********************************************************************************/
require_once('data/SugarBean.php');
require_once('include/utils.php');
// Task is used to store customer information.
class Task extends SugarBean {
var $field_name_map;
// Stored fields
var $id;
var $date_entered;
var $date_modified;
var $assigned_user_id;
var $modified_user_id;
var $created_by;
var $created_by_name;
var $modified_by_name;
var $description;
var $name;
var $status;
var $date_due_flag;
var $date_due;
var $time_due;
var $date_start_flag;
var $date_start;
var $time_start;
var $priority;
var $parent_type;
var $parent_id;
var $contact_id;
var $google_calendar_task;
var $parent_name;
var $contact_name;
var $contact_phone;
var $contact_email;
var $assigned_user_name;
var $default_task_name_values = array('Assemble catalogs', 'Make travel arrangements', 'Send a letter', 'Send contract', 'Send fax', 'Send a follow-up letter', 'Send literature', 'Send proposal', 'Send quote', 'Call to schedule meeting', 'Setup evaluation', 'Get demo feedback', 'Arrange introduction', 'Escalate support request', 'Close out support request', 'Ship product', 'Arrange reference call', 'Schedule training', 'Send local user group information', 'Add to mailing list');
var $table_name = "tasks";
var $object_name = "Task";
var $module_dir = 'Tasks';
var $importable = true;
// This is used to retrieve related fields from form posts.
var $additional_column_fields = Array('assigned_user_name', 'assigned_user_id', 'contact_name', 'contact_phone', 'contact_email', 'parent_name');
function Task() {
parent::SugarBean();
}
var $new_schema = true;
function get_summary_text()
{
return "$this->name";
}
function create_list_query($order_by, $where, $show_deleted = 0)
{
$custom_join = $this->custom_fields->getJOIN();
$query = "SELECT ";
$query .= "
$this->table_name.*,
contacts.first_name,
contacts.last_name,
contacts.assigned_user_id contact_name_owner,
users.user_name as assigned_user_name";
if($custom_join){
$query .= $custom_join['select'];
}
$query .= " FROM tasks ";
$query .= "LEFT JOIN contacts
ON tasks.contact_id=contacts.id";
$query .= " LEFT JOIN users
ON tasks.assigned_user_id=users.id ";
if($custom_join){
$query .= $custom_join['join'];
}
$where_auto = '1=1';
if($show_deleted == 0){
$where_auto = " tasks.deleted=0
AND ( contacts.deleted IS NULL OR contacts.deleted=0) ";
}else if($show_deleted == 1){
$where_auto = "$this->table_name.deleted=1";
}
//GROUP BY tasks.id";
if($where != "")
$query .= "where $where AND ".$where_auto;
else
$query .= "where ".$where_auto;
if($order_by != "")
$query .= " ORDER BY ". $this->process_order_by($order_by, null);
else
$query .= " ORDER BY tasks.name";
return $query;
}
function create_export_query(&$order_by, &$where)
{
$contact_required = ereg("contacts", $where);
$custom_join = $this->custom_fields->getJOIN(true, true);
if($contact_required)
{
$query = "SELECT tasks.*, contacts.first_name, contacts.last_name";
if($custom_join){
$query .= $custom_join['select'];
}
$query .= " FROM contacts, tasks ";
$where_auto = "tasks.contact_id = contacts.id AND tasks.deleted=0 AND contacts.deleted=0";
}
else
{
$query = 'SELECT tasks.*';
if($custom_join){
$query .= $custom_join['select'];
}
$query .= ' FROM tasks ';
$where_auto = "tasks.deleted=0";
}
if($custom_join){
$query .= $custom_join['join'];
}
if($where != "")
$query .= "where $where AND ".$where_auto;
else
$query .= "where ".$where_auto;
if($order_by != "")
$query .= " ORDER BY ". $this->process_order_by($order_by, null);
else
$query .= " ORDER BY tasks.name";
return $query;
}
function fill_in_additional_list_fields()
{
//global $app_strings, $focus;
//if(!empty($this->field_defs['created_by'])) {
//$this->created_by_name = get_assigned_user_name($this->field_defs['created_by']);
//print_r($this);
//echo $focus->created_by;
//$this->created_by_name = $this->field_defs['created_by'];
//}
}
function fill_in_additional_detail_fields()
{
parent::fill_in_additional_detail_fields();
global $app_strings;
if (isset($this->contact_id)) {
require_once("modules/Contacts/Contact.php");
$contact = new Contact();
$contact->retrieve($this->contact_id);
if($contact->id != "") {
$this->contact_name = $contact->full_name;
$this->contact_name_owner = $contact->assigned_user_id;
$this->contact_name_mod = 'Contacts';
$this->contact_phone = $contact->phone_work;
$this->contact_email = $contact->emailAddress->getPrimaryAddress($contact);
} else {
$this->contact_name_mod = '';
$this->contact_name_owner = '';
$this->contact_name='';
$this->contact_email = '';
$this->contact_id='';
}
}
$this->fill_in_additional_parent_fields();
}
function fill_in_additional_parent_fields()
{
$this->parent_name = '';
global $app_strings, $beanFiles, $beanList;
if ( ! isset($beanList[$this->parent_type]))
{
$this->parent_name = '';
return;
}
$beanType = $beanList[$this->parent_type];
require_once($beanFiles[$beanType]);
$parent = new $beanType();
if (is_subclass_of($parent, 'Person')) {
$query = "SELECT first_name, last_name, assigned_user_id parent_name_owner from $parent->table_name where id = '$this->parent_id'";
}
else {
$query = "SELECT name ";
if(isset($parent->field_defs['assigned_user_id'])){
$query .= " , assigned_user_id parent_name_owner ";
}else{
$query .= " , created_by parent_name_owner ";
}
$query .= " from $parent->table_name where id = '$this->parent_id'";
}
$result = $this->db->query($query,true," Error filling in additional detail fields: ");
// Get the id and the name.
$row = $this->db->fetchByAssoc($result);
if ($row && !empty($row['parent_name_owner'])){
$this->parent_name_owner = $row['parent_name_owner'];
$this->parent_name_mod = $this->parent_type;
}
if (is_subclass_of($parent, 'Person') and $row != null)
{
$this->parent_name = '';
if ($row['first_name'] != '') $this->parent_name .= stripslashes($row['first_name']). ' ';
if ($row['last_name'] != '') $this->parent_name .= stripslashes($row['last_name']);
}
elseif($row != null)
{
$this->parent_name = stripslashes($row['name']);
}
else {
$this->parent_name = '';
}
}
function get_list_view_data(){
global $action, $currentModule, $focus, $current_module_strings, $app_list_strings, $image_path, $timedate;
$today = $timedate->handle_offset(date($GLOBALS['timedate']->get_db_date_time_format(), time()), $timedate->dbDayFormat, true);
$task_fields = $this->get_list_view_array();
$task_fields['TIME_DUE'] = $timedate->to_display_time($timedate->to_db_time($task_fields['DATE_DUE']));
$task_fields['DATE_DUE'] = $timedate->to_display_date($timedate->to_db($task_fields['DATE_DUE']));
$date_due = $task_fields['DATE_DUE'];
if (!empty($this->priority))
$task_fields['PRIORITY'] = $app_list_strings['task_priority_dom'][$this->priority];
if (isset($this->parent_type))
$task_fields['PARENT_MODULE'] = $this->parent_type;
if ($this->status != "Completed" && $this->status != "Deferred" ) {
$task_fields['SET_COMPLETE'] = "<a href='index.php?return_module=$currentModule&return_action=".(($action=="DynamicAction")?"index":$action)."&return_id=" . ((!empty($focus->id)) ? $focus->id : "") . "&action=EditView&module=Tasks&record={$this->id}&status=Completed'>".get_image($image_path."close_inline","alt='Close' border='0'")."</a>";
}
$dd = $timedate->to_db_date($date_due, false);
if ($dd < $today){
$task_fields['DATE_DUE']= "<font class='overdueTask'>".$date_due."</font>";
}else if( $dd == $today ){
$task_fields['DATE_DUE'] = "<font class='todaysTask'>".$date_due."</font>";
}else{
$task_fields['DATE_DUE'] = "<font class='futureTask'>".$date_due."</font>";
}
require_once('include/generic/SugarWidgets/SugarWidgetSubPanelAdditionalDetailsLink.php');
$arr = array();
$swspadl = new SugarWidgetSubPanelAdditionalDetailsLink($arr);
$arr = array( 'module' => 'Tasks', 'fields' => array ( 'ID' => $this->id ) );
$task_fields['AD'] = $swspadl->displayList($arr);
$task_fields['CONTACT_NAME']= $this->contact_name; //return_name($task_fields,"FIRST_NAME","LAST_NAME");
$task_fields['TITLE'] = '';
if (!empty($task_fields['CONTACT_NAME'])) {
$task_fields['TITLE'] .= $current_module_strings['LBL_LIST_CONTACT'].": ".$task_fields['CONTACT_NAME'];
}
if (!empty($this->parent_name)) {
$task_fields['TITLE'] .= "\n".$app_list_strings['record_type_display_notes'][$this->parent_type].": ".$this->parent_name;
$task_fields['PARENT_NAME']=$this->parent_name;
}
return $task_fields;
}
function set_notification_body($xtpl, $task)
{
global $app_list_strings;
global $timedate;
$notifyUser = $task->current_notify_user;
$prefDate = User::getUserDateTimePreferences($notifyUser);
$xtpl->assign("TASK_SUBJECT", $task->name);
//MFH #13507
$xtpl->assign("TASK_PRIORITY", (isset($task->priority)?$app_list_strings['task_priority_dom'][$task->priority]:""));
$xtpl->assign("TASK_DUEDATE", $timedate->to_display_date_time($task->date_due . " " . $task->time_due,true,true,$notifyUser)." ".$prefDate['userGmt']);
$xtpl->assign("TASK_STATUS", (isset($task->status)?$app_list_strings['task_status_dom'][$task->status]:""));
$xtpl->assign("TASK_DESCRIPTION", $task->description);
return $xtpl;
}
function bean_implements($interface){
switch($interface){
case 'ACL':return true;
}
return false;
}
function listviewACLHelper(){
$array_assign = parent::listviewACLHelper();
$is_owner = false;
if(!empty($this->parent_name)){
if(!empty($this->parent_name_owner)){
global $current_user;
$is_owner = $current_user->id == $this->parent_name_owner;
}
}
if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)){
$array_assign['PARENT'] = 'a';
}else{
$array_assign['PARENT'] = 'span';
}
$is_owner = false;
if(!empty($this->contact_name)){
if(!empty($this->contact_name_owner)){
global $current_user;
$is_owner = $current_user->id == $this->contact_name_owner;
}
}
if( ACLController::checkAccess('Contacts', 'view', $is_owner)){
$array_assign['CONTACT'] = 'a';
}else{
$array_assign['CONTACT'] = 'span';
}
return $array_assign;
}
//<Section1>
//Before
/*
function save($check_notify = FALSE)
{
if (empty($this->status) ) {
$mod_strings = return_module_language($GLOBALS['current_language'], $this->module_dir);
$this->status = $mod_strings['LBL_DEFAULT_STATUS'];
}
return parent::save($check_notify);
}
*/
//After
function save($check_notify = FALSE){
global $current_user;
if (empty($this->status) ) {
$mod_strings = return_module_language($GLOBALS['current_language'], $this->module_dir);
$this->status = $mod_strings['LBL_DEFAULT_STATUS'];
}
if(is_array($this->fetched_row)) {
if($this->fetched_row['status'] != $this->status) {
if($this->fetched_row['status'] == "In Progress" && $this->status == "In Review") {
require_once('modules/Users/User.php');
$user = new User();
$user->retrieve($this->created_by);
$this->send_to_user($user);
}
if($this->fetched_row['status'] == "In Review" && $this->status == "Completed" && $current_user->id != $this->assigned_user_id) {
require_once('modules/Users/User.php');
$user = new User();
$user->retrieve($this->assigned_user_id);
$this->send_to_user($user);
}
}
}
if ($this->assigned_user_id == 'f1963700-4249-2022-1022-4e393326a7b4' )
$this->google_calendar_task = $this->addGoogleCalendarTask();
$return_id = parent::save($check_notify);
//add event to google calendar
return $return_id;
}
function send_to_user($notify_user) {
require_once("modules/Administration/Administration.php");
$admin = new Administration();
$admin->retrieveSettings();
$sendNotifications = false;
if ($admin->settings['notify_on'])
{
$GLOBALS['log']->info("Notifications: user assignment has changed, checking if user receives notifications");
$sendNotifications = true;
}
elseif(isset($_REQUEST['send_invites']) && $_REQUEST['send_invites'] == 1)
{
// cn: bug 5795 Send Invites failing for Contacts
$sendNotifications = true;
}
else
{
$GLOBALS['log']->info("Notifications: not sending e-mail, notify_on is set to OFF");
}
if($sendNotifications == true) $this->send_assignment_notifications__($notify_user, $admin);
}
function send_assignment_notifications__($notify_user, $admin)
{
global $current_user;
if(($this->object_name == 'Meeting' || $this->object_name == 'Call') || $notify_user->receive_notifications) {
$sendToEmail = $notify_user->emailAddress->getPrimaryAddress($notify_user);
if(empty($sendToEmail)) {
$GLOBALS['log']->warn("Notifications: no e-mail address set for user {$notify_user->user_name}, cancelling send");
} else {
$notify_mail = $this->create_notification_email__($notify_user);
$notify_mail->setMailerForSystem();
if(empty($admin->settings['notify_send_from_assigning_user'])) {
$notify_mail->From = $admin->settings['notify_fromaddress'];
$notify_mail->FromName = (empty($admin->settings['notify_fromname'])) ? "" : $admin->settings['notify_fromname'];
} else {
// Send notifications from the current user's e-mail (ifset)
$fromAddress = $current_user->emailAddress->getReplyToAddress($current_user);
$fromAddress = !empty($fromAddress) ? $fromAddress : $admin->settings['notify_fromaddress'];
$notify_mail->From = $fromAddress;
$from_name = !empty($admin->settings['notify_fromname']) ? $admin->settings['notify_fromname'] : "";
if($current_user->getPreference('mail_fromname') != '') {
$from_name = $current_user->getPreference('mail_fromname');
}
$notify_mail->FromName = $from_name;
}
if(!$notify_mail->Send()) {
$GLOBALS['log']->warn("Notifications: error sending e-mail (method: {$notify_mail->Mailer}), (error: {$notify_mail->ErrorInfo})");
} else {
$GLOBALS['log']->info("Notifications: e-mail successfully sent");
}
}
}
}
function create_notification_email__($notify_user) {
global $sugar_version;
global $sugar_config;
global $app_list_strings;
global $current_user;
global $locale;
require_once("XTemplate/xtpl.php");
require_once("include/SugarPHPMailer.php");
$notify_address = $notify_user->emailAddress->getPrimaryAddress($notify_user);
$notify_name = $notify_user->full_name;
$GLOBALS['log']->debug("Notifications: user has e-mail defined");
$notify_mail = new SugarPHPMailer();
$notify_mail->AddAddress($notify_address, $notify_name);
if(empty($_SESSION['authenticated_user_language'])) {
$current_language = $sugar_config['default_language'];
} else {
$current_language = $_SESSION['authenticated_user_language'];
}
$xtpl = new XTemplate("include/language/{$current_language}.notify_template.html");
$template_name = 'Task';
$this->current_notify_user = $notify_user;
if(in_array('set_notification_body', get_class_methods($this))) {
$xtpl = $this->set_notification_body($xtpl, $this);
} else {
$xtpl->assign("OBJECT", $this->object_name);
$template_name = "Default";
}
$xtpl->assign("ASSIGNED_USER", $this->new_assigned_user_name);
$xtpl->assign("ASSIGNER", $current_user->name);
$port = '';
if(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
$port = $_SERVER['SERVER_PORT'];
}
$httpHost = $_SERVER['HTTP_HOST'];
if($colon = strpos($httpHost, ':')) {
$httpHost = substr($httpHost, 0, $colon);
}
$parsedSiteUrl = parse_url($sugar_config['site_url']);
$host = ($parsedSiteUrl['host'] != $httpHost) ? $httpHost : $parsedSiteUrl['host'];
if(!isset($parsedSiteUrl['port'])) {
$parsedSiteUrl['port'] = 80;
}
$port = ($parsedSiteUrl['port'] != 80) ? ":".$parsedSiteUrl['port'] : '';
$path = !empty($parsedSiteUrl['path']) ? $parsedSiteUrl['path'] : "";
$cleanUrl = "{$parsedSiteUrl['scheme']}://{$host}{$port}{$path}";
if(eregi("localhost",$cleanUrl))$cleanUrl=str_replace("localhost","localhost:8080/vserver/e5crm.more7.com/www",$cleanUrl);
$xtpl->assign("URL", $cleanUrl."/index.php?module={$this->module_dir}&action=DetailView&record={$this->id}");
$xtpl->assign("SUGAR", "Sugar v{$sugar_version}");
$xtpl->assign("WHO_CHANGE", $current_user->full_name);
$xtpl->parse($template_name);
$xtpl->parse($template_name . "_Subject");
$notify_mail->Body = from_html(trim($xtpl->text($template_name)));
$notify_mail->Subject = from_html($xtpl->text($template_name . "_Subject"));
// cn: bug 8568 encode notify email in User's outbound email encoding
$notify_mail->prepForOutbound();
return $notify_mail;
}
function addGoogleCalendarTask() {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Http_Client');
// connect to service
$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$user = "mz@e5.pl";
$pass = "3x4z8123";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
$gcal = new Zend_Gdata_Calendar($client);
//date
$start_time = array();
$start_time[] = intval(substr($this->date_start, 11,2));
$start_time[] = intval(substr($this->date_start, 14,2));
$start_time[] = intval(substr($this->date_start, 0,2));
$start_time[] = intval(substr($this->date_start, 3,2));
$start_time[] = intval(substr($this->date_start, 6,4));
$start = date(DATE_ATOM, mktime($start_time[0],$start_time[1],0,$start_time[2],$start_time[3],$start_time[4]));
$end_time = array();
$end_time[] = intval(substr($this->date_due, 11,2));
$end_time[] = intval(substr($this->date_due, 14,2));
$end_time[] = intval(substr($this->date_due, 0,2));
$end_time[] = intval(substr($this->date_due, 3,2));
$end_time[] = intval(substr($this->date_due, 6,4));
$end = date(DATE_ATOM, mktime($end_time[0],$end_time[1],0,$end_time[2],$end_time[3],$end_time[4]));
//create description
$description = $this->description;
if ($this->parent_id && $this->parent_id!='') {
//get parent informations
$p = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT * FROM ".strtolower($this->parent_type)." WHERE id='".$this->parent_id."'"));
global $app_strings;
$description.=" ".$app_strings['LBL_'.strtoupper($this->parent_type)].': '.$p['name'];
//create where
$where = $p['billing_address_city'].', '.$p['billing_address_street'];
}
// save to server
try {
$event = $gcal->newEventEntry();
$event->title = $gcal->newTitle($this->name);
$event->content =$gcal->newContent($description);
$when = $gcal->newWhen();
$when->startTime = $start;
$when->endTime = $end;
$event->when = array($when);
$event->where = array($gcal->newWhere($where));
$new_task = $gcal->insertEvent($event);
$google_calendar_task = $new_task->getEditLink()->href;
//save gct_id
//$GLOBALS['db'] -> query("UPDATE tasks SET google_calendar_task='".$google_calendar_task."' WHERE id='".$this->id."'");
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getResponse();
} catch (Exception $e) {
echo "Error:" . $e->getResponse();
}
return $google_calendar_task;
}
//</Section1>
}
?>