function populate(int $document_id, &$document)
 {
     if ($document == null) {
         $document = new rental_document($document_id);
         $document->set_title($this->unmarshal($this->db->f('document_title', true), 'string'));
         $document->set_description($this->unmarshal($this->db->f('description', true), 'string'));
         $document->set_name($this->unmarshal($this->db->f('name', true), 'string'));
         $document->set_type($this->unmarshal($this->db->f('type_title', true), 'string'));
         $document->set_contract_id($this->unmarshal($this->db->f('contract_id', true), 'int'));
         $document->set_party_id($this->unmarshal($this->db->f('party_id', true), 'int'));
     }
     return $document;
 }
 /**
  * Public function to add a document.
  * 
  * @param HTTP::contract_id	the contract id
  * @param HTTP::party_id	the party id
  * @return unknown_type
  */
 public function add()
 {
     // Get target ids
     $contract_id = intval(phpgw::get_var('contract_id'));
     $party_id = intval(phpgw::get_var('party_id'));
     $data = array();
     // Check permissions if contract id is set
     if (isset($contract_id) && $contract_id > 0) {
         //Load contract
         $contract = rental_socontract::get_instance()->get_single($contract_id);
         if (!$contract->has_permission(PHPGW_ACL_EDIT)) {
             $data['error'] = lang('permission_denied_add_document');
             $this->render('permission_denied.php', $data);
             return;
         }
     }
     // Check permissions if party id is set
     if (isset($party_id) && $party_id > 0) {
         //Load party
         $party = rental_soparty::get_instance()->get_single($party_id);
         if (!($this->isAdministrator() || $this->isExecutiveOfficer())) {
             $data['error'] = lang('permission_denied_add_document');
             $this->render('permission_denied.php', $data);
             return;
         }
     }
     // If no contract or party is loaded
     if (!(isset($party) || isset($contract))) {
         $data['error'] = lang('error_no_contract_or_party');
         $this->render('permission_denied.php', $data);
         return;
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         //Create a document object
         $document = new rental_document();
         $document->set_title(phpgw::get_var('document_title'));
         $document->set_name($_FILES["file_path"]["name"]);
         $document->set_type_id(phpgw::get_var('document_type'));
         $document->set_contract_id($contract_id);
         $document->set_party_id($party_id);
         //Retrieve the document properties
         $document_properties = $this->get_type_and_id($document);
         // Move file from temporary storage to vfs
         $result = rental_sodocument::get_instance()->write_document_to_vfs($document_properties['document_type'], $_FILES["file_path"]["tmp_name"], $document_properties['id'], $_FILES["file_path"]["name"]);
         if ($result) {
             if (rental_sodocument::get_instance()->store($document)) {
                 if (isset($party)) {
                     $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uiparty.edit', 'id' => $party->get_id(), 'tab' => 'documents'));
                 } else {
                     if (isset($contract)) {
                         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract->get_id(), 'tab' => 'documents'));
                     }
                 }
             } else {
                 // Handle failure on storing document
                 $this->redirect($document, $document_propeties, '', '');
             }
         } else {
             //Handle vfs failure to store document
             $this->redirect($document, $document_propeties, '', '');
         }
     }
 }
 public function savePDFToContract($file, $contract_id, $title)
 {
     //Create a document object
     $document = new rental_document();
     $document->set_title($title);
     $document->set_name("Kontrakt_" . strtotime(date('Y-m-d')) . ".pdf");
     $document->set_type_id(1);
     $document->set_contract_id($contract_id);
     $document->set_party_id(NULL);
     //Retrieve the document properties
     $document_properties = $this->get_type_and_id($document);
     // Move file from temporary storage to vfs
     $result = rental_sodocument::get_instance()->write_document_to_vfs($document_properties['document_type'], $file, $document_properties['id'], "Kontrakt_" . strtotime(date('Y-m-d')) . ".pdf");
     if ($result) {
         if (rental_sodocument::get_instance()->store($document)) {
             $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'rental.uicontract.edit', 'id' => $contract_id, 'tab' => 'documents'));
         } else {
             // Handle failure on storing document
             $this->redirect($document, $document_properties, '', '');
         }
     } else {
         //Handle vfs failure to store document
         $this->redirect($document, $document_properties, '', '');
     }
     return false;
 }