function populate() { $module = DataObjectFactory::Factory('ModuleObject'); $module->loadBy('name', $_GET['module']); $files = new EntityAttachmentCollection(); $files->setParams(); $pl = new PageList(' Documents'); $sh = new SearchHandler($files, FALSE); $fields = array('id', 'file as document', 'revision', 'note', 'file_id'); $sh->setOrderBy('file'); $sh->setFields($fields); $sh->addConstraint(new Constraint('entity_id', '=', $module->id)); $sh->addConstraint(new Constraint('data_model', '=', 'moduleobject')); $this->setSearchLimit($sh); $files->load($sh); $this->contents = $files; $ao = AccessObject::Instance(); $this->contents->can_upload = $ao->hasPermission($_GET['module'], 'attachments', 'new'); }
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); }
private function getAttachments() { $attachments = new EntityAttachmentCollection(); $sh = new SearchHandler($attachments, FALSE); $sh->addConstraint(new Constraint('data_model', '=', 'modulecomponent')); $sh->addConstraint(new Constraint('entity_id', '=', ModuleComponent::getComponentId($this->_modules['module'], strtolower(get_class($this))))); $sh->addConstraint(new Constraint('createdby', '=', EGS_USERNAME)); $files = $attachments->load($sh, null, RETURN_ROWS); $dirobjs = array(); if (count($files) > 0) { foreach ($files as $attachment) { $link = '/?' . setParamsString(array('modules' => $this->_modules, 'controller' => 'attachments', 'action' => 'view_file', 'other' => array(file_id => $attachment['file_id']))); $details = array('name' => $attachment['file'], 'link' => $link, 'type' => 'attachment', 'delete' => array('modules' => $this->_modules, 'controller' => 'attachments', 'action' => 'delete', 'id' => $attachment['id']), 'size' => sizify($attachment['size']), 'mtime' => un_fix_date($attachment['lastupdated'])); $dirobjs['file'][] = $details; } } return $dirobjs; }