/** * Create a new document using the given file and metadata. * This method makes sure that there are no inconsistencies between a real * file and its database entry. Only if the file were copied/moved to the * documents folder, the database entry is written. If this fails too, the * file will be unlinked again. * The first parameter can either be an uploaded file or the path to an * already existing one. This file will either be moved using * move_uploaded_file or it will be copied. * The destination is determined this way: If the second parameter $data * already contains a "dokument_id", this will be used as the file's * destination. This is usually the case when refreshing a file. * If there is no such parameter, a new "dokument_id" is generated as usual * and is used as the file's destination. * * Before a document (and its file) is created, the notification * "DocumentWillCreate" will be posted. * If the document was created successfuly, the notification * "DocumentDidCreate" will be posted. * It the document was updated rather than created (see above), the * notifications will be "DocumentWillUpdate" and "DocumentDidUpdate". * The subject of the notification will always be that document. * * @param $file string full path to a file (either uploaded or already existing) * @param $data array an array containing the metadata of the document; * just use the same way as StudipDocument::setData * @return StudipDocument|null if successful the created document, null otherwise */ static function createWithFile($file, $data) { $doc = new StudipDocument(@$data['dokument_id']); $doc->setData($data); // create new ID (and thus path) if (!$doc->getId()) { $doc->setId($doc->getNewId()); } $notifications = !isset($data['dokument_id']) ? array('DocumentWillCreate', 'DocumentDidCreate') : array('DocumentWillUpdate', 'DocumentDidUpdate'); // send DocumentWill(Create|Update) notification NotificationCenter::postNotification($notifications[0], $doc); if (!$doc->attachFile($file) || !$doc->safeStore()) { return null; } // send DocumentDid(Create|Update) notification NotificationCenter::postNotification($notifications[1], $doc); return $doc; }