Example #1
0
 public function save()
 {
     $errors = array();
     $file = File::Factory($_FILES['file'], $errors, new File());
     $file->save();
     $ticketAttachment = TicketAttachment::Factory(array('ticket_id' => $this->_data['ticket_id'], 'file_id' => $file->id), $errors, new TicketAttachment());
     $ticketAttachment->save();
     sendTo('Attachments', 'view', array('ticketing'), array('id' => $ticketAttachment->id));
 }
Example #2
0
 public function save_response()
 {
     parent::save('TicketResponse');
     $ticket = new Ticket();
     $ticket->load($this->_data['TicketResponse']['ticket_id']);
     $plateout = TicketingUtils::StatusPlate($ticket);
     $queue = new TicketQueue();
     $queue->load($ticket->ticket_queue_id);
     $headers = array('From' => $queue->email_address);
     // FIXME: If someone forces a file upload... I guess that causes this code to randomly send the file?
     if ($_FILES['file']['size'] > 0) {
         // Send MIME mail
         $boundary = 'EGS-Ticketing-System-' . base_convert(rand(1000, 9000), 10, 2);
         $headers['Content-Type'] = 'multipart/mixed; boundary=' . $boundary;
         $base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']));
         // Yay, hand written MIME email!
         $body = "--{$boundary}\r\n" . "Content-Transfer-Encoding: 8bit\r\n" . "Content-Type: text/plain; charset=ISO-8859-1\r\n" . "\r\n" . $plateout . $this->_data['TicketResponse']['body'] . "\r\n" . "\r\n" . "--{$boundary}\r\n" . 'Content-Type: octet/stream; name="' . $_FILES['file']['name'] . '"' . "\r\n" . "Content-Transfer-Encoding: base64\r\n" . 'Content-Disposition: attachment; filename="' . $_FILES['file']['name'] . '"' . "\r\n" . "\r\n" . chunk_split($base64) . "\r\n" . "\r\n" . "--{$boundary}--\r\n" . ".";
         $errors = array();
         $file = File::Factory($_FILES['file'], $errors, new File());
         $file->save();
         $ticketAttachment = TicketAttachment::Factory(array('ticket_id' => $this->_data['TicketResponse']['ticket_id'], 'file_id' => $file->id), $errors, new TicketAttachment());
         $ticketAttachment->save();
     } else {
         // No attachment, send plain text mail
         $body = $plateout . $this->_data['TicketResponse']['body'];
     }
     $header_string = "";
     foreach ($headers as $header => $value) {
         $header_string .= $header . ': ' . $value . "\r\n";
     }
     // FIXME: Do this further up
     if (!isset($this->_data['TicketResponse']['internal']) || isset($this->_data['TicketResponse']['internal']) && $this->_data['TicketResponse']['internal'] != 'on') {
         $recipients = $recipients = TicketingUtils::GetRecipients($ticket);
         foreach ($recipients as $recipient) {
             mail($recipient, 're: [' . $ticket->ticket_queue_id . '-' . $ticket->id . '] ' . $ticket->summary, $body, $header_string);
         }
     }
     sendTo('Client', 'view', array('ticketing'), array('id' => $this->_data['TicketResponse']['ticket_id']));
 }
