<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * SugarCRM Community Edition is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero 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 Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero 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 Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero 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".
 ********************************************************************************/

require_once('modules/Tasks/Task.php');
require_once('include/TimeDate.php');

class add_tasks  {
   
    function add_new_tasks($bean, $event, $arguments){
global $timedate,$current_user;
$format ="Y-m-d h:i:s";

    if(!empty($_REQUEST['return_module'])){   
                
                        $bean->load_relationships('contact_group_task_contact_group_task_tasks_1');
                        $res = $bean->contact_group_task_contact_group_task_tasks_1->getBeans();
                             if(is_array($res)){
                             foreach($res as $result){
                             $task = new Task();
 $task->id = create_guid();
 $task->new_with_id =1;
                             $task->name = $result->name;
                            $curTime = $timedate->nowDb();
                            $curTime = "now";

                        if(strstr($result->after_offer_date, 'hour') || strstr($result->after_offer_date, 'minutes')){    
                        $task->date_due = date($format,strtotime($result->after_offer_date, strtotime($curTime)));
                        }
                        else{
                            $days = " +".$result->after_offer_date." day";
                            $task->date_due = date($format,strtotime($curTime.$days));
                        }

                             $task->date_start= $task->date_due;
                             $task->date_end= $task->date_due;

                             $task->assigned_user_id = $result->assigned_user_id;

if( $_REQUEST['module'] == 'contact_group_task' && $_REQUEST['child_field'] =='contact_group_task_accounts_1'){
                             $task->parent_id =$_REQUEST['subpanel_id'];
                             $task->parent_type ="Accounts";
}
else if( $_REQUEST['module'] == 'contact_group_task' && $_REQUEST['child_field'] =='contact_group_task_contacts_1'){
                             $task->parent_id =$_REQUEST['subpanel_id'];
                             $task->parent_type ="Contacts";
}
else if( $_REQUEST['module'] == 'contact_group_task' && $_REQUEST['child_field'] =='contact_group_task_leads_1'){
                             $task->parent_id =$_REQUEST['subpanel_id'];
                             $task->parent_type ="Leads";
}
else if( $_REQUEST['module'] == 'contact_group_task' && $_REQUEST['child_field'] =='contact_group_task_prospects_1'){
                             $task->parent_id =$_REQUEST['subpanel_id'];
                             $task->parent_type ="Prospects";
}
else{
 $task->parent_id =$_REQUEST['record'];
                             $task->parent_type =$_REQUEST['return_module'];
}
//$_REQUEST['refresh_page']=1;

                            $task->description =$result->description;
							                            $task->status =$result->status;
							                            $task->priority =$result->priority;

                            $id = $task->Save();

if($_REQUEST['module'] !='contact_Group_task_tasks'){
$task_bean = outright_retrieve('outr_tasks_history');
							$task_bean->name = 'Automated Task';
							$task_bean->task_id_c = $task->id;
							$task_bean->contact_group_task_id_c = $bean->id;

							$task_bean->save();  
}

                                     }  
							                                 
                                   }
							
      }
       

    }

        function del_tasks($bean, $event, $arguments){
            global $db,$current_user;

if($_REQUEST['linked_field'] || $_REQUEST['module']){
echo  $sql ="UPDATE tasks set deleted =1 where tasks.parent_id='".$_REQUEST['record']."'";
//$db->query($sql);
}
        }
}