/**
  * Public function to add a document.
  * 
  * @param HTTP::procedure_id	the procedure id
  * @return unknown_type
  */
 public function add()
 {
     // Get target ids
     $procedure_id = intval(phpgw::get_var('procedure_id'));
     $data = array();
     // Check permissions if procedure id is set
     if (isset($procedure_id) && $procedure_id > 0) {
         //Load procedure
         $procedure = $this->so_procedure->get_single($procedure_id);
     }
     // If no contract or party is loaded
     if (!isset($procedure)) {
         $data['error'] = lang('error_no_procedure');
         $this->render('permission_denied.php', $data);
         return;
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (!$this->add && !$this->edit) {
             phpgwapi_cache::message_set('No access', 'error');
             $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uidocument.show', 'procedure_id' => $procedure->get_id(), 'tab' => 'documents'));
         }
         //Create a document object
         $document = new controller_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'));
         $desc = phpgw::get_var('document_description', 'html');
         $desc = str_replace(" ", " ", $desc);
         $document->set_description($desc);
         $document->set_procedure_id($procedure_id);
         //Retrieve the document properties
         $document_properties = $this->get_type_and_id($document);
         // Move file from temporary storage to vfs
         $result = $this->so->write_document_to_vfs($document_properties['document_type'], $_FILES["file_path"]["tmp_name"], $document_properties['id'], $_FILES["file_path"]["name"]);
         if ($result) {
             if ($this->so->store($document)) {
                 if (isset($procedure)) {
                     $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uidocument.show', 'procedure_id' => $procedure->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, '', '');
         }
     }
 }
 function populate(int $document_id, &$document)
 {
     $document_id = (int) $document_id;
     if ($document == null) {
         $document = new controller_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_procedure_id($this->unmarshal($this->db->f('procedure_id'), 'int'));
     }
     return $document;
 }
 /**
  * Add a document to this procedure. This method does not check if
  * object is already added and does not do any db handling.
  * 
  * @param $new_document
  */
 public function add_document(controller_document $new_document)
 {
     $new_document_id = $new_document->get_id();
     if (!in_array($new_document_id, $this->documents)) {
         $this->documents[$new_document_id] = $new_document;
     }
 }
 function get_single_with_documents($id, $return_type = "return_object")
 {
     $id = (int) $id;
     $counter = 0;
     $documents = null;
     $joins .= " {$this->left_join} controller_document ON (p.id = controller_document.procedure_id)";
     $sql = "SELECT p.*, controller_document.id AS document_id, controller_document.title AS document_title, controller_document.description as document_description FROM controller_procedure p {$joins} WHERE p.id = " . $id;
     //var_dump($sql);
     $this->db->query($sql, __LINE__, __FILE__);
     while ($this->db->next_record()) {
         if (!$counter) {
             $procedure = new controller_procedure($this->unmarshal($this->db->f('id'), 'int'));
             $procedure->set_title($this->unmarshal($this->db->f('title', true), 'string'));
             $procedure->set_purpose($this->unmarshal($this->db->f('purpose', true), 'string'));
             $procedure->set_responsibility($this->unmarshal($this->db->f('responsibility', true), 'string'));
             $procedure->set_description($this->unmarshal($this->db->f('description', true), 'string'));
             $procedure->set_reference($this->unmarshal($this->db->f('reference', true), 'string'));
             $procedure->set_attachment($this->unmarshal($this->db->f('attachment', true), 'string'));
             $procedure->set_start_date($this->unmarshal($this->db->f('start_date'), 'int'));
             $procedure->set_end_date($this->unmarshal($this->db->f('end_date'), 'int'));
             $procedure->set_procedure_id($this->unmarshal($this->db->f('procedure_id'), 'int'));
             $procedure->set_revision_no($this->unmarshal($this->db->f('revision_no'), 'int'));
             $procedure->set_revision_date($this->unmarshal($this->db->f('revision_date'), 'int'));
             $procedure->set_control_area_id($this->unmarshal($this->db->f('control_area_id', 'int')));
             $category = execMethod('phpgwapi.categories.return_single', $this->unmarshal($this->db->f('control_area_id', 'int')));
             $procedure->set_control_area_name($category[0]['name']);
         }
         if ($this->db->f('document_id')) {
             $document = new controller_document($this->unmarshal($this->db->f('document_id'), 'int'));
             $document->set_procedure_id($procedure->get_id());
             $document->set_title($this->unmarshal($this->db->f('document_title', true), 'string'));
             $document->set_description($this->unmarshal($this->db->f('document_description', true), 'string'));
             if ($return_type == "return_array") {
                 $doc_as_array = $document->toArray();
                 $doc_as_array['document_link'] = controller_uidocument::link(array('menuaction' => 'controller.uidocument.view', 'id' => $document->get_id()));
                 //_debug_array($doc_as_array);
                 $documents_array[] = $doc_as_array;
             } else {
                 $documents_array[] = $document;
             }
         }
         $counter++;
     }
     if ($procedure != null) {
         $procedure->set_documents($documents_array);
         if ($return_type == "return_array") {
             return $procedure->toArray();
         } else {
             return $procedure;
         }
     } else {
         return null;
     }
 }