Beispiel #1
0
    public function external_hook($hook)
    {
        switch ($hook) {
            case 'view':
                $file_id = isset($_REQUEST['i']) ? (int) $_REQUEST['i'] : false;
                $hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : false;
                if ($file_id && $hash) {
                    $correct_hash = $this->link_public($file_id, true);
                    if ($correct_hash == $hash) {
                        // all good to print a receipt for this payment.
                        $file_data = $this->get_file($file_id, false);
                        if ($file_data && $file_data['file_id'] == $file_id) {
                            if (isset($_POST['save_file_comments'])) {
                                if (isset($_POST['file_approve']) && isset($_POST['file_approve_go']) && isset($_POST['file_approve_name']) && strlen($_POST['file_approve_name']) > 0) {
                                    update_insert('file_id', $file_id, 'file', array('approved_time' => time(), 'approved_by' => $_POST['file_approve_name']));
                                    // send email, same 'updated' email as before.
                                    $this->send_file_changed_notice($file_id, false, true);
                                    //redirect_browser($this->link_public($file_id));
                                    $_REQUEST['new_comment_text'] = _l('File was approved at %s by %s', print_date(time(), true), htmlspecialchars($_POST['file_approve_name']));
                                }
                                if (isset($_POST['pointers'])) {
                                    update_insert('file_id', $file_id, 'file', array('pointers' => $_POST['pointers']));
                                }
                                $this->save_file_comments($file_id);
                                redirect_browser($this->link_public($file_id));
                            }
                            module_template::init_template('file_approval_view', '<h2>File Details</h2>
    File Name: <strong>{FILE_NAME}</strong> <br/>
    Download: <strong><a href="{FILE_DOWNLOAD_URL}">Click Here</a></strong> <br/>
    Status: <strong>{STATUS}</strong> <br/>
    Customer: <strong>{CUSTOMER_NAME}</strong> <br/>
    {if:JOB_NAME}Job: <strong>{JOB_NAME}</strong> <br/>{endif:JOB_NAME}
    {if:FILE_APPROVAL_PENDING}
    <h2>File Approval Pending</h2>
    <p>If you would like to approve this file please complete the form below:</p>
    <p>Your Name: <input type="text" name="file_approve_name"> </p>
    <p><input type="checkbox" name="file_approve_go" value="yes"> Yes, I approve this file. </p>
    <p><input type="submit" name="file_approve" value="Approve File" class="submit_button save_button"></p>
    {endif:FILE_APPROVAL_PENDING}
    {if:FILE_APPROVED}
    <h2>File Has Been Approved</h2>
    <p>Thank you, the file was approved by <strong>{APPROVED_BY}</strong> on <strong>{APPROVED_TIME}</strong>.</p>
    {endif:FILE_APPROVED}
    <h2>File Comments</h2>
    <p>Please feel free to add comments to this file using the form below.</p>
    {FILE_COMMENTS}
    {if:FILE_PREVIEW}
    <h2>File Preview</h2>
    <div style="overflow:scroll;">{FILE_PREVIEW}</div>
    {endif:FILE_PREVIEW}
    ', 'Used when displaying the file to a customer for approval.', 'code');
                            $template = module_template::get_template_by_key('file_approval_view');
                            // generate the html for the task output
                            $job_data = $file_data['job_id'] ? module_job::get_replace_fields($file_data['job_id']) : array();
                            if (class_exists('module_quote', false)) {
                                $quote_data = $file_data['quote_id'] ? module_quote::get_replace_fields($file_data['quote_id']) : array();
                            }
                            $customer_data = $file_data['customer_id'] ? module_customer::get_replace_fields($file_data['customer_id']) : array();
                            $file_data['file_preview'] = module_file::generate_preview($file_id, $file_data['file_name'], $file_data);
                            $file_data['FILE_DOWNLOAD_URL'] = module_file::link_public_view($file_id);
                            if (isset($file_data['approved_time'])) {
                                switch ($file_data['approved_time']) {
                                    case -1:
                                        $file_data['FILE_APPROVAL_PENDING'] = 1;
                                        break;
                                    case 0:
                                        break;
                                    default:
                                        $file_data['FILE_APPROVED'] = 1;
                                        $file_data['APPROVED_TIME'] = print_date($file_data['approved_time'], true);
                                }
                            }
                            if (class_exists('module_extra', false) && module_extra::is_plugin_enabled()) {
                                $all_extra_fields = module_extra::get_defaults('file');
                                foreach ($all_extra_fields as $e) {
                                    $file_data[$e['key']] = _l('N/A');
                                }
                                // and find the ones with values:
                                $extras = module_extra::get_extras(array('owner_table' => 'file', 'owner_id' => $file_id));
                                foreach ($extras as $e) {
                                    $file_data[$e['extra_key']] = $e['extra'];
                                }
                            }
                            ob_start();
                            ?>

                            <div id="file_notes">
                            <div style="border-top:1px dashed #CCCCCC; padding:3px; margin:3px 0;">
                                <textarea name="new_comment_text" style="width:100%;" class="no_permissions"></textarea>
                                <div style="text-align: right;">
                                <input type="submit" name="butt_save_note" id="butt_save_note" value="<?php 
                            echo _l('Add Comment');
                            ?>
" class="submit_button no_permissions">
                                    </div>
                            </div>
                            <?php 
                            foreach (module_file::get_file_comments($file_id) as $item) {
                                $note_text = forum_text($item['comment']);
                                if (preg_match_all('/#(\\d+)/', $note_text, $matches)) {
                                    //
                                    foreach ($matches[1] as $digit) {
                                        $note_text = preg_replace('/#' . $digit . '([^\\d]*)/', '<span node_id=' . $digit . ' class="pointer-ids pointer-id-' . $digit . '">#' . $digit . '</span>$1', $note_text);
                                    }
                                }
                                ?>

                                <div style="border-top:1px dashed #CCCCCC; padding:3px; margin:3px 0;">
                                    <?php 
                                echo $note_text;
                                ?>

                                    <div style="font-size:10px; text-align:right; color:#CCCCCC;">From <?php 
                                echo $item['create_user_id'] ? module_user::link_open($item['create_user_id'], true) : _l('Customer');
                                ?>
 on <?php 
                                echo print_date($item['date_created'], true);
                                ?>

                                    </div>
                                </div>

                                <?php 
                            }
                            ?>

                            </div>
                            <?php 
                            $file_data['file_comments'] = ob_get_clean();
                            $template->assign_values($file_data);
                            $template->assign_values($customer_data);
                            $template->assign_values($job_data);
                            if (class_exists('module_quote', false)) {
                                $quote_data['quote_approved_by'] = $quote_data['approved_by'];
                                $quote_data['quote_date_approved'] = $quote_data['date_approved'];
                                unset($quote_data['approved_by']);
                                unset($quote_data['date_approved']);
                                $template->assign_values($quote_data);
                            }
                            $template->page_title = $file_data['file_name'];
                            $template->content = '<form action="" method="post"><input type="hidden" name="save_file_comments" value="1">' . $template->content . '</form>';
                            echo $template->render('pretty_html');
                        }
                    }
                }
                break;
            case 'download_bucket':
                @ob_end_clean();
                $file_id = isset($_REQUEST['i']) ? (int) $_REQUEST['i'] : false;
                $hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : false;
                if ($file_id && $hash) {
                    $correct_hash = $this->link_public_download_bucket($file_id, true);
                    if ($correct_hash == $hash) {
                        // all good to print a receipt for this payment.
                        $file_data = $this->get_file($file_id, false);
                        @ignore_user_abort(true);
                        $search = array();
                        $search['bucket_parent_file_id'] = $file_id;
                        $files = module_file::get_files($search);
                        //Create ZIP
                        $zip = new ZipArchive();
                        $zipName = "bucket-" . $file_id . "-" . md5($file_id . _UCM_SECRET) . ".zip";
                        if ($zip->open(_FILE_UPLOAD_PATH . $zipName, ZIPARCHIVE::CREATE) !== TRUE) {
                            echo 'Failed to create bucket zip file';
                            exit;
                        }
                        foreach ($files as $file) {
                            if (is_file($file['file_path'])) {
                                $zip->addFromString($file['file_name'], file_get_contents($file['file_path']));
                            }
                        }
                        $zip->close();
                        //Set headers
                        header("Pragma: public");
                        header("Expires: 0");
                        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                        header("Cache-Control: public");
                        header("Content-Description: File Transfer");
                        header("Content-type: application/octet-stream");
                        //header("Content-Disposition: attachment; filename='" . $zipName . "'");
                        header("Content-Disposition: attachment; filename=\"" . preg_replace("#[^a-zA-Z0-9]+#", "-", $file_data['file_name']) . ".zip\";");
                        header("Content-Transfer-Encoding: binary");
                        header("Content-Length: " . filesize(_FILE_UPLOAD_PATH . $zipName));
                        @clearstatcache();
                        //Make sure the file size isn't cached
                        $size = @readfile(_FILE_UPLOAD_PATH . $zipName);
                        if (!$size) {
                            echo file_get_contents(_FILE_UPLOAD_PATH . $zipName);
                        }
                        @unlink(_FILE_UPLOAD_PATH . $zipName);
                    }
                }
                exit;
                break;
            case 'download':
                @ob_end_clean();
                $file_id = isset($_REQUEST['i']) ? (int) $_REQUEST['i'] : false;
                $hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : false;
                if ($file_id && $hash) {
                    $correct_hash = $this->link_public_view($file_id, true);
                    if ($correct_hash == $hash) {
                        // all good to print a receipt for this payment.
                        $file_data = $this->get_file($file_id, false);
                        if (isset($file_data['file_url']) && strlen($file_data['file_url'])) {
                            redirect_browser($file_data['file_url']);
                        } else {
                            if (is_file($file_data['file_path'])) {
                                header("Pragma: public");
                                header("Expires: 0");
                                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                                header("Cache-Control: private", false);
                                header("Content-type: " . dtbaker_mime_type($file_data['file_name'], $file_data['file_path']));
                                if (!isset($_REQUEST['embed'])) {
                                    header("Content-Disposition: attachment; filename=\"" . $file_data['file_name'] . "\";");
                                    header("Content-Transfer-Encoding: binary");
                                }
                                header("Content-Length: " . filesize($file_data['file_path']));
                                //readfile($file_data['file_path']);
                                $size = @readfile($file_data['file_path']);
                                if (!$size) {
                                    echo file_get_contents($file_data['file_path']);
                                }
                            } else {
                                echo 'Not found';
                            }
                        }
                    }
                }
                exit;
                break;
        }
    }
Beispiel #2
0
 /**
  * @param $invoice_id
  * @param $invoice_data
  * @return array
  *
  * todo: make this method call the other 'get_replace_fields' available in website/job/etc..
  */
 public static function get_replace_fields($invoice_id, $invoice_data = false)
 {
     if (!$invoice_data) {
         $invoice_data = self::get_invoice($invoice_id);
     }
     $customer_data = array();
     if ($invoice_data['customer_id']) {
         $customer_data = module_customer::get_replace_fields($invoice_data['customer_id'], $invoice_data['user_id']);
     }
     if (!is_array($customer_data)) {
         $customer_data = array();
     }
     $data = array_merge($customer_data, $invoice_data);
     // so we get total_amount_due and stuff.
     $data = array_merge($data, array('invoice_number' => htmlspecialchars($invoice_data['name']), 'project_type' => _l(module_config::c('project_name_single', 'Website')), 'print_link' => self::link_public_print($invoice_id), 'title' => module_config::s('admin_system_name'), 'invoice_paid' => $invoice_data['total_amount_due'] <= 0 ? '<p> <font style="font-size: 1.6em;"><strong>' . _l('INVOICE PAID') . '</strong></font> </p>' : '', 'is_invoice_paid' => $invoice_data['total_amount_due'] <= 0 ? 1 : '', 'date_create' => print_date($invoice_data['date_create']), 'due_date' => print_date($invoice_data['date_due'])));
     $data['total_amount'] = dollar($invoice_data['total_amount'], true, $invoice_data['currency_id']);
     $data['total_amount_due'] = dollar($invoice_data['total_amount_due'], true, $invoice_data['currency_id']);
     $data['total_amount_paid'] = dollar($invoice_data['total_amount_paid'], true, $invoice_data['currency_id']);
     $data['date_paid'] = print_date($invoice_data['date_paid']);
     $data['date_due'] = print_date($invoice_data['date_due']);
     $data['date_sent'] = print_date($invoice_data['date_sent']);
     $data['invoice_number'] = $invoice_data['name'];
     $data['invoice_url'] = module_invoice::link_public($invoice_id);
     $data['invoice_date_range'] = '';
     if ($invoice_data['date_renew'] != '0000-00-00') {
         $data['invoice_date_range'] = _l('%s to %s', print_date($invoice_data['date_create']), print_date(strtotime("-1 day", strtotime($invoice_data['date_renew']))));
     }
     if (isset($invoice_data['credit_note_id']) && $invoice_data['credit_note_id']) {
         $credit_invoice = module_invoice::get_invoice($invoice_data['credit_note_id'], true);
         $data['credit_invoice_number'] = htmlspecialchars($credit_invoice['name']);
     }
     $data['invoice_notes'] = '';
     // grab any public notes
     if (class_exists('module_note', false) && module_note::is_plugin_enabled()) {
         $notes = module_note::get_notes(array('public' => 1, 'owner_table' => 'invoice', 'owner_id' => $invoice_id));
         if (count($notes) > 1) {
             $data['invoice_notes'] .= '<ul>';
             foreach ($notes as $note) {
                 if ($note['public']) {
                     $data['invoice_notes'] .= '<li>';
                     $data['invoice_notes'] .= htmlspecialchars($note['note']);
                     $data['invoice_notes'] .= '</li>';
                 }
             }
             $data['invoice_notes'] .= '</ul>';
         } else {
             $note = array_shift($notes);
             $data['invoice_notes'] .= htmlspecialchars($note['note']);
         }
     }
     $job_names = $website_url = $project_names = $project_names_and_url = array();
     foreach ($invoice_data['job_ids'] as $job_id) {
         $job_data = module_job::get_job($job_id);
         if ($job_data && $job_data['job_id'] == $job_id) {
             $job_names[$job_data['job_id']] = $job_data['name'];
             if (module_config::c('job_invoice_show_date_range', 1)) {
                 // check if this job is a renewable job.
                 if ($job_data['date_renew'] != '0000-00-00') {
                     $data['invoice_date_range'] = _l('%s to %s', print_date($job_data['date_start']), print_date(strtotime("-1 day", strtotime($job_data['date_renew']))));
                 }
             }
             $fields = module_job::get_replace_fields($job_id, $job_data);
             foreach ($fields as $key => $val) {
                 if (!isset($data[$key]) || !$data[$key] && $val) {
                     $data[$key] = $val;
                 }
             }
             if ($job_data['website_id']) {
                 $website_data = module_website::get_website($job_data['website_id']);
                 if ($website_data && $website_data['website_id'] == $job_data['website_id']) {
                     if (isset($website_data['url']) && $website_data['url']) {
                         $website_url[$website_data['website_id']] = module_website::urlify($website_data['url']);
                         $website_data['name_url'] = $website_data['name'] . ' (' . module_website::urlify($website_data['url']) . ')';
                     } else {
                         $website_data['name_url'] = $website_data['name'];
                     }
                     $project_names[$website_data['website_id']] = $website_data['name'];
                     $project_names_and_url[$website_data['website_id']] = $website_data['name_url'];
                     $fields = module_website::get_replace_fields($website_data['website_id'], $website_data);
                     foreach ($fields as $key => $val) {
                         if (!isset($data[$key]) || !$data[$key] && $val) {
                             $data[$key] = $val;
                         }
                     }
                 }
             }
         }
     }
     if (isset($invoice_data['website_id']) && $invoice_data['website_id']) {
         $website_data = module_website::get_website($invoice_data['website_id']);
         if ($website_data && $website_data['website_id'] == $invoice_data['website_id']) {
             if (isset($website_data['url']) && $website_data['url']) {
                 $website_url[$website_data['website_id']] = module_website::urlify($website_data['url']);
                 $website_data['name_url'] = $website_data['name'] . ' (' . module_website::urlify($website_data['url']) . ')';
             } else {
                 $website_data['name_url'] = $website_data['name'];
             }
             $project_names[$website_data['website_id']] = $website_data['name'];
             $project_names_and_url[$website_data['website_id']] = $website_data['name_url'];
             $fields = module_website::get_replace_fields($website_data['website_id'], $website_data);
             foreach ($fields as $key => $val) {
                 if (!isset($data[$key]) || !$data[$key] && $val) {
                     $data[$key] = $val;
                 }
             }
         }
     }
     $data['website_name'] = $data['project_name'] = forum_text(count($project_names) ? implode(', ', $project_names) : '');
     $data['website_name_url'] = forum_text(count($project_names_and_url) ? implode(', ', $project_names_and_url) : '');
     $data['website_url'] = forum_text(count($website_url) ? implode(', ', $website_url) : '');
     $data['job_name'] = forum_text($job_names ? implode(', ', $job_names) : '');
     if (class_exists('module_group', false)) {
         // get the job groups
         $wg = array();
         $g = array();
         foreach ($invoice_data['job_ids'] as $group_job_id) {
             $group_job_id = (int) trim($group_job_id);
             if ($group_job_id > 0) {
                 $job_data = module_job::get_job($group_job_id);
                 foreach (module_group::get_groups_search(array('owner_table' => 'job', 'owner_id' => $group_job_id)) as $group) {
                     $g[$group['group_id']] = $group['name'];
                 }
                 // get the website groups
                 if ($job_data['website_id']) {
                     foreach (module_group::get_groups_search(array('owner_table' => 'website', 'owner_id' => $job_data['website_id'])) as $group) {
                         $wg[$group['group_id']] = $group['name'];
                     }
                 }
             }
         }
         if (isset($invoice_data['website_id']) && $invoice_data['website_id']) {
             foreach (module_group::get_groups_search(array('owner_table' => 'website', 'owner_id' => $invoice_data['website_id'])) as $group) {
                 $wg[$group['group_id']] = $group['name'];
             }
         }
         $data['job_group'] = implode(', ', $g);
         $data['website_group'] = implode(', ', $wg);
     }
     // addition. find all extra keys for this invoice and add them in.
     // we also have to find any EMPTY extra fields, and add those in as well.
     if (class_exists('module_extra', false) && module_extra::is_plugin_enabled()) {
         $all_extra_fields = module_extra::get_defaults('invoice');
         foreach ($all_extra_fields as $e) {
             $data[$e['key']] = _l('N/A');
         }
         // and find the ones with values:
         $extras = module_extra::get_extras(array('owner_table' => 'invoice', 'owner_id' => $invoice_id));
         foreach ($extras as $e) {
             $data[$e['extra_key']] = $e['extra'];
         }
     }
     $new_data = hook_handle_callback('invoice_replace_fields', $invoice_id, $data);
     if (is_array($new_data)) {
         foreach ($new_data as $new_d) {
             $data = array_merge($data, $new_d);
         }
     }
     return $data;
 }
$find_other_templates = 'email_template_';
$to = module_user::get_contacts(array('customer_id' => $customer_id));
$bcc = module_config::c('admin_email_address', '');
$headers = @unserialize($email['headers']);
if ($current_template && !$email_id) {
    $template = module_template::get_template_by_key($current_template);
    //todo: replace fields.
    //$replace = module_invoice::get_replace_fields($invoice_id,$invoice);
    if ($email['customer_id']) {
        $customer_data = module_customer::get_customer($email['customer_id']);
        $replace = module_customer::get_replace_fields($email['customer_id'], false, $customer_data);
        $template->assign_values($replace);
    }
    if ($email['job_id']) {
        $job_data = module_job::get_job($email['job_id']);
        $replace = module_job::get_replace_fields($email['job_id'], $job_data);
        $template->assign_values($replace);
    }
    if ($email['website_id']) {
        $website_data = module_website::get_website($email['website_id']);
        $replace = module_website::get_replace_fields($email['website_id'], $website_data);
        $template->assign_values($replace);
    }
    $email['text_content'] = $template->render('html');
    $email['subject'] = $template->replace_description();
}
$options = array('cancel_url' => module_email::link_open(false), 'complete_url' => module_email::link_open(false), 'customer_id' => $customer_id);
$options = module_email::get_email_compose_options($options);
?>

<?php 
Beispiel #4
0
 private static function send_job_task_email($job_id, $task_id, $reason)
 {
     $return_messages = array();
     if (module_config::c('job_send_staff_task_email_automatically', 0) && $reason == 'created') {
         // send the same emial as if going to job_admin_email_staff.php
         $task_data = self::get_task($job_id, $task_id);
         $job_data = self::get_job($job_id);
         if ($task_data['user_id'] > 0 && $task_data['user_id'] != module_security::get_loggedin_id()) {
             $staff = module_user::get_user($task_data['user_id']);
             if ($staff && $staff['user_id'] == $task_data['user_id'] && !(module_config::c('job_staff_email_skip_complete', 0) && $task_data['fully_completed'])) {
                 $template = module_template::get_template_by_key('job_staff_email');
                 $job_data['job_name'] = $job_data['name'];
                 $job_data['staff_name'] = $staff['name'];
                 $job_data['job_url'] = module_job::link_open($job_id);
                 $job_data['job_tasks'] = '<ul>';
                 $job_data['task_count'] = 0;
                 //foreach($job_tasks as $job_task){
                 $job_task = $task_data;
                 //if($job_task['user_id']!=$staff_id)continue;
                 //if(module_config::c('job_staff_email_skip_complete',0)&&$job_task['fully_completed'])continue;
                 $job_data['job_tasks'] .= '<li><strong>' . $job_task['description'] . '</strong>';
                 if ($job_task['fully_completed']) {
                     $job_data['job_tasks'] .= ' <span style="color: #99cc00; font-weight:bold;">(' . _l('complete') . ')</span>';
                 }
                 $job_data['job_tasks'] .= ' <br/>';
                 if ($job_task['long_description']) {
                     $job_data['job_tasks'] .= _l('Notes:') . ' <em>' . $job_task['long_description'] . '</em><br/>';
                 }
                 if ($job_task['date_due'] && $job_task['date_due'] != '0000-00-00') {
                     $job_data['job_tasks'] .= _l('Date Due:') . ' ' . print_date($job_task['date_due']) . '<br/>';
                 }
                 if ($job_task['hours']) {
                     $job_data['job_tasks'] .= _l('Assigned Hours:') . ' ' . $job_task['hours'] . '<br/>';
                 }
                 if ($job_task['completed']) {
                     $job_data['job_tasks'] .= _l('Completed Hours:') . ' ' . $job_task['completed'] . '<br/>';
                 }
                 $job_data['job_tasks'] .= '</li>';
                 $job_data['task_count']++;
                 //}
                 $job_data['job_tasks'] .= '</ul>';
                 // find available "to" recipients.
                 // customer contacts.
                 $to = array();
                 $to[] = array('name' => $staff['name'], 'email' => $staff['email']);
                 $html = $template->render('html');
                 // send an email to this user.
                 $email = module_email::new_email();
                 $email->replace_values = $job_data;
                 $email->set_to('user', $staff['user_id']);
                 $email->set_bcc_manual(module_config::c('admin_email_address', ''), '');
                 //$email->set_from('user',); // nfi
                 $email->set_subject($template->description);
                 // do we send images inline?
                 $email->set_html($html);
                 $email->job_id = $job_id;
                 $email->prevent_duplicates = true;
                 if ($email->send(false)) {
                     self::add_history($job_id, _l('Job task emailed to staff successfully'));
                     $return_messages[] = _l(' and email sent to staff %s', $staff['name']);
                 } else {
                     /// log err?
                 }
             }
         }
     }
     if (module_config::c('job_send_task_completion_email_automatically', 0) && isset($_POST['confirm_job_task_email'])) {
         $task_data = self::get_task($job_id, $task_id);
         $job_data = self::get_job($job_id);
         if ($task_data['fully_completed'] && $job_data['customer_id']) {
             $template_name = 'job_task_completion_email';
             /*if(class_exists('module_company',false) && isset($invoice_data['company_id']) && (int)$invoice_data['company_id']>0){
             			module_company::set_current_company_id($invoice_data['company_id']);
             		}*/
             $template = module_template::get_template_by_key($template_name);
             $replace = module_job::get_replace_fields($job_id, $job_data);
             $to_select = false;
             if ($job_data['customer_id']) {
                 $customer = module_customer::get_customer($job_data['customer_id']);
                 $replace['customer_name'] = $customer['customer_name'];
                 $to = module_user::get_contacts(array('customer_id' => $job_data['customer_id']));
                 if ($customer['primary_user_id']) {
                     $primary = module_user::get_user($customer['primary_user_id']);
                     if ($primary) {
                         $to_select = $primary['email'];
                     }
                 }
             } else {
                 $to = array();
             }
             $replace['job_name'] = $job_data['name'];
             $replace['task_description'] = $task_data['description'];
             $template->assign_values($replace);
             $html = $template->render('html');
             // send an email to this user.
             $email = module_email::new_email();
             $email->replace_values = $replace;
             // todo: send to all customer contacts ?
             if ($to_select) {
                 $email->set_to_manual($to_select);
             } else {
                 foreach ($to as $t) {
                     $email->set_to_manual($t['email']);
                     break;
                     // only 1? todo: all?
                 }
             }
             $email->set_bcc_manual(module_config::c('admin_email_address', ''), '');
             //$email->set_from('user',); // nfi
             $email->set_subject($template->description);
             // do we send images inline?
             $email->set_html($html);
             $email->job_id = $job_id;
             $email->customer_id = $job_data['customer_id'];
             $email->prevent_duplicates = true;
             if ($email->send(false)) {
                 // it worked successfully!!
                 // record a log on the invoice when it's done.
                 /*self::email_sent(array(
                 			'invoice_id' => $invoice_id,
                 			'template_name' => $template_name,
                 		));*/
                 self::add_history($job_id, _l('Job task emailed to customer successfully'));
                 $return_messages[] = _l(' and email sent to customer');
             } else {
                 // log err?
             }
         }
     }
     // if we are approving or rejecting job tasks with a message.
     if (isset($_POST['job_task'][$task_id]['approval_actioned']) && $_POST['job_task'][$task_id]['approval_actioned']) {
         $task_data = self::get_task($job_id, $task_id);
         $job_data = self::get_job($job_id);
         if ($task_data['user_id'] > 0) {
             $staff = module_user::get_user($task_data['user_id']);
             if ($staff && $staff['user_id'] == $task_data['user_id'] && !(module_config::c('job_staff_email_skip_complete', 0) && $task_data['fully_completed'])) {
                 $template = module_template::get_template_by_key('job_task_approval');
                 $job_data['job_name'] = $job_data['name'];
                 $job_data['staff_name'] = $staff['name'];
                 $job_data['job_url'] = module_job::link_open($job_id);
                 $job_data['approved_or_rejected'] = $_POST['job_task'][$task_id]['approval_required'] == 2 ? _l('Rejected') : _l('Approved');
                 $job_data['message'] = isset($_POST['job_task'][$task_id]['approval_message']) ? $_POST['job_task'][$task_id]['approval_message'] : _l('N/A');
                 $job_data['job_task'] = '<ul>';
                 $job_data['task_count'] = 0;
                 //foreach($job_tasks as $job_task){
                 $job_task = $task_data;
                 //if($job_task['user_id']!=$staff_id)continue;
                 //if(module_config::c('job_staff_email_skip_complete',0)&&$job_task['fully_completed'])continue;
                 $job_data['job_task'] .= '<li><strong>' . $job_task['description'] . '</strong>';
                 if ($job_task['fully_completed']) {
                     $job_data['job_task'] .= ' <span style="color: #99cc00; font-weight:bold;">(' . _l('complete') . ')</span>';
                 }
                 $job_data['job_task'] .= ' <br/>';
                 if ($job_task['long_description']) {
                     $job_data['job_task'] .= _l('Notes:') . ' <em>' . $job_task['long_description'] . '</em><br/>';
                 }
                 if ($job_task['date_due'] && $job_task['date_due'] != '0000-00-00') {
                     $job_data['job_task'] .= _l('Date Due:') . ' ' . print_date($job_task['date_due']) . '<br/>';
                 }
                 if ($job_task['hours']) {
                     $job_data['job_task'] .= _l('Assigned Hours:') . ' ' . $job_task['hours'] . '<br/>';
                 }
                 if (isset($job_task['completed']) && $job_task['completed']) {
                     $job_data['job_task'] .= _l('Completed Hours:') . ' ' . (isset($job_task['completed']) ? $job_task['completed'] : '') . '<br/>';
                 }
                 $job_data['job_task'] .= '</li>';
                 $job_data['task_count']++;
                 //}
                 $job_data['job_task'] .= '</ul>';
                 // find available "to" recipients.
                 // customer contacts.
                 $to = array();
                 $to[] = array('name' => $staff['name'], 'email' => $staff['email']);
                 $template->assign_values($job_data);
                 $html = $template->render('html');
                 // send an email to this user.
                 $email = module_email::new_email();
                 $email->replace_values = $job_data;
                 $email->set_to('user', $staff['user_id']);
                 $email->set_bcc_manual(module_config::c('admin_email_address', ''), '');
                 //$email->set_from('user',); // nfi
                 $email->set_subject($template->description);
                 // do we send images inline?
                 $email->set_html($html);
                 $email->job_id = $job_id;
                 $email->prevent_duplicates = true;
                 if ($email->send(false)) {
                     self::add_history($job_id, _l('Job task emailed to staff successfully'));
                     $return_messages[] = _l(' and email sent to staff %s', $staff['name']);
                 } else {
                     /// log err?
                 }
             }
         }
     }
     if (count($return_messages)) {
         return array('message' => implode(' ', $return_messages));
     }
     return false;
 }
Beispiel #5
0
 private function _handle_send_email()
 {
     $options = @unserialize(base64_decode($_REQUEST['options']));
     if (!$options) {
         $options = array();
     }
     $options = $this->get_email_compose_options($options);
     if (isset($_REQUEST['custom_to'])) {
         $custom_to = is_array($_REQUEST['custom_to']) ? $_REQUEST['custom_to'] : array($_REQUEST['custom_to']);
         $to = array();
         foreach ($custom_to as $ct) {
             $ct = explode('||', $ct);
             $ct['email'] = $ct[0];
             $ct['name'] = isset($ct[1]) ? $ct[1] : '';
             $ct['user_id'] = isset($ct[2]) ? (int) $ct[2] : 0;
             $to[] = $ct;
         }
     } else {
         $to = isset($options['to']) && is_array($options['to']) ? $options['to'] : array();
     }
     $email = $this->new_email();
     $email->subject = $options['subject'];
     foreach ($to as $t) {
         if (isset($t['user_id']) && $t['user_id'] > 0) {
             $email->set_to('user', $t['user_id'], $t['email'], $t['name'] . (isset($t['last_name']) && module_config::c('email_to_full_name', 1) ? ' ' . $t['last_name'] : ''));
         } else {
             $email->set_to_manual($t['email'], $t['name'] . (isset($t['last_name']) && module_config::c('email_to_full_name', 1) ? ' ' . $t['last_name'] : ''));
         }
     }
     // set from is the default from address.
     if (isset($options['from_email'])) {
         $email->set_from_manual($options['from_email'], isset($options['from_name']) ? $options['from_name'] : '');
         $email->set_bounce_address($options['from_email']);
     }
     if ($options['cc'] && is_array($options['cc'])) {
         foreach ($options['cc'] as $cc_details) {
             $bits = explode('||', $cc_details);
             if (count($bits) >= 2 && $bits[0]) {
                 $email->set_cc_manual($bits[0], $bits[1]);
             }
         }
     }
     if ($options['bcc']) {
         $bcc = explode(',', $options['bcc']);
         foreach ($bcc as $b) {
             $b = trim($b);
             if (strlen($b)) {
                 $email->set_bcc_manual($b, '');
             }
         }
     }
     if (isset($options['company_id'])) {
         $email->company_id = $options['company_id'];
     }
     if (isset($options['customer_id'])) {
         // todo: verify this is a legit customer id we can send emails to.
         $email->customer_id = $options['customer_id'];
         if ($options['customer_id'] > 0) {
             foreach (module_customer::get_replace_fields($options['customer_id']) as $key => $val) {
                 //echo "Replacing $key with $val <br>";
                 $email->replace($key, $val);
             }
         }
     }
     if (isset($options['newsletter_id'])) {
         $email->newsletter_id = $options['newsletter_id'];
     }
     if (isset($options['file_id'])) {
         $email->file_id = $options['file_id'];
     }
     if (isset($options['send_id'])) {
         $email->send_id = $options['send_id'];
     }
     if (isset($options['invoice_id'])) {
         $email->invoice_id = $options['invoice_id'];
         if ($options['invoice_id'] > 0) {
             foreach (module_invoice::get_replace_fields($options['invoice_id']) as $key => $val) {
                 $email->replace($key, $val);
             }
         }
     }
     if (isset($options['job_id'])) {
         $email->job_id = $options['job_id'];
         if ($options['job_id'] > 0) {
             foreach (module_job::get_replace_fields($options['job_id']) as $key => $val) {
                 $email->replace($key, $val);
             }
         }
     }
     if (isset($options['website_id'])) {
         $email->website_id = $options['website_id'];
         if ($options['website_id'] > 0) {
             foreach (module_website::get_replace_fields($options['website_id']) as $key => $val) {
                 $email->replace($key, $val);
             }
         }
     }
     if (isset($options['quote_id'])) {
         $email->quote_id = $options['quote_id'];
         if ($options['quote_id'] > 0) {
             foreach (module_quote::get_replace_fields($options['quote_id']) as $key => $val) {
                 $email->replace($key, $val);
             }
         }
     }
     // custom data integration
     if (class_exists('module_data', false) && module_config::c('custom_data_in_email', 1) && $options['customer_id'] > 0 && !empty($_REQUEST['custom_data_info']) && !empty($_REQUEST['custom_data_related'])) {
         global $plugins;
         // find all possible custom data entries
         $data_types = $plugins['data']->get_data_types();
         foreach ($data_types as $data_type) {
             switch ($data_type['data_type_menu']) {
                 case _CUSTOM_DATA_MENU_LOCATION_CUSTOMER:
                     if ($plugins['data']->can_i('view', $data_type['data_type_name'])) {
                         $search = array('customer_id' => $options['customer_id'], 'data_type_id' => $data_type['data_type_id']);
                         // we have to limit the data types to only those created by current user if they are not administration
                         $datas = $plugins['data']->get_datas($search);
                         if ($datas) {
                             // found some! does this exist in one of our inputs?
                             if (!empty($_REQUEST['custom_data_info'][$data_type['data_type_id']]) && !empty($_REQUEST['custom_data_related'][$data_type['data_type_id']])) {
                                 $data_record_id = $_REQUEST['custom_data_related'][$data_type['data_type_id']];
                                 $data_info = json_decode($_REQUEST['custom_data_info'][$data_type['data_type_id']], true);
                                 if (is_array($data_info) && isset($datas[$data_record_id])) {
                                     // we have a winner!
                                     $list_fields = array();
                                     $data_field_groups = $plugins['data']->get_data_field_groups($data_type['data_type_id']);
                                     foreach ($data_field_groups as $data_field_group) {
                                         $data_fields = $plugins['data']->get_data_fields($data_field_group['data_field_group_id']);
                                         foreach ($data_fields as $data_field) {
                                             if ($data_field['show_list']) {
                                                 $list_fields[$data_field['data_field_id']] = $data_field;
                                             }
                                         }
                                     }
                                     $list_data_items = $plugins['data']->get_data_items($data_record_id);
                                     foreach ($list_fields as $list_field) {
                                         $settings = @unserialize($list_data_items[$list_field['data_field_id']]['data_field_settings']);
                                         if (!isset($settings['field_type'])) {
                                             $settings['field_type'] = isset($list_field['field_type']) ? $list_field['field_type'] : false;
                                         }
                                         $value = false;
                                         if (isset($list_data_items[$list_field['data_field_id']])) {
                                             $value = $list_data_items[$list_field['data_field_id']]['data_text'];
                                         }
                                         if ($value) {
                                             $data_info['key'] = $value;
                                             break;
                                         }
                                     }
                                     $data_info['data_record_id'] = $data_record_id[$data_type['data_type_id']];
                                     $email->custom_data[$data_type['data_type_id']] = $data_info;
                                     $email->set_custom_data($data_type['data_type_id'], $data_record_id);
                                 }
                             }
                         }
                     }
             }
         }
     }
     // final override for first_name last_name if selected from the custom to drop down
     foreach ($to as $t) {
         if (isset($t['user_id']) && $t['user_id'] > 0) {
             $user = module_user::get_user($t['user_id']);
             if ($user) {
                 if (strpos($options['content'], '{AUTO_LOGIN_LINK}') !== false && $t['user_id'] != 1) {
                     $email->replace('AUTO_LOGIN_LINK', module_security::generate_auto_login_link($t['user_id']));
                 }
                 $email->replace('first_name', $user['name']);
                 $email->replace('last_name', $user['last_name']);
             }
         }
     }
     if (isset($options['note_id'])) {
         $email->note_id = $options['note_id'];
     }
     if (isset($options['debug_message'])) {
         $email->debug_message = $options['debug_message'];
     }
     $email->set_html($options['content']);
     foreach ($options['attachments'] as $attachment) {
         $email->AddAttachment($attachment['path'], $attachment['name']);
     }
     // new addition, manually added attachments.
     if (isset($_FILES['manual_attachment']) && isset($_FILES['manual_attachment']['tmp_name'])) {
         foreach ($_FILES['manual_attachment']['tmp_name'] as $key => $tmp_name) {
             if (is_uploaded_file($tmp_name) && isset($_FILES['manual_attachment']['name'][$key]) && strlen($_FILES['manual_attachment']['name'][$key])) {
                 $email->AddAttachment($tmp_name, $_FILES['manual_attachment']['name'][$key]);
             }
         }
     }
     if ($email->send()) {
         if (isset($options['success_callback_args']) && count($options['success_callback_args']) && $options['success_callback'] && is_callable($options['success_callback'])) {
             // new callback method using call_user_func_array
             $args = $options['success_callback_args'];
             $args['email_id'] = $email->email_id;
             if (preg_match('#module_\\w#', $options['success_callback'])) {
                 call_user_func($options['success_callback'], $args);
             }
         }
         /*else if($options['success_callback']){
               eval($options['success_callback']);
           }*/
         set_message('Email sent successfully');
         redirect_browser($options['complete_url']);
     } else {
         set_error('Sending email failed: ' . $email->error_text);
         redirect_browser($options['cancel_url']);
     }
 }