コード例 #1
0
 function store($object_id)
 {
     global $AppUI, $db, $_FILES, $m, $_POST;
     $file_uploaded = false;
     // instantiate the file object and eventually load exsiting file data
     $obj = new CFile();
     if ($_POST[$this->field_name . '_id']) {
         $obj->load($_POST[$this->field_name . '_id']);
         // create an old object for the case that
         // the file must be replaced
         if ($_POST[$this->field_name . '_id'] > 0) {
             $oldObj = new CFile();
             $oldObj->load($_POST[$this->field_name . '_id']);
         }
     }
     // if the cf lives in the projects module
     // affiliate the file to the suitable project
     if ($m == 'projects' && !empty($_POST['project_id'])) {
         $obj->file_project = $_POST['project_id'];
     }
     // todo: implement task affiliation here, too
     $upload = null;
     if (isset($_FILES[$this->field_name])) {
         $upload = $_FILES[$this->field_name];
         if ($upload['size'] > 0) {
             // store file with a unique name
             $obj->file_name = $upload['name'];
             $obj->file_type = $upload['type'];
             $obj->file_size = $upload['size'];
             $obj->file_date = str_replace("'", '', $db->DBTimeStamp(time()));
             $obj->file_real_filename = uniqid(rand());
             $obj->file_owner = $AppUI->user_id;
             $obj->file_version++;
             $obj->file_version_id = $obj->file_id;
             $res = $obj->moveTemp($upload);
             if ($res) {
                 $file_uploaded = true;
             }
             if ($msg = $obj->store()) {
                 $AppUI->setMsg($msg, UI_MSG_ERROR);
             } else {
                 // reset the cf field_name to the file_id
                 $this->setValue($obj->file_id);
             }
         }
     }
     // Delete the existing (old) file in case of file replacement
     // (through addedit not through c/o-versions)
     if ($_POST[$this->field_name . '_id'] && $upload['size'] > 0 && $file_uploaded) {
         $oldObj->deleteFile();
     }
     if ($upload['size'] > 0 && $file_uploaded) {
         return parent::store($object_id);
     } else {
         if ($upload['size'] > 1 && !$file_uploaded) {
             $AppUI->setMsg('File could not be stored!', UI_MSG_ERROR, true);
             return true;
         }
     }
 }