/**
  * This checks a document into the repository.
  *
  * <code>
  * $ktapi = new KTAPI();
  * $session = $ktapi->start_system_session();
  * $document = $ktapi->get_document_by_id($documentid);
  * if($document->is_checked_out()){
  *     $document->checkin('filename.txt', 'Reason for checkin', '/tmp/filename');
  * }
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param string $filename The name of the file
  * @param string $reason The reason for checking the document in
  * @param string $tempfilename The location of the temporary file
  * @param bool $major_update Determines if the version number should have a major increment (+1) or a minor increment (+0.1)
  */
 function checkin($filename, $reason, $tempfilename, $major_update = false)
 {
     if (!is_file($tempfilename)) {
         return new PEAR_Error('File does not exist.');
     }
     $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
     if (PEAR::isError($user)) {
         return $user;
     }
     if (!$this->document->getIsCheckedOut()) {
         return new PEAR_Error(KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT);
     }
     $filename = KTUtil::replaceInvalidCharacters($filename);
     $options = array('major_update' => $major_update);
     $currentfilename = $this->document->getFileName();
     if ($filename != $currentfilename) {
         $options['newfilename'] = $filename;
     }
     DBUtil::startTransaction();
     $result = KTDocumentUtil::checkin($this->document, $tempfilename, $reason, $user, $options);
     if (PEAR::isError($result)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $result);
     }
     DBUtil::commit();
     KTUploadManager::temporary_file_imported($tempfilename);
 }
 /**
  * This adds a document to the current folder.
  *
  * <code>
  * $kt = new KTAPI();
  * $kt->start_session("admin", "admin");
  * $folder = $kt->get_folder_by_name("My New folder");
  * $res = $folder->add_document("Test Document", "test.txt", "Default", $tmpfname);
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param string $title This is the title for the file in the repository.
  * @param string $filename This is the filename in the system for the file.
  * @param string $documenttype This is the name or id of the document type. It first looks by name, then by id.
  * @param string $tempfilename This is a reference to the file that is accessible locally on the file system.
  * @return KTAPI_Document
  */
 function add_document($title, $filename, $documenttype, $tempfilename)
 {
     if (!is_file($tempfilename)) {
         return new PEAR_Error('File does not exist.');
     }
     $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_WRITE);
     if (PEAR::isError($user)) {
         return $user;
     }
     //KTS-4016: removed the replacing of special characters from the title as they should be allowed there
     //$title = KTUtil::replaceInvalidCharacters($title);
     $filename = basename($filename);
     $filename = KTUtil::replaceInvalidCharacters($filename);
     $documenttypeid = KTAPI::get_documenttypeid($documenttype);
     if (PEAR::isError($documenttypeid)) {
         $config = KTCache::getSingleton();
         $defaultToDefaultDocType = $config->get('webservice/useDefaultDocumentTypeIfInvalid', true);
         if ($defaultToDefaultDocType) {
             $documenttypeid = KTAPI::get_documenttypeid('Default');
         } else {
             return new KTAPI_DocumentTypeError('The document type could not be resolved or is disabled: ' . $documenttype);
         }
     }
     $options = array('contents' => new KTFSFileLike($tempfilename), 'temp_file' => $tempfilename, 'novalidate' => true, 'documenttype' => DocumentType::get($documenttypeid), 'description' => $title, 'metadata' => array(), 'cleanup_initial_file' => true);
     DBUtil::startTransaction();
     $document =& KTDocumentUtil::add($this->folder, $filename, $user, $options);
     if (PEAR::isError($document)) {
         DBUtil::rollback();
         return new PEAR_Error(KTAPI_ERROR_INTERNAL_ERROR . ' : ' . $document->getMessage());
     }
     DBUtil::commit();
     KTUploadManager::temporary_file_imported($tempfilename);
     return new KTAPI_Document($this->ktapi, $this, $document);
 }