Example #3
0
 public function save()
 {
     $errors = array();
     $flash = Flash::Instance();
     // Need to upload file before checking
     $file = File::Factory($_FILES['file'], $errors, DataObjectFactory::Factory('File'));
     // Check if this file name already exists
     $collection = new EntityAttachmentCollection();
     $sh = new SearchHandler($collection, FALSE);
     $sh->addConstraint(new Constraint('data_model', '=', $this->_data['data_model']));
     $sh->addConstraint(new Constraint('entity_id', '=', $this->_data['entity_id']));
     $sh->addConstraint(new Constraint('file', '=', $file->name));
     $data = $collection->load($sh, null, RETURN_ROWS);
     $count = count($data);
     $update = FALSE;
     // Should only be one or none; otherwise this is an error
     if ($count > 1) {
         $errors[] = 'Found ' . $count . ' versions of this file';
     } elseif ($count > 0) {
         $row = current($data);
         $current_revision = $row['revision'];
         $new_revision = ++$row['revision'];
         $update = TRUE;
     } else {
         $current_revision = 0;
         $new_revision = 1;
     }
     if (empty($this->_data['revision'])) {
         $this->_data['revision'] = $new_revision;
     } elseif ($this->_data['revision'] <= $current_revision) {
         $errors[] = 'Current version ' . $current_revision . ' is the same or later than input version ' . $this->_data['revision'];
     }
     $db = DB::Instance();
     $db->StartTrans();
     $attachment_save = $file_save = FALSE;
     // Save the File data
     if (empty($errors)) {
         $file->note = $this->_data['note'];
         $file->revision = $this->_data['revision'];
         $file_save = $file->save();
     }
     // If file save OK, save the attachment details
     if ($file_save) {
         $attachment_data = array();
         if ($update) {
             $attachment_data['id'] = $row['id'];
         }
         $attachment_data['entity_id'] = $this->_data['entity_id'];
         $attachment_data['data_model'] = $this->_data['data_model'];
         $attachment_data['file_id'] = $file->id;
         $attachment = EntityAttachment::Factory($attachment_data, $errors, $this->modeltype);
         if (empty($errors)) {
             $attachment_save = $attachment->save();
         }
     }
     if ($update && $attachment_save) {
         // delete the old file entry for an update
         $old_file = DataObjectFactory::Factory('File');
         $file_save = $old_file->delete($row['file_id'], $errors);
     }
     // Now check and tidy up
     if (!$file_save || !$attachment_save) {
         $errors[] = 'Error loading file';
     }
     if (!empty($errors)) {
         $flash->addErrors($errors);
         $db->FailTrans();
     } else {
         if ($update) {
             $flash->addMessage('File ' . $file->name . ' version ' . $current_revision . ' replaced with version ' . $new_revision);
         } else {
             $flash->addMessage('File ' . $file->name . ' version ' . $new_revision . ' uploaded OK');
         }
     }
     $db->CompleteTrans();
     // Return to calling module/controller
     sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null);
 }
 public function save()
 {
     if (!$this->CheckParams($this->modeltype)) {
         sendBack();
     }
     $this->loadData();
     $systemcompany = $this->_uses[$this->modeltype];
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $flash = Flash::Instance();
     $data = $this->_data[$this->modeltype];
     if (!isset($data['id']) || empty($data['id'])) {
         $company = DataObject::Factory($data, $errors, 'Company');
         if (count($errors) > 0 || !$company->save()) {
             $errors[] = 'Failed to create Company';
             $db->FailTrans();
         } else {
             $data['company_id'] = $company->id;
         }
     }
     if (isset($data['debug_options']) && isset($data['id'])) {
         $debug = DebugOption::getCompanyOption($data['id']);
         if (isset($data['debug_enabled'])) {
             $data['DebugOption']['company_id'] = $data['id'];
             $data['DebugOption']['options'] = $debug->setOptions($data['debug_options']);
         } else {
             if ($debug->isLoaded()) {
                 $debug->delete();
             }
             unset($data['DebugOption']);
         }
     } else {
         unset($data['DebugOption']);
     }
     if (isset($data['delete_logo'])) {
         if ($this->delete_logo($systemcompany->logo_file_id, $errors)) {
             $data['logo_file_id'] = NULL;
         } else {
             $errors[] = 'Error deleting Logo image';
         }
     }
     if (!empty($_FILES['file']['name'])) {
         // Need to upload file before checking
         $file = File::Factory($_FILES['file'], $errors, DataObjectFactory::Factory('File'));
         if ($file->save()) {
             if (!is_null($systemcompany->logo_file_id)) {
                 $old_file = DataObjectFactory::Factory('File');
                 if (!$this->delete_logo($systemcompany->logo_file_id, $errors)) {
                     $errors[] = 'Error replacing Logo image';
                 }
             }
             $data['logo_file_id'] = $file->id;
         } else {
             $errors[] = 'Error loading Logo image';
         }
     }
     if (count($errors) == 0) {
         if (!parent::save($this->modeltype, $data, $errors)) {
             $errors[] = 'Failed to save System Company';
             $db->FailTrans();
         }
     }
     if (count($errors) == 0) {
         $result = $this->saved_model->setPermissions($this->_data['Permission'], $errors);
         if ($result === FALSE) {
             $errors[] = 'Failed to save System Company Permissions';
             $db->FailTrans();
         }
     }
     $db->CompleteTrans();
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         if (isset($data['id'])) {
             $this->_data['id'] = $data['id'];
         }
         $this->refresh();
     } else {
         sendTo($this->name, 'index', $this->_modules);
     }
 }
