Example #1
0
 public static function create(Application $app, \record_adapter $record, $name, MediaInterface $media)
 {
     $databox = $record->get_databox();
     $connbas = $databox->get_connection();
     $path = $media->getFile()->getPath();
     $newname = $media->getFile()->getFilename();
     $params = [':path' => $path, ':file' => $newname, ':width' => 0, ':height' => 0, ':mime' => $media->getFile()->getMimeType(), ':size' => $media->getFile()->getSize(), ':dispatched' => 1];
     if (method_exists($media, 'getWidth') && null !== $media->getWidth()) {
         $params[':width'] = $media->getWidth();
     }
     if (method_exists($media, 'getHeight') && null !== $media->getHeight()) {
         $params[':height'] = $media->getHeight();
     }
     try {
         $sql = 'SELECT subdef_id FROM subdef
                 WHERE record_id = :record_id AND name = :name';
         $stmt = $connbas->prepare($sql);
         $stmt->execute([':record_id' => $record->get_record_id(), ':name' => $name]);
         $row = $stmt->fetch(PDO::FETCH_ASSOC);
         $stmt->closeCursor();
         if (!$row) {
             throw new \Exception_Media_SubdefNotFound('Require the real one');
         }
         $sql = "UPDATE subdef\n              SET path = :path, file = :file\n                  , width = :width , height = :height, mime = :mime\n                  , size = :size, dispatched = :dispatched, updated_on = NOW()\n              WHERE subdef_id = :subdef_id";
         $params[':subdef_id'] = $row['subdef_id'];
     } catch (\Exception_Media_SubdefNotFound $e) {
         $sql = "INSERT INTO subdef\n              (record_id, name, path, file, width\n                , height, mime, size, dispatched, created_on, updated_on)\n              VALUES (:record_id, :name, :path, :file, :width, :height\n                , :mime, :size, :dispatched, NOW(), NOW())";
         $params[':record_id'] = $record->get_record_id();
         $params[':name'] = $name;
     }
     $stmt = $connbas->prepare($sql);
     $stmt->execute($params);
     $stmt->closeCursor();
     $subdef = new self($app, $record, $name);
     $subdef->delete_data_from_cache();
     if ($subdef->get_permalink() instanceof media_Permalink_Adapter) {
         $subdef->get_permalink()->delete_data_from_cache();
     }
     if ($name === 'thumbnail') {
         $app['phraseanet.thumb-symlinker']->symlink($subdef->get_pathfile());
     }
     unset($media);
     return $subdef;
 }