/**
  * List all attachments, and manage attachments
  *
  * @param void
  * @return null
  */
 function attachments()
 {
     if (!$this->active_object->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
     }
     // if
     $attachemts = $this->active_object->getAttachments();
     $this->smarty->assign(array('attachments' => $attachemts));
     if ($this->request->isSubmitted()) {
         $async = (bool) $this->request->get('async');
         db_begin_work();
         $file = array_shift($_FILES);
         $this->active_object->attachUploadedFile($file, $this->logged_user);
         $save = $this->active_object->save();
         if ($save && !is_error($save)) {
             $attachment = Attachments::findLastByObject($this->active_object);
             if (instance_of($attachment, 'Attachment')) {
                 db_commit();
                 if ($async) {
                     $this->smarty->assign(array('_attachment' => $attachment, '_object_attachments_cycle_name' => 'object_attachments_cycle_' . $attachment->getId()));
                     // jQuery acts a bit weird here. Insted of providing response as
                     // a string it tries to append it to the BODY so some markup
                     // (tr, td) gets discarded. That is why we need to use temp table
                     // in order to get properly marked-up row
                     die('<table style="display: none">' . $this->smarty->fetch(get_template_path('_object_attachments_row', 'attachments', RESOURCES_MODULE))) . '</table>';
                 } elseif ($this->request->isApiCall()) {
                     $this->serveData($attachment, 'attachment');
                 } else {
                     flash_success('File ":file" has been added', array('file' => $file['name']));
                     $this->redirectToUrl($this->active_object->getAttachmentsUrl());
                 }
                 // if
             }
             // if
         }
         // if
         db_rollback();
         if ($async) {
             $this->httpError(HTTP_ERR_OPERATION_FAILED);
         } elseif ($this->request->isApiCall()) {
             $this->httpError(HTTP_ERR_OPERATION_FAILED, null, true, true);
         } else {
             flash_error('File ":file" has not been added', array('file' => $file['name']));
             $this->redirectToUrl($this->active_object->getAttachmentsUrl());
         }
         // if
     } else {
         if ($this->request->isApiCall()) {
             $this->serveData($attachemts, 'attachments');
         }
         // if
     }
     // if
 }
예제 #2
0
/**
 * Attach files from $_FILES to $to object
 * 
 * Returns number of attached files or an errors
 *
 * @param ProjectObject $to
 * @param User $user
 * @return integer
 */
function attach_from_files(&$to, $user)
{
    $attached = 0;
    if (is_foreachable($_FILES)) {
        foreach ($_FILES as $file) {
            $attach = $to->attachUploadedFile($file, $user);
            if ($attach && !is_error($attach)) {
                $attached++;
            }
            // if
        }
        // foreach
    }
    // if
    return $attached;
}