Example #5
0
 public function save_response()
 {
     // sort the data out
     if (isset($this->_data['response'])) {
         $this->_data['TicketResponse']['body'] = $this->_data['response'];
         $this->_data['TicketResponse']['ticket_id'] = $this->_data['ticket_id'];
         $this->_data['TicketResponse']['type'] = 'site';
     }
     $save = parent::save('TicketResponse', $this->_data['TicketResponse']);
     $ticket = new Ticket();
     $ticket->load($this->_data['TicketResponse']['ticket_id']);
     $plateout = TicketingUtils::StatusPlate($ticket);
     $headers = array('From' => TicketingUtils::getReplyAddress($ticket));
     $hours_data =& $this->_data['Hour'];
     if (isset($hours_data['duration']) && $hours_data['duration'] != 0) {
         if ($hours_data['duration_unit'] == 'days') {
             $hours_data['duration'] = $hours_data['duration'] * SystemCompanySettings::DAY_LENGTH;
         }
         // Calculate start time by working backward NB: Needs changing with date_format?
         $hours_data['start_time'] = date("d/m/Y H:i", time() - $hours_data['duration'] * 60 * 60);
         $hours_data['duration'] .= ' hours';
         parent::save('Hour');
     }
     // FIXME: If someone forces a file upload... I guess that causes this code to randomly send the file?
     if ($_FILES['file']['size'] > 0) {
         // Send MIME mail
         $boundary = 'EGS-Ticketing-System-' . base_convert(rand(1000, 9000), 10, 2);
         $headers['Content-Type'] = 'multipart/mixed; boundary=' . $boundary;
         $base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']));
         // Yay, hand written MIME email!
         $body = "--{$boundary}\r\n" . "Content-Transfer-Encoding: 8bit\r\n" . "Content-Type: text/plain; charset=ISO-8859-1\r\n" . "\r\n" . $plateout . $this->_data['TicketResponse']['body'] . "\r\n" . "\r\n" . "--{$boundary}\r\n" . 'Content-Type: octet/stream; name="' . $_FILES['file']['name'] . '"' . "\r\n" . "Content-Transfer-Encoding: base64\r\n" . 'Content-Disposition: attachment; filename="' . $_FILES['file']['name'] . '"' . "\r\n" . "\r\n" . chunk_split($base64) . "\r\n" . "\r\n" . "--{$boundary}--\r\n" . ".";
         $errors = array();
         $file = File::Factory($_FILES['file'], $errors, new File());
         $file->save();
         $ticketAttachment = TicketAttachment::Factory(array('ticket_id' => $this->_data['TicketResponse']['ticket_id'], 'file_id' => $file->id), $errors, new TicketAttachment());
         $ticketAttachment->save();
     } else {
         // No attachment, send plain text mail
         $body = $plateout . $this->_data['TicketResponse']['body'];
     }
     $header_string = "";
     foreach ($headers as $header => $value) {
         $header_string .= $header . ': ' . $value . "\r\n";
     }
     // FIXME: Do this further up
     if (!isset($this->_data['TicketResponse']['internal']) || isset($this->_data['TicketResponse']['internal']) && $this->_data['TicketResponse']['internal'] != 'on') {
         $recipients = TicketingUtils::GetRecipients($ticket);
         foreach ($recipients as $recipient) {
             mail($recipient, 're: [' . $ticket->ticket_queue_id . '-' . $ticket->id . '] ' . $ticket->summary, $body, $header_string);
         }
     }
     if (is_ajax()) {
         echo json_encode(array('success' => $save));
         exit;
     } else {
         sendTo('Tickets', 'view', array('ticketing'), array('id' => $this->_data['TicketResponse']['ticket_id']));
     }
 }
Example #6
0
 protected function saveFiles($key, array $filenames)
 {
     $file = new File();
     $errors = array();
     $data = $_FILES[$key];
     $fields = $file->getFields();
     foreach ($filenames as $name) {
         $newdata = array();
         if (!empty($data['name'][$name])) {
             foreach ($fields as $fieldname => $field) {
                 if (isset($data[$fieldname][$name])) {
                     $newdata[$fieldname] = $data[$fieldname][$name];
                 }
             }
             $newdata['tmp_name'] = $data['tmp_name'][$name];
             $newdata['note'] = 'Image attached to ' . $key;
             ${$name} = File::Factory($newdata, $errors, new File());
             if (${$name} instanceof File) {
                 ${$name}->save();
             }
             $this->_data[$key][$name] = ${$name}->id;
         }
     }
 }