phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] property/inc/class.boalarm.inc.php, 1.1


From: nomail
Subject: [Phpgroupware-cvs] property/inc/class.boalarm.inc.php, 1.1
Date: Tue, 29 Jun 2004 14:49:58 +0200

Update of /property/inc
Added Files:
        Branch: 
          class.boalarm.inc.php

date: 2004/06/29 12:49:58;  author: sigurdne;  state: Exp;

Log Message:
no message
=====================================================================
<?php
        
/**************************************************************************\
        * phpGroupWare - property                                               
   *
        * http://www.phpgroupware.org                                           
   *
        *                                                                       
   *
        * Facilities Management                                                 
   *
        * Written by Sigurd Nes [sigurdne at online.no]                         
   *
        * 
------------------------------------------------------------------------ *
        * Copyright 2000 - 2003 Free Software Foundation, Inc                   
   *
        * This program is part of the GNU project, see http://www.gnu.org/      
   *
        * 
------------------------------------------------------------------------ *
        * This program is free software; you can redistribute it and/or modify 
it  *
        * under the terms of the GNU General Public License as published by the 
   *
        * Free Software Foundation; either version 2 of the License, or (at 
your   *
        * option) any later version.                                            
   *
        
\**************************************************************************/
        /* $Id: class.boalarm.inc.php,v 1.1 2004/06/29 12:49:58 sigurdne Exp $ 
*/

        class boalarm
        {

                function boalarm()
                {

                        $this->currentapp               = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        if (!is_object($GLOBALS['phpgw']->asyncservice))
                        {
                                $GLOBALS['phpgw']->asyncservice = 
CreateObject('phpgwapi.asyncservice');
                        }
                        $this->async = &$GLOBALS['phpgw']->asyncservice;

                }

                /*!
                @function read_alarms
                @abstract read the alarms of a calendar-event specified by 
$cal_id
                @returns array of alarms with alarm-id as key
                @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, 
it is used as the job-id too
                */
                function read_alarms($type='',$input_id,$text='')
                {
                        $alarms = array();

                        if ($jobs = $this->async->read($type . 
':'.intval($input_id).':%'))
                        {
                                foreach($jobs as $id => $job)
                                {
                                        $alarm                          = 
$job['data']; // text, enabled
                                        $alarm['alarm_id']      = $id;
                                        $alarm['time']          = 
$GLOBALS['phpgw']->common->show_date($job['next']);
                                        $alarm['user']          = 
$GLOBALS['phpgw']->accounts->id2name($alarm['owner']);
                                        $alarm['text']          = $text;

                                        $alarms[] = $alarm;
                                }
                        }
                        return $alarms;
                }


                /*!
                @function read_alarm
                @abstract read a single alarm specified by it's $id
                @returns array with data of the alarm
                @note the alarm-id is a string of 'cal:'.$cal_id.':'.$alarm_nr, 
it is used as the job-id too
                */
                function read_alarm($alarm_type,$id)
                {
                        if (!($jobs = $this->async->read($id)))
                        {
                                return False;
                        }
                        list($id,$job) = each($jobs);
                        $alarm         = $job['data'];  // text, enabled
                        $alarm['id']   = $id;
                        $alarm['time'] = $job['next'];

                        //echo "<p>read_alarm('$id')="; print_r($alarm); echo 
"</p>\n";
                        return $alarm;
                }



                /*!
                @function enable
                @abstract enable or disable one or more alarms identified by 
its ids
                @syntax enable($ids,$enable=True)
                @param $ids array with alarm ids as keys (!)
                @returns the number of alarms enabled or -1 for insuficent 
permission to do so
                @note Not found alarms or insuficent perms stop the enableing 
of multiple alarms
                */
                function enable_alarm($alarm_type,$alarms,$enable=True)
                {
                        $enabled = 0;
                        foreach ($alarms as $id => $field)
                        {
                                if (!($alarm = 
$this->read_alarm($alarm_type,$id)))
                                {
                                        return 0;       // alarm not found
                                }
                                if (!$alarm['enabled'] == !$enable)
                                {
                                        continue;       // nothing to do
                                }
/*                              if ($enable && 
!$this->check_perms(PHPGW_ACL_SETALARM,$alarm['owner']) ||
                                        !$enable && 
!$this->check_perms(PHPGW_ACL_DELETEALARM,$alarm['owner']))
                                {
                                        return -1;
                                }
*/
                                $alarm['enabled'] = intval(!$alarm['enabled']);
                                if 
($this->save_alarm($alarm_type,$alarm['entity_id'],$alarm))
                                {
                                        ++$enabled;
                                }
                        }
                        return $enabled;
                }


                /*!
                @function save_alarm
                @abstract saves a new or updated alarm
                @syntax save_alarm($cal_id,$alarm,$id=False)
                @param $cal_id Id of the calendar-entry
                @param $alarm array with fields: text, owner, enabled, ..
                */
                function save_alarm($alarm_type,$entity_id,$alarm)
                {
//                      echo "<p>save_alarm(entity_id=$entity_id, alarm="; 
print_r($alarm); echo ")</p>\n";
                        if (!($id = $alarm['id']))
                        {
                                $alarms = 
$this->read_alarms($alarm_type,$entity_id);   // find a free alarm#
                                $n = count($alarms);
                                do
                                {
                                        $id = $alarm_type 
.':'.intval($entity_id).':'.$n;
                                        ++$n;
                                }
                                while (@isset($alarms[$id]));
                        }
                        else
                        {
                                $this->async->cancel_timer($id);
                        }
                        $alarm[$alarm_type.'_id'] = $entity_id;         // we 
need the back-reference

                        if 
(!$this->async->set_timer($alarm['time'],$id,$this->currentapp 
.'.boalarm.send_alarm',$alarm))
                        {
                                return False;
                        }
                        return $id;
                }

                /*!
                @function add_alarm
                @abstract adds a new alarm to an event
                @syntax add(&$event,$time,$login_id)
                @param &$event event to add the alarm too
                @param $time for the alarm in sec before the starttime of the 
event
                @param $login_id user to alarm
                @returns the alarm or False
                */
                function add_alarm($alarm_type,&$event,$time,$owner)
                {
/*                      if (!$this->check_perms(PHPGW_ACL_SETALARM,$owner) || 
!($cal_id = $event['id']))
                        {
                                return False;
                        }
*/
                        if(!$owner>0)
                        {
                                $receipt['error'][]=array('msg'=>lang('No user 
selected'));
                                return  $receipt;
                        }

                        $alarm = Array(
                                'time'    => ($event['start_date'] - $time), 
//($etime=$this->bo->maketime($event['start'])) - $time,
                                'offset'  => $time,
                                'owner'   => $owner,
                                'enabled' => 1
                        );
                        $alarm['id'] = 
$this->save_alarm($alarm_type,$event['id'],$alarm);
//_debug_array($alarm);

                        $event['alarm'][$alarm['id']] = $alarm;

                        return $alarm;
                }


                /*!
                @function delete
                @abstract delete one or more alarms identified by its ids
                @syntax delete($ids)
                @param $ids array with alarm ids as keys (!)
                @returns the number of alarms deleted or -1 for insuficent 
permission to do so
                @note Not found alarms or insuficent perms stop the deleting of 
multiple alarms
                */
                function delete_alarm($alarm_type,$alarms)
                {
                        $deleted = 0;
                        foreach ($alarms as $id => $field)
                        {
                                if (!($alarm = 
$this->read_alarm($alarm_type,$id)))
                                {
                                        return 0;       // alarm not found
                                }
/*                              if 
(!$this->check_perms(PHPGW_ACL_DELETEALARM,$alarm['owner']))
                                {
                                        return -1;
                                }
*/
                                if ($this->async->cancel_timer($id))
                                {
                                        ++$deleted;
                                }
                        }
                        return $deleted;
                }


                function send_alarm($alarm)
                {

                        //echo "<p>bocalendar::send_alarm("; print_r($alarm); 
echo ")</p>\n";
                        $GLOBALS['phpgw_info']['user']['account_id'] = 
$this->owner = $alarm['owner'];

                        if (!$alarm['enabled'] || !$alarm['owner'])
                        {
                                return False;   // event not found
                        }

                        $this->config           = 
CreateObject('phpgwapi.config');
                        $this->config->read_repository();
                        $this->send                     = 
CreateObject('phpgwapi.send');

                        $members = array();

                        // build subject
                        $subject = lang('Alarm');

                        $prefs_user = 
$this->bocommon->create_preferences($this->currentapp,$alarm['owner']);

                        $from_address=$prefs_user['email'];

        //-----------from--------


                        
$current_user_id=$GLOBALS['phpgw_info']['user']['account_id'];

                        $current_user_firstname = 'FM';

                        $current_user_lastname  = 'System';

                        $current_user_name= $user_firstname . " " 
.$user_lastname ;

                        $current_prefs_user = 
$this->bocommon->create_preferences($this->currentapp,$alarm['owner']);
                        $current_user_address=$current_prefs_user['email'];

                        $headers = "Return-Path: <". $current_user_address 
.">\r\n";
                        $headers .= "From: " . $current_user_name . "<" . 
$current_user_address .">\r\n";
//                      $headers .= "Bcc: " . $current_user_name . "<" . 
$current_user_address .">\r\n";
                        $headers .= "Content-type: text/html; 
charset=iso-8859-1\r\n";
                        $headers .= "MIME-Version: 1.0\r\n";

        //-----------from--------
                // build body
                        $body  = '';
                        $body .= lang('Alarm').' #'.$alarm['alarm_id']."\n";
//                      $body .= lang('Date Opened').': '.$timestampopened."\n";
//                      $body .= lang('Category').': '. 
$this->get_category_name($ticket['cat_id']) ."\n";
//                      $body .= lang('Subject').': '. $ticket['subject'] ."\n";
//                      $body .= lang('Location').': '. 
$ticket['location_code'] ."\n";
//                      $body .= lang('Address').': '. $ticket['address'] ."\n";

                        $body .= lang('Assigned To').': 
'.$GLOBALS['phpgw']->accounts->id2name($alarm['owner'])."\n";

                        // add assigned to recipients
                        $members[] = array('account_id' => $alarm['owner'], 
'account_name' => $GLOBALS['phpgw']->accounts->id2name($alarm['owner']));

                        $error = Array();
                        $toarray = Array();
                        $i=0;
                        for ($i=0;$i<count($members);$i++)
                        {
                                if ($members[$i]['account_id'])
                                {
                        //              $prefs = 
$GLOBALS['phpgw']->preferences->create_email_preferences($members[$i]['account_id']);
                                        $prefs = 
$this->bocommon->create_preferences($this->currentapp,$members[$i]['account_id']);
                                        if (strlen($prefs['email'])> 
(strlen($members[$i]['account_name'])+1))
                                        {
                                                $toarray[$prefs['email']] = 
$prefs['email'];
                                        }
                                        else
                                        {
                                                $receipt['error'][] = 
array('msg'=> lang('Your message could not be sent!'));
                                                $receipt['error'][] = 
array('msg'=>lang('This user has not defined an email address !') . ' : ' . 
$members[$i]['account_name']);
                                        }
                                }
                        }

                        if(count($toarray) > 1)
                        {
                                $to = implode(',',$toarray);
                        }
                        else
                        {
                                $to = current($toarray);
                        }

                        $mail_method= 
$this->config->config_data['fmwrkorder_mail'];
//                      echo 'mail_method: '.$mail_method .'<BR>';

                        if ($mail_method=='smtp'):
                        {
                                $rc = $this->send->msg('email', $to, $subject, 
stripslashes($body), '', $cc, 
$bcc,$current_user_address,$current_user_name,'txt');
                        }
                        elseif ($mail_method=='sendmail'):
                        {
                                $rc=    @mail($to,$subject,stripslashes($body), 
$headers);
                        }
                        else:
                        {
                                $receipt['error'][] = array('msg'=> 
lang('Mailing method is not chosen! (admin section)'));
                        }
                        endif;

                //      $rc=1;
                        if (!$rc)
                        {
                                $receipt['error'][] = array('msg'=> lang('Your 
message could not be sent by mail!'));
                                $receipt['error'][] = array('msg'=> lang('The 
mail server returned'));
                                $receipt['error'][] = array('msg'=> 'From :' . 
$current_user_name . '<' . $current_user_address .'>');
                                $receipt['error'][] = array('msg'=> 'to: '.$to);
                                $receipt['error'][] = array('msg'=> 'subject: 
'.$subject);
                                $receipt['error'][] = array('msg'=> $body );
        //                      $receipt['error'][] = array('msg'=> 'cc: ' . 
$cc);
        //                      $receipt['error'][] = array('msg'=> 'bcc: 
'.$bcc);
                                $receipt['error'][] = array('msg'=> 'group: 
'.$group_name);
                                $receipt['error'][] = array('msg'=> 'err_code: 
'.$this->send->err['code']);
                                $receipt['error'][] = array('msg'=> 'err_msg: 
'. htmlspecialchars($this->send->err['msg']));
                                $receipt['error'][] = array('msg'=> 'err_desc: 
'. $GLOBALS['phpgw']->err['desc']);
                        }

//_debug_array($receipt);
//                      return $receipt;
                }

        }
?>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]