Album.php 39.6 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20 21 22 23
 */
/**
 * utlisation Storm :
 * - récupérer tous les Albums
continuum's avatar
continuum committed
24
 * $albums = Class_Album::findAll();
25 26
 *
 * - récupérer album avec l'id 2
continuum's avatar
continuum committed
27
 * $album = Class_Album::find(2);
28 29
 *
 * - tous les albums de la catégorie 3
continuum's avatar
continuum committed
30
 * $albums = Class_Album::findAllBy(array('cat_id' => 3));
31 32 33
 *
 * ou bien
 *
continuum's avatar
continuum committed
34
 * $albums = Class_AlbumCategorie::find(3)->getAlbums()
35 36 37 38 39 40 41 42 43
 *
 * - modifier le libelle d'un album et sauver:
 * $album->setLibelle('Mon album')->save();
 *
 * - modifier les attributs en masse
 * $post = array('libelle' => 'Mon album', 'commentaire' => 'bla bla');
 * $album->updateAttributes($post)->save()
 *
 * - Récupérer l'album id 4 et afficher son libelle
continuum's avatar
continuum committed
44
 * echo Class_Album::find(4)->getLibelle();
45 46
 *
 * - Créer une nouvelle catégorie, son album et sauvegarder.
continuum's avatar
continuum committed
47
 * Class_AlbumCategorie::newInstance()
48
 *    ->setLibelle('Mes Favoris')
continuum's avatar
continuum committed
49
 *    ->addAlbum(Class_Album::newInstance()
Patrick Barroca's avatar
Patrick Barroca committed
50
 *                     ->setLibelle('Mes BD')
51 52 53 54 55
 *                     ->setCommentaire('bla bla'))
 *    ->save();
 *
 */

56
class AlbumLoader extends Storm_Model_Loader {
57 58 59
  use Trait_MemoryCleaner;


Patrick Barroca's avatar
Patrick Barroca committed
60 61
  public function getItemsOf($categoryId) {
    return Class_Album::findAllBy([
62 63 64 65
                                   'cat_id' => Class_AlbumCategorie::getAllCatAndSubCat([$categoryId]),
                                   'order' => 'titre',
                                   'limit' => 1000
                                   ]);
Patrick Barroca's avatar
Patrick Barroca committed
66
  }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83


  public function findFirstWithPosterBy($params) {
    while($albums = Class_Album::findAllBy(array_merge($params,
                                                       ['limitPage'=> [($page = 1), 100]]))) {

      if ($album = (new Storm_Model_Collection($albums))
        ->detect(function($model)
                 {
                   return $model->hasPoster();
                 }))
        return $album;

      $this->_cleanMemory();
      $page++;
    }
  }
84 85 86 87 88 89 90 91


  public function findAllAfter($id, $date_maj, $limit=100) {
    $where = "id > " . $id . " and date_maj >= '" . $date_maj . "'";
    return Class_Album::findAllBy(['where' => $where,
                                   'order' => 'id',
                                   'limit' => $limit]);
  }
92 93 94
}


95 96


97
class Class_Album extends Storm_Model_Abstract {
98
  use Trait_TimeSource, Trait_Indexable, Trait_StaticFileSystem, Trait_Translator, Trait_TreeViewableItem;
99

Patrick Barroca's avatar
Patrick Barroca committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  const BASE_PATH     = 'album/';
  const THUMBS_PATH   = 'thumbs/';
  const ORIGINAL_PATH = 'big/';
  const ANNEE_MIN = 800;
  const DEFAULT_CODE_LANGUE = 'fre';
  const VIDEO_URL_FIELD = '856';
  const VIDEO_URL_TYPE = 'video';
  const DURATION_FIELD = '215$a';
  const URI_TYPE_POSTER = 'poster';
  const URI_TYPE_TRAILER = 'trailer';
  const URI_TYPE_PHOTO = 'photo';
  const URI_TYPE_THUMNAIL = 'thumbnail';
  const URI_TYPE_EXTERNAL_URI = 'external_uri';
  const AUTHOR_FIELD = '701';
  const EDITOR_FIELD = '210';
  const COLLECTION_FIELD = '225';
  const DISTRIBUTOR_FIELD = '306$a';
  const THUMB_PREFIX = 'thumb_';

  const STATUS_DRAFT = 1;
  const STATUS_VALIDATED = 3;

122 123
  const RIGHT_PUBLIC_DOMAIN_KEY = 1;
  const RIGHT_OTHER_KEY = 2;
Patrick Barroca's avatar
Patrick Barroca committed
124 125

  protected static $DEFAULT_THUMBNAIL_VALUES;
126 127 128

  protected $_table_name = 'album';
  protected $_table_primary = 'id';
Patrick Barroca's avatar
Patrick Barroca committed
129 130 131 132
  protected $_loader_class = 'AlbumLoader';
  protected $_belongs_to = ['categorie' => ['model' => 'Class_AlbumCategorie',
                                            'referenced_in' => 'cat_id'],
                            'type_doc' => ['model' => 'Class_TypeDoc'],
133

Patrick Barroca's avatar
Patrick Barroca committed
134
                            'langue' => ['model' => 'Class_CodifLangue',
135
                                         'referenced_in' => 'id_langue']];
Patrick Barroca's avatar
Patrick Barroca committed
136 137 138 139 140 141 142 143 144 145

  protected $_has_many = ['ressources' => ['model' => 'Class_AlbumRessource',
                                           'role'       => 'album',
                                           'dependents' => 'delete',
                                           'order'        => 'ordre'],

                          'items' => ['model' => 'Class_Album_Item',
                                      'role' => 'album',
                                      'dependents' => 'delete']];

146 147
  protected $_default_attribute_values = ['notice_id' => null,
                                          'titre' => '',
Patrick Barroca's avatar
Patrick Barroca committed
148 149 150 151 152 153 154 155
                                          'sous_titre' => '',
                                          'fichier' => '',
                                          'pdf' => '',
                                          'description' => '',
                                          'date_maj' => '',
                                          'genre' => '',
                                          'matiere' => '',
                                          'type_doc_id' => Class_TypeDoc::DIAPORAMA,
156
                                          'created_at' => '',
Patrick Barroca's avatar
Patrick Barroca committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
                                          'annee' => '',
                                          'id_langue' => self::DEFAULT_CODE_LANGUE,
                                          'id_origine' => '',
                                          'cat_id' => 0,
                                          'cfg_thumbnails' => '',
                                          'provenance' => '',
                                          'cote' => '',
                                          'notes' => '',
                                          'visible' => true,
                                          'droits' => '',
                                          'nature_doc' => '',
                                          'bibliotheques' => '',
                                          'annexes' => '',
                                          'sections' => '',
                                          'status' => self::STATUS_DRAFT,
efalcy's avatar
efalcy committed
172 173
                                          'pcdm4' => '',
                                          'tags' => ''];
Patrick Barroca's avatar
Patrick Barroca committed
174 175 176


  /** @var Class_Upload */
177
  protected $_uploadHandler = [];
Patrick Barroca's avatar
Patrick Barroca committed
178 179 180 181 182

  protected $_marc;

  protected $path_flash;

183 184
  protected $_date_maj_enabled = true;

185

186 187 188 189 190 191
  public function __construct() {
    parent::__construct();
    $this->setCreatedAt(static::getCurrentDate());
  }


192 193 194 195 196 197
  public function describeAssociationsOn($associations) {
		$associations
			->add(new Storm_Model_Association_HasOne('notice', ['model' => 'Class_Notice',
																													'referenced_in' => 'notice_id']));
	}

198

Patrick Barroca's avatar
Patrick Barroca committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  /**
   * @return array
   */
  public static function thumbnailAttributeKeys() {
    return array_keys(self::getDefaultThumbnailValues());
  }


  /**
   * @return array
   */
  public static function getDefaultThumbnailValues() {
    if (!isset(self::$DEFAULT_THUMBNAIL_VALUES))
      self::$DEFAULT_THUMBNAIL_VALUES = ['thumbnail_width' => 400,
                                         'thumbnail_left_page_crop_top' => 0,
                                         'thumbnail_left_page_crop_right' => 0,
                                         'thumbnail_left_page_crop_bottom' => 0,
                                         'thumbnail_left_page_crop_left' => 0,
                                         'thumbnail_right_page_crop_top' => 0,
                                         'thumbnail_right_page_crop_right' => 0,
                                         'thumbnail_right_page_crop_bottom' => 0,
                                         'thumbnail_right_page_crop_left' => 0,
                                         'thumbnail_crop_top' => 0,
                                         'thumbnail_crop_right' => 0,
                                         'thumbnail_crop_bottom' => 0,
                                         'thumbnail_crop_left' => 0,
                                         'op_hauteur_boite' => 400,
                                         'display_one_page' => false];
    return self::$DEFAULT_THUMBNAIL_VALUES;
  }


  public function getMarc() {
    if (!isset($this->_marc))
      $this->_marc = new Class_Album_Marc($this->getNotes());

    return $this->_marc;
  }


  /**
   * @return array
   */
  public static function getKnownStatus() {
243 244 245
    $album = new Class_Album;
    return [self::STATUS_DRAFT => $album->_('Brouillon'),
            self::STATUS_VALIDATED => $album->_('Validé')];
Patrick Barroca's avatar
Patrick Barroca committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  }


  /**
   * @return bool
   */
  public function isMonopage() {
    return (bool)$this->getDisplayOnePage();
  }


  /**
   * @return bool
   */
  public function beMonopage() {
    return $this->setDisplayOnePage(true);
  }


  /**
   * @param Class_Matiere $matiere
   * @return Class_Album
   */
  public function addMatiere($matiere) {
    $matieres = explode(';', $this->getMatiere());
    $matieres []= $matiere->getId();
    return $this->setMatiere(implode(';', array_unique(array_filter($matieres))));
  }


  /**
   * @return int
   */
  public function getRessourcesCount() {
    return $this->numberOfRessources();
  }


  /**
   * @param string $param
   * @return bool
   */
  public function hasDefaultValue($param) {
    return array_key_exists($param, self::getDefaultThumbnailValues());
  }


  public function setISBN($isbn) {
294
    $this->getMarc()->addZone('10', ['a' => $isbn]);
Patrick Barroca's avatar
Patrick Barroca committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    return $this;
  }


  public function getISBN() {
    $values = $this->getMarc()->getSubfield('10', 'a');
    return end($values);
  }


  /**
   * @return array
   */
  public function getPermalink() {
    $permalink = ['module' => 'opac',
                  'controller' => 'bib-numerique',
311 312
                  'action' => 'notice',
                  'id' => $this->getId()];
Patrick Barroca's avatar
Patrick Barroca committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    return $permalink;
  }


  /**
   * @return array
   */
  public function getPermalinkThumbnail() {
    $permalink = $this->getPermalink();
    $permalink['action'] = 'notice-thumbnail';
    return $permalink;
  }


  /**
   * @return int
   */
  public function indexOfRessourceByFolio($folio) {
    $pageno = 1;
    $ressources = $this->getRessources();
    foreach($ressources as $ressource) {
      $pageno ++;
      if ($ressource->getFolio() == $folio)
        return $pageno;
    }
    return 0;
  }


  /**
   * @return int
   */
  public function getRessourceByFolio($folio) {
    $ressources = $this->getRessources();
    foreach($ressources as $ressource) {
      if ($ressource->getFolio() == $folio)
        return $ressource;
    }
    return null;
  }


  /**
   * @param string $param
   * @return mixed
   */
  public function getDefaultValue($param) {
    $default_values = self::getDefaultThumbnailValues();
    return $default_values[$param];
  }


  /**
   * Si un attribut n'est pas trouvé, regarde s'il n'est pas dans cfgThumbnails (paramètres des vignettes).
   * @param string $field
   * @return mixed
   */
  public function _get($field) {
    if ($field !== 'cfg_thumbnails' and
        array_key_exists($field, $this->getThumbnailAttributes()))
      return $this->getThumbnailAttribute($field);

    if ($this->isAttributeExists($field))
      return parent::_get($field);

    if ($this->hasDefaultValue($field))
      return $this->getDefaultValue($field);

381
    throw new Storm_Model_Exception($this->_('Tried to call unknown method Class_Album::get%s',
Patrick Barroca's avatar
Patrick Barroca committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
                                            $this->attributeNameToAccessor($field)));
  }


  /**
   * @param string $field
   * @param mixed $value
   * @return Class_Album
   * @see _get
   */
  public function _set($field, $value) {
    if (in_array($field, self::thumbnailAttributeKeys()))
      return $this->setThumbnailAttribute($field, $value);
    return parent::_set($field, $value);
  }


  public function getThumbnailAttribute($attribute) {
    $cfg_thumbnails = $this->getThumbnailAttributes();
    if (!array_key_exists($attribute, $cfg_thumbnails))
      return $this->getDefaultValue($attribute);
    return $cfg_thumbnails[$attribute];
  }


  /** @return string */
  public function getLibelle() {
    return $this->getTitre();
  }


  /** @return array */
  public function getHierarchy() {
    $hierarchy = [];
    $this->getCategorie()->getHierarchyOn($hierarchy);
    return $hierarchy;
  }


  /** @return boolean */
  public function isLivreNumerique() {
    return $this->getTypeDocId() == Class_TypeDoc::LIVRE_NUM;
  }


  /** @return boolean */
  public function isDiaporama() {
    return $this->getTypeDocId() == Class_TypeDoc::DIAPORAMA;
  }


  public function beLivreNumerique() {
    return $this->setTypeDocId(Class_TypeDoc::LIVRE_NUM);
  }


  public function beDiaporama() {
    return $this->setTypeDocId(Class_TypeDoc::DIAPORAMA);
  }


  public function beEPUB() {
    return $this->setTypeDocId(Class_TypeDoc::EPUB);
  }
446 447


Patrick Barroca's avatar
Patrick Barroca committed
448 449 450
  public function beOAI() {
    return $this->setTypeDocId(Class_TypeDoc::OAI);
  }
451 452


Patrick Barroca's avatar
Patrick Barroca committed
453 454 455
  public function isOAI() {
    return $this->getTypeDocId() == Class_TypeDoc::OAI;
  }
456 457


Patrick Barroca's avatar
Patrick Barroca committed
458 459 460
  public function beArteVOD() {
    return $this->setTypeDocId(Class_TypeDoc::ARTEVOD);
  }
461 462


Patrick Barroca's avatar
Patrick Barroca committed
463 464 465
  public function beNumilog() {
    return $this->setTypeDocId(Class_TypeDoc::NUMILOG);
  }
466 467


Patrick Barroca's avatar
Patrick Barroca committed
468 469 470
  public function beCyberlibris() {
    return $this->setTypeDocId(Class_TypeDoc::CYBERLIBRIS);
  }
471 472


473 474 475 476 477
  public function beCiteDeLaMusique() {
    return $this->setTypeDocId(Class_TypeDoc::CITEDELAMUSIQUE);
  }


478 479 480 481 482
  public function isCiteDeLaMusique() {
    return Class_TypeDoc::CITEDELAMUSIQUE == $this->getTypeDocId();
  }


Patrick Barroca's avatar
Patrick Barroca committed
483 484 485
  public function beNumeriquePremium() {
    return $this->setTypeDocId(Class_TypeDoc::NUMERIQUEPREMIUM);
  }
486 487


Patrick Barroca's avatar
Patrick Barroca committed
488 489 490
  public function isNumilog() {
    return $this->getTypeDocId() == Class_TypeDoc::NUMILOG;
  }
491

492

Patrick Barroca's avatar
Patrick Barroca committed
493 494 495
  public function isDilicom() {
    return $this->getTypeDocId() == Class_TypeDoc::DILICOM;
  }
496

497

Patrick Barroca's avatar
Patrick Barroca committed
498 499 500
  public function isCyberlibris() {
    return $this->getTypeDocId() == Class_TypeDoc::CYBERLIBRIS;
  }
501 502


Patrick Barroca's avatar
Patrick Barroca committed
503 504 505
  public function beFormationVodeclic() {
    return $this->setTypeDocId(Class_TypeDoc::VODECLIC);
  }
506 507


Patrick Barroca's avatar
Patrick Barroca committed
508 509 510
  public function isFormationVodeclic() {
    return $this->getTypeDocId() == Class_TypeDoc::VODECLIC;
  }
511

512

Patrick Barroca's avatar
Patrick Barroca committed
513 514 515
  public function isEPUB() {
    return $this->getTypeDocId() == Class_TypeDoc::EPUB;
  }
516

517

Patrick Barroca's avatar
Patrick Barroca committed
518
  public function isAudioRecord() {
519
    return in_array($this->getTypeDocId(),
520 521 522
                    [Class_TypeDoc::JAMENDO,
                     Class_TypeDoc::AUDIO_RECORD,
                     Class_TypeDoc::SOUNDCLOUD]);
Patrick Barroca's avatar
Patrick Barroca committed
523
  }
524

525

Patrick Barroca's avatar
Patrick Barroca committed
526 527 528
  public function isArteVOD() {
    return $this->getTypeDocId() == Class_TypeDoc::ARTEVOD;
  }
529

530

Patrick Barroca's avatar
Patrick Barroca committed
531 532 533
  public function isJamendo() {
    return $this->getTypeDocId() == Class_TypeDoc::JAMENDO;
  }
534

535

536 537 538 539 540
  public function isSoundCloud() {
    return $this->getTypeDocId() == Class_TypeDoc::SOUNDCLOUD;
  }


Patrick Barroca's avatar
Patrick Barroca committed
541 542 543
  public function isNumeriquePremium() {
    return $this->getTypeDocId() == Class_TypeDoc::NUMERIQUEPREMIUM;
  }
Laurent's avatar
Laurent committed
544

545

Patrick Barroca's avatar
Patrick Barroca committed
546 547 548 549
  public function beOrphea() {
    $this->setTypeDocId(Class_TypeDoc::ORPHEA);
    return $this;
  }
Laurent's avatar
Laurent committed
550

551

Patrick Barroca's avatar
Patrick Barroca committed
552 553 554
  public function isOrphea() {
    return $this->getTypeDocId() == Class_TypeDoc::ORPHEA;
  }
Laurent's avatar
Laurent committed
555

556

557 558 559 560 561
  public function isWebSite() {
    return $this->getTypeDocId() == Class_TypeDoc::WEBSITE;
  }


Patrick Barroca's avatar
Patrick Barroca committed
562 563 564 565 566 567
  /**
   * @param Zend_Controller_Request_Http $request
   * @return array
   */
  public function addFile($request) {
    return Class_AlbumRessource::newInstance()
568 569
      ->setAlbum($this)
      ->initializeWith($request);
Patrick Barroca's avatar
Patrick Barroca committed
570
  }
571

572

Patrick Barroca's avatar
Patrick Barroca committed
573 574 575 576 577 578 579 580
  /**
   * @param string $prefix
   * @return string
   */
  public function getBase($prefix) {
    return str_replace('//', '/',
                       $prefix.'/'.self::BASE_PATH.'/'.$this->getId().'/');
  }
581 582


Patrick Barroca's avatar
Patrick Barroca committed
583 584 585 586 587 588
  /**
   * @return string
   */
  public function getBaseUrl() {
    return $this->getBase(USERFILESURL);
  }
589

590

Patrick Barroca's avatar
Patrick Barroca committed
591 592 593 594 595 596 597 598
  /**
   * @return String
   */
  public function getIdLangue() {
    if (!$id_langue = $this->_get('id_langue'))
      return self::DEFAULT_CODE_LANGUE;
    return $id_langue;
  }
599 600


Patrick Barroca's avatar
Patrick Barroca committed
601 602 603 604 605 606
  /**
   * @return Class_CodifLangue
   */
  public function getLangue() {
    return Class_CodifLangue::find($this->getIdLangue());
  }
607 608


Patrick Barroca's avatar
Patrick Barroca committed
609 610 611 612 613 614 615
  /**
   * @param string $prefix
   * @return string
   */
  public function getThumbnails($prefix) {
    return $prefix . self::THUMBS_PATH;
  }
616 617


Patrick Barroca's avatar
Patrick Barroca committed
618 619 620 621 622 623
  /**
   * @return string
   */
  public function getThumbnailsPath() {
    return $this->getThumbnails($this->getBasePath());
  }
Laurent's avatar
Laurent committed
624 625


Patrick Barroca's avatar
Patrick Barroca committed
626 627 628 629 630 631
  /**
   * @return string
   */
  public function getThumbnailsUrl() {
    return $this->getThumbnails($this->getBaseUrl());
  }
632 633


Patrick Barroca's avatar
Patrick Barroca committed
634 635 636 637 638 639 640
  /**
   * @param string $prefix
   * @return string
   */
  public function getOriginals($prefix) {
    return $prefix . self::ORIGINAL_PATH;
  }
641 642


Patrick Barroca's avatar
Patrick Barroca committed
643 644 645 646 647 648
  /**
   * @return string
   */
  public function getOriginalsPath() {
    return $this->getOriginals($this->getBasePath());
  }
649 650


Patrick Barroca's avatar
Patrick Barroca committed
651 652 653 654 655 656
  /**
   * @return string
   */
  public function getOriginalsUrl() {
    return $this->getOriginals($this->getBaseUrl());
  }
657 658


Patrick Barroca's avatar
Patrick Barroca committed
659
  public function deleteVignette() {
660 661 662 663 664 665
    if (!$this->getFichier())
      return;

    $this->getFileSystem()->unlink($this->getVignettePath());
    $this->setFichier('')->save();
    $this->index();
Patrick Barroca's avatar
Patrick Barroca committed
666
  }
667 668


Patrick Barroca's avatar
Patrick Barroca committed
669 670 671
  public function getVignettePath() {
    return $this->getBasePath() . $this->getFichier();
  }
672 673


Patrick Barroca's avatar
Patrick Barroca committed
674 675 676 677 678 679
  public function deletePdf() {
    if ('' != $this->getPdf()) {
      $this->getFileSystem()->unlink($this->getBasePath() . $this->getPdf());
      $this->setPdf('')->save();
    }
  }
680 681


682 683 684 685 686 687 688 689
  /**
   * @return string
   */
  public function getBasePath() {
    return $this->getBase(USERFILESPATH);
  }


Patrick Barroca's avatar
Patrick Barroca committed
690 691 692
  /**
   * @return bool
   */
693 694
  public function receiveFile() {
    $to_delete = [$this->getVignettePath(), $this->getThumbnailPath()];
695 696 697 698

    $on_update = function($old_file, $new_file) use ($to_delete)
      {
        if ($old_file && $old_file != $new_file)
699 700
          foreach($to_delete as $item)
            $this->getFileSystem()->unlink($item);
701

702
        $this->createThumbnail();
703

704 705
        return true;
      };
706 707 708 709 710 711 712 713 714

    $handler = $this->getUploadHandler('fichier')
                    ->setAllowedExtensions(['jpg', 'gif', 'png', 'jpeg', 'bro']);

    return (new Class_FileProperty($this, 'fichier', $handler, $on_update))
      ->receive();
  }


Patrick Barroca's avatar
Patrick Barroca committed
715
  public function receivePDF() {
716 717 718 719 720 721
    $to_delete = [$this->getBasePath() . $this->getPdf()];
    $on_update = function($old_file, $new_file) use ($to_delete) {
      if ($old_file && $old_file != $new_file)
        foreach($to_delete as $item)
          $this->getFileSystem()->unlink($item);

Patrick Barroca's avatar
Patrick Barroca committed
722
      return true;
723 724 725 726
    };

    $handler = $this->getUploadHandler('pdf')
                    ->setAllowedExtensions(['pdf']);
727

728 729
    return (new Class_FileProperty($this, 'pdf', $handler, $on_update))
      ->receive();
Patrick Barroca's avatar
Patrick Barroca committed
730
  }
731 732


733 734 735 736
  public function createThumbnail() {
    if (!$this->hasFichier())
      return true;

737 738 739
    if ($this->getFileSystem()->file_exists($this->getThumbnailPath()))
      return true;

740 741
    try {
      $image = new Imagick($this->getVignettePath());
742
      $image->thumbnailImage(300, 0);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

      if (!$image->writeImage($this->getThumbnailPath())) {
        $this->addError('Erreur lors de l\'enregistrement de la vignette');
        return false;
      }

      return true;

    } catch (Exception $e) {
      $this->addError('Erreur lors de la création de la vignette '
                      . (string)$e->getMessage());
      return false;
    }
  }


759 760
  public function getFile() {
    return $this->getFichier();
Patrick Barroca's avatar
Patrick Barroca committed
761
  }
762

763

Patrick Barroca's avatar
Patrick Barroca committed
764 765 766 767
  /** @return string */
  public function getThumbnailUrl() {
    if ('' == $this->getFichier())
      return '';
768

769
    return $this->getBaseUrl() . self::THUMB_PREFIX . $this->getFichier();
Patrick Barroca's avatar
Patrick Barroca committed
770
  }
771 772


Patrick Barroca's avatar
Patrick Barroca committed
773 774 775
  public function getThumbnailPath() {
    return $this->getBasePath() . self::THUMB_PREFIX . $this->getFichier();
  }
776 777


Patrick Barroca's avatar
Patrick Barroca committed
778 779 780 781 782 783
  public function hasOnlyImages() {
    foreach($this->getRessources() as $ressource)
      if (!$ressource->isImage())
        return false;
    return true;
  }
784 785


Patrick Barroca's avatar
Patrick Barroca committed
786 787 788 789 790 791
  /**
   * @param Class_AlbumRessource $resourceToMove
   * @param int $targetId
   */
  public function moveRessourceAfter($resourceToMove, $targetId) {
    $targetId = (int)$targetId;
792

Patrick Barroca's avatar
Patrick Barroca committed
793 794 795 796
    if (0 == $targetId) {
      $this->_moveResourceToTop($resourceToMove);
      return;
    }
797

Patrick Barroca's avatar
Patrick Barroca committed
798
    $sourceId = $resourceToMove->getId();
799

Patrick Barroca's avatar
Patrick Barroca committed
800 801 802
    if ($sourceId == $targetId) {
      return;
    }
803

Patrick Barroca's avatar
Patrick Barroca committed
804 805
    $sourceKey = $targetKey = null;
    $resources = $this->getRessources();
806

Patrick Barroca's avatar
Patrick Barroca committed
807 808 809
    foreach ($resources as $k => $resource) {
      if ($resource->getId() == $sourceId)
        $sourceKey = $k;
810

Patrick Barroca's avatar
Patrick Barroca committed
811 812
      if ($resource->getId() == $targetId)
        $targetKey = $k;
813

Patrick Barroca's avatar
Patrick Barroca committed
814 815 816
      if ((0 < $targetKey) && (0 < $sourceKey))
        break;
    }
817

Patrick Barroca's avatar
Patrick Barroca committed
818 819
    if ((null === $targetKey) || (null === $sourceKey))
      return;
820 821


Patrick Barroca's avatar
Patrick Barroca committed
822
    $newRessources = [];
823

Patrick Barroca's avatar
Patrick Barroca committed
824 825 826 827 828
    $order = 1;
    foreach ($resources as $k => $resource) {
      if ($k == $sourceKey) {
        continue;
      }
829

Patrick Barroca's avatar
Patrick Barroca committed
830
      $newRessources[] = $resource->setOrdre($order);
831

Patrick Barroca's avatar
Patrick Barroca committed
832 833 834 835
      if ($k == $targetKey) {
        $order++;
        $newRessources[] = $resources[$sourceKey]->setOrdre($order);
      }
836

Patrick Barroca's avatar
Patrick Barroca committed
837 838
      $order++;
    }
839

Patrick Barroca's avatar
Patrick Barroca committed
840 841
    $this->save();
  }
842 843


844
  protected function hasToBeIndexed() {
845
		return $this->isVisible() && $this->isValidated();
846 847 848
	}


Patrick Barroca's avatar
Patrick Barroca committed
849 850 851 852
  /**
   * @return Array
   */
  public function getRessourcesWithTitre(){
853

Patrick Barroca's avatar
Patrick Barroca committed
854 855 856 857 858 859 860 861
    $ressources_with_titre = [];
    $ressources = $this->getRessources();
    foreach($ressources as $ressource) {
      if ($ressource->hasTitre())
        $ressources_with_titre[]=$ressource;
    }
    return $ressources_with_titre;
  }
862 863


Patrick Barroca's avatar
Patrick Barroca committed
864 865 866 867 868 869
  /**
   * @codeCoverageIgnore
   * @param string $name
   * @return Class_Upload
   */
  public function getUploadHandler($name) {
870 871
    if (!isset($this->_uploadHandler[$name])) {
      $this->_uploadHandler[$name] = Class_Upload::newInstanceFor($name)
Patrick Barroca's avatar
Patrick Barroca committed
872 873 874
        ->setBaseName($this->getId())
        ->setBasePath($this->getBasePath());
    }
875

876
    return $this->_uploadHandler[$name];
Patrick Barroca's avatar
Patrick Barroca committed
877
  }
878

879

Patrick Barroca's avatar
Patrick Barroca committed
880 881 882 883
  public function setUploadMover($name, $mover) {
    $this->getUploadHandler($name)->setUploadMover($mover);
    return $this;
  }
884

885

Patrick Barroca's avatar
Patrick Barroca committed
886 887 888 889
  /**
   * @category testing
   * @param Class_Upload $handler
   */
890 891
  public function setUploadHandler($handler, $name) {
    $this->_uploadHandler[$name] = $handler;
Patrick Barroca's avatar
Patrick Barroca committed
892 893
    return $this;
  }
894 895


Patrick Barroca's avatar
Patrick Barroca committed
896 897
  public function beforeDelete() {
    parent::beforeDelete();
898
    $this->deleteRecord()
899
         ->deleteFiles();
Patrick Barroca's avatar
Patrick Barroca committed
900
  }
901 902


Patrick Barroca's avatar
Patrick Barroca committed
903 904 905 906 907 908 909 910
  public function beforeSave() {
    $this
      ->updateDateMaj()
      ->collectAuthors()
      ->collectEditors()
      ->collectCollections()
      ->setNotes($this->getMarc()->serialized());
  }
911 912


913 914 915 916 917 918 919
  public function saveWithoutDateMaj() {
    $this->_date_maj_enabled = false;
    $this->save();
    $this->_date_maj_enabled = true;
  }


Patrick Barroca's avatar
Patrick Barroca committed
920 921 922 923 924
  public function collectAuthors() {
    $authors = $this->_collect('authors', self::AUTHOR_FIELD);
    if(isset($authors['author']))
      foreach($authors['author'] as $k => $v)
        $this->addAuthor($v, $authors['fonction'][$k]);
925

Patrick Barroca's avatar
Patrick Barroca committed
926 927
    return $this;
  }
928

continuum's avatar
continuum committed
929

Patrick Barroca's avatar
Patrick Barroca committed
930 931 932 933 934
  public function collectEditors() {
    $editors = $this->_collect('editors', self::EDITOR_FIELD);
    if(isset($editors['editor']))
      foreach($editors['editor'] as $k => $v)
        $this->addEditor($v);
935

Patrick Barroca's avatar
Patrick Barroca committed
936 937
    return $this;
  }
938 939


Patrick Barroca's avatar
Patrick Barroca committed
940 941 942 943 944
  public function collectCollections() {
    $collections = $this->_collect('collections', self::COLLECTION_FIELD);
    if(isset($collections['collection']))
      foreach($collections['collection'] as $k => $v)
        $this->addCollection($v);
945

Patrick Barroca's avatar
Patrick Barroca committed
946 947
    return $this;
  }
948 949


Patrick Barroca's avatar
Patrick Barroca committed
950 951 952
  protected function _collect($field, $zone) {
    if (!isset($this->_attributes[$field]))
      return [];
953

Patrick Barroca's avatar
Patrick Barroca committed
954 955
    $fields = $this->_attributes[$field];
    unset($this->_attributes[$field]);
continuum's avatar
continuum committed
956

Patrick Barroca's avatar
Patrick Barroca committed
957 958 959
    $this->getMarc()->clearZone($zone);
    return $fields;
  }
960 961


Patrick Barroca's avatar
Patrick Barroca committed
962
  public function updateDateMaj() {
963 964 965
    if (!$this->_date_maj_enabled)
      return $this;

Patrick Barroca's avatar
Patrick Barroca committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
    return $this->setDateMaj(date('Y-m-d H:i:s', self::getTimeSource()->time()));
  }


  public function deleteFiles() {
    try {
      $iterator = new DirectoryIterator($this->getBasePath());
      foreach ($iterator as $fileInfo)
        if ($fileInfo->isFile())
          $this->getFileSystem()->unlink($fileInfo->getPathname());

      $this->_removeFolders();
    } catch (Exception $e) {
      // on arrive ici si DirectoryIterator ne peut pas ouvrir le rep
      // on ne peut rien faire
    }
  }


985
  public function deleteRecord() {
986 987
    if ($notice = $this->getNotice())
      $notice->delete();
988 989 990 991 992

    return $this;
  }


Patrick Barroca's avatar
Patrick Barroca committed
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  /**
   * Ceci échouera si les reps ne sont pas vides
   */
  protected function _removeFolders() {
    $fileSystem = $this->getFileSystem();
    $fileSystem->rmdir($this->getOriginalsPath() . Class_AlbumRessource::BASE_PATH);
    $fileSystem->rmdir($this->getOriginalsPath());
    $fileSystem->rmdir($this->getThumbnailsPath() . Class_AlbumRessource::BASE_PATH);
    $fileSystem->rmdir($this->getThumbnailsPath());
    $fileSystem->rmdir($this->getBasePath());
  }


  /**
   * @param Class_AlbumRessource $resourceToMove
   */
  protected function _moveResourceToTop($resourceToMove) {
    $sourceId   = $resourceToMove->getId();
    $sourceKey  = null;

    $resources = $this->getRessources();

    foreach ($resources as $k => $resource) {
      if ($resource->getId() == $sourceId) {
        $sourceKey = $k;
        break;
      }
    }

    if (null === $sourceKey)
      return;

    $newRessources = [$resources[$sourceKey]->setOrdre(1)];
    $order = 2;
    foreach ($resources as $k => $resource) {
      if ($k == $sourceKey)
        continue;

      $newRessources[] = $resource->setOrdre($order);

      $order++;
    }

    $this->setRessources($newRessources);
    $this->save();
  }


  /**
   * @param string $name
   * @param array $arguments
   * @return mixed
   */
  public function __call($name, $arguments) {
    $supportedTypes = ['Image', 'Flash', 'Video', 'File'];
    $supportedPluralTypes = ['Images', 'Flashs', 'Videos', 'Files'];

    if ('has' === substr($name, 0, 3)) {
      $type = substr($name, 3);
      if (('Only' == substr($type, -4))
          && (in_array(substr($type, 0, strlen($type) - 4),
                       $supportedTypes))) {
        return $this->_hasOnlyKindOf(substr($type, 0, strlen($type) - 4));
      }

      if (in_array($type, $supportedTypes)) {
        return $this->_hasKindOf($type);
      }
    }

    if (('get' === substr($name, 0, 3))
1064
        && (in_array(substr($name, 3), $supportedPluralTypes))) {
Patrick Barroca's avatar
Patrick Barroca committed
1065 1066 1067 1068 1069
      return $this->_getKindOf(substr($name, 3));
    }

    return parent::__call($name, $arguments);
  }
1070 1071


Patrick Barroca's avatar
Patrick Barroca committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  /**
   * @param string $type
   * @return bool
   */
  protected function _hasKindOf($type) {
    foreach ($this->getRessources() as $ressource)
      if ($ressource->{'is' . $type}())
        return true;
    return false;
  }
1082

1083

Patrick Barroca's avatar
Patrick Barroca committed
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
  /**
   * @param string $type
   * @return bool
   */
  protected function _hasOnlyKindOf($type) {
    foreach ($this->getRessources() as $ressource)
      if (!$ressource->{'is' . $type}())
        return false;
    return true;
  }
1094

1095

Patrick Barroca's avatar
Patrick Barroca committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  /**
   * @param string $type
   * @return array
   */
  public function _getKindOf($type) {
    $type = substr($type, 0, strlen($type) - 1);
    $items = [];
    foreach ($this->getRessources() as $ressource)
      if ($ressource->{'is' . $type}())
        $items[] = $ressource;
    return $items;
  }
1108 1109


1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  /**
   * @return string
   */
  public function getFirstImagePathFoundInResources() {
    foreach($this->getRessources() as $ressource) {
      if ($ressource->isImage())
        return $ressource->getOriginalPath();

      if ($poster = $ressource->getPoster())
        return $ressource->getPosterPath();
    }
1121 1122 1123
  }


Patrick Barroca's avatar
Patrick Barroca committed
1124 1125 1126 1127
  /**
   * @return string
   */
  public function getAbsolutePath() {
1128
    return $this->getCategorie()->getAbsolutePath() . ' > ' . $this->getTitre();
Patrick Barroca's avatar
Patrick Barroca committed
1129
  }
1130

1131

Patrick Barroca's avatar
Patrick Barroca committed
1132 1133 1134 1135
  public function getCategorie() {
    return ($parent = parent::getCategorie()) ?
      $parent : Class_AlbumCategorie::defaultCategory();
  }
1136

Laurent's avatar
Laurent committed
1137

1138 1139 1140 1141 1142
  public function getCategoryLabel() {
    return $this->getCategorie()->getLibelle();
  }


Patrick Barroca's avatar
Patrick Barroca committed
1143 1144 1145 1146 1147
  public function setAnnee($annee) {
    if (!$annee)
      return $this->_set('annee', '');
    return $this->_set('annee', sprintf('%04d', $annee));
  }
1148

1149

Patrick Barroca's avatar
Patrick Barroca committed
1150 1151 1152 1153 1154
  public function validate() {
    $next_year = date('Y', strtotime('+1 year'));
    $this->check((0==(int)$this->getAnnee())
                 || (($this->getAnnee() >= self::ANNEE_MIN)
                     and ($this->getAnnee() <= $next_year)),
1155
                 sprintf($this->_("L'année doit être comprise entre %s et %s"),
Patrick Barroca's avatar
Patrick Barroca committed
1156 1157
                         self::ANNEE_MIN,
                         $next_year));
Laurent's avatar
Laurent committed
1158

1159 1160 1161 1162
    $this->check($this->hasTitre(),
                 $this->_('Le titre est obligatoire'));
    $this->check($this->hasTypeDocId(),
                 $this->_('L\'album doit avoir un type de document'));
Patrick Barroca's avatar
Patrick Barroca committed
1163
  }
1164

1165

Patrick Barroca's avatar
Patrick Barroca committed
1166 1167 1168 1169 1170
  public function setThumbnailAttributes($attributes) {
    if (is_array($attributes))
      return $this->setCfgThumbnails(ZendAfi_Filters_Serialize::serialize($attributes));
    return $this->setCfgThumbnails($attributes);
  }
1171 1172


Patrick Barroca's avatar
Patrick Barroca committed
1173 1174 1175 1176 1177
  public function setThumbnailAttribute($attribute, $value) {
    $attributes = $this->getThumbnailAttributes();
    $attributes[$attribute] = $value;
    return $this->setThumbnailAttributes($attributes);
  }
1178 1179


Patrick Barroca's avatar
Patrick Barroca committed
1180 1181 1182
  public function getDuration() {
    return $this->getNote(self::DURATION_FIELD);
  }
1183 1184


Patrick Barroca's avatar
Patrick Barroca committed
1185 1186 1187
  public function setDuration($hh_mm_ss) {
    return $this->addNote(self::DURATION_FIELD, $hh_mm_ss);
  }
1188 1189


Patrick Barroca's avatar
Patrick Barroca committed
1190 1191 1192
  public function setNotes($array_or_string) {
    return parent::setNotes($this->getMarc()->setDatas($array_or_string)->serialized());
  }
1193

continuum's avatar
continuum committed
1194

Patrick Barroca's avatar
Patrick Barroca committed
1195 1196 1197
  public function getNote($field) {
    return $this->getMarc()->getNote($field);
  }
1198 1199


Patrick Barroca's avatar
Patrick Barroca committed
1200 1201 1202 1203
  public function addNote($field, $value) {
    $this->getMarc()->addNote($field, $value);
    return $this;
  }
1204 1205


Patrick Barroca's avatar
Patrick Barroca committed
1206 1207 1208
  public function clearNotes() {
    return $this->getMarc()->clear();
  }
efalcy's avatar
efalcy committed
1209

1210

Patrick Barroca's avatar
Patrick Barroca committed
1211 1212 1213 1214 1215 1216 1217 1218 1219
  /**
   * @return int
   */
  public function getThumbnailHeight() {
    if (!$this->hasRessources())
      return 1;
    $first_ressource =  array_first($this->getRessources());
    return round(($first_ressource->getHeight() / $first_ressource->getWidth()) * $this->getThumbnailWidth());
  }
1220

1221

Patrick Barroca's avatar
Patrick Barroca committed
1222 1223 1224 1225 1226 1227 1228 1229
  /**
   * @return array
   */
  public function getThumbnailAttributes() {
    if (!$attributes = ZendAfi_Filters_Serialize::unserialize($this->getCfgThumbnails()))
      $attributes = [];
    return $attributes;
  }
1230 1231


Patrick Barroca's avatar
Patrick Barroca committed
1232 1233 1234 1235 1236 1237 1238
  /**
   * @return int
   */
  public function getFirstThumbnailSize() {
    if (!$this->hasRessources())
      return 0;
    $first_ressource = array_first($this->getRessources());
1239

Patrick Barroca's avatar
Patrick Barroca committed
1240 1241 1242 1243
    $filepath = $first_ressource->getThumbnailFilePath(['width' => $this->getThumbnailWidth()]);
    $fileInfos = stat($filepath);
    return $fileInfos['size'];
  }
1244 1245


Patrick Barroca's avatar
Patrick Barroca committed
1246 1247 1248 1249 1250 1251
  /**
   * @return int
   */
  public function getNavigatorThumbnailWidth() {
    return 80;
  }
1252

Laurent's avatar
Laurent committed
1253

Patrick Barroca's avatar
Patrick Barroca committed
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  /**
   * @return array
   */
  public function toArray() {
    $attributes = parent::toArray();
    return array_merge($attributes,
                       $this->getDefaultThumbnailValues(),
                       $this->getThumbnailAttributes(),
                       ['nature_doc_ids' => $this->getNatureDocIds(),
                        'distributor' => $this->getDistributor(),
1264
                        'isbn' => $this->getIsbn(),
Patrick Barroca's avatar
Patrick Barroca committed
1265 1266
                        'duration' => $this->getDuration()]);
  }
Laurent's avatar
Laurent committed
1267

1268

Patrick Barroca's avatar
Patrick Barroca committed
1269 1270 1271 1272 1273 1274 1275
  /**
   * @category testing
   * @return array
   */
  public function rawToArray() {
    return parent::toArray();
  }
1276

1277

Patrick Barroca's avatar
Patrick Barroca committed
1278 1279 1280 1281 1282 1283
  /**
   * @return array
   */
  public function getNatureDocIds() {
    return array_filter(explode(';', $this->getNatureDoc()));
  }
1284

1285

Patrick Barroca's avatar
Patrick Barroca committed
1286 1287 1288 1289 1290
  public function setNatureDocIds($ids) {
    if (!is_array($ids))
      $ids = [];
    return $this->setNatureDoc(implode(';', $ids));
  }
1291

Laurent's avatar
Laurent committed
1292

Patrick Barroca's avatar
Patrick Barroca committed
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  /**
   * @return Class_Album
   */
  public function sortRessourceByFileName() {
    $ressources = $this->getRessources();
    usort($ressources, ['Class_AlbumRessource', 'sortByFileName']);
    foreach($ressources as $i => $ressource)
      $ressource->setOrdre($i + 1);
    return $this;
  }
1303

Laurent's avatar
Laurent committed
1304

Patrick Barroca's avatar
Patrick Barroca committed
1305 1306 1307
  public function formatedCount() {
    return sprintf('%03d', $this->getRessourcesCount());
  }
1308 1309


Patrick Barroca's avatar
Patrick Barroca committed
1310 1311 1312
  public function isGallica() {
    return ($this->isOAI() && (false !== strpos($this->getIdOrigine(), 'gallica')));
  }
1313 1314


Patrick Barroca's avatar
Patrick Barroca committed
1315 1316 1317 1318 1319
  public function getGallicaArkId() {
    if (!$this->isGallica())
      return '';
    return array_last(explode('/', $this->getIdOrigine()));
  }
1320

1321

Patrick Barroca's avatar
Patrick Barroca committed
1322 1323 1324 1325 1326 1327 1328
  public function getTrailers() {
    $trailers = [];
    $trailers_url = $this->getUnimarc856Values('trailer');
    foreach($trailers_url as $url)
      $trailers[] = Class_Video::newWithUrl($url);
    return $trailers;
  }
1329

1330

Patrick Barroca's avatar
Patrick Barroca committed
1331 1332 1333
  public function getPoster() {
    if (!$posters_url = $this->getUnimarc856Values('poster'))
      return '';
1334

Patrick Barroca's avatar
Patrick Barroca committed
1335 1336
    foreach($posters_url as $url)
      if (false !== strpos($url, 'small')) return $url;
1337

Patrick Barroca's avatar
Patrick Barroca committed
1338 1339
    return $posters_url[0];
  }
1340 1341


Patrick Barroca's avatar
Patrick Barroca committed
1342 1343 1344 1345 1346
  public function getThumbnail() {
    if ($thumbnails = $this->getUnimarc856Values('thumbnail'))
      return $thumbnails[0];
    return '';
  }
llaffont's avatar
llaffont committed
1347 1348


Patrick Barroca's avatar
Patrick Barroca committed
1349 1350
  public function getAudioTracks() {
    return array_filter(array_map(
1351 1352 1353 1354
                                  function($item){
                                    return $item->isAudio() ? $item : null;
                                  },
                                  $this->getRessources()));
Patrick Barroca's avatar
Patrick Barroca committed
1355
  }
llaffont's avatar
llaffont committed
1356 1357


Patrick Barroca's avatar
Patrick Barroca committed
1358 1359 1360 1361 1362 1363
  /**
   * Return arteVOD poster url
   * @return string
   */
  public function getUnimarc856Values($field) {
    $values = [];
1364

Patrick Barroca's avatar
Patrick Barroca committed
1365 1366 1367 1368 1369 1370
    foreach ($this->getMarc()->getDatasOfField('856') as $unimarc_value) {
      if (!isset($unimarc_value['x'])
          || !isset($unimarc_value['a'])
          || $field !== $unimarc_value['x']
          || '' == trim($unimarc_value['a']))
        continue;
Laurent's avatar
Laurent committed
1371

Patrick Barroca's avatar
Patrick Barroca committed
1372 1373
      $values[] = $unimarc_value['a'];
    }
gloas's avatar
gloas committed
1374

Patrick Barroca's avatar
Patrick Barroca committed
1375 1376
    return $values;
  }
1377

1378

Patrick Barroca's avatar
Patrick Barroca committed
1379 1380 1381 1382 1383 1384 1385
  /**
   * @return array key/value pour les zones 856$x (donnée privé/ type URI) $a (URI)
   */
  protected function _getUnimarcForType($type, $url) {
    return ['field' => '856',
            'data' => ['x' => $type, 'a' => $url]];
  }
1386

1387

Patrick Barroca's avatar
Patrick Barroca committed
1388 1389 1390
  public function addPosterURI($uri) {
    return $this->addURIInNotes(self::URI_TYPE_POSTER, $uri);
  }
1391

1392

Patrick Barroca's avatar
Patrick Barroca committed
1393 1394 1395
  public function addThumbnailURI($uri) {
    return $this->addURIInNotes(self::URI_TYPE_THUMNAIL, $uri);
  }
1396

1397

Patrick Barroca's avatar
Patrick Barroca committed
1398 1399 1400
  public function addTrailerURI($uri) {
    return $this->addURIInNotes(self::URI_TYPE_TRAILER, $uri);
  }
1401 1402


Patrick Barroca's avatar
Patrick Barroca committed
1403 1404 1405
  public function addPhotoURI($uri) {
    return $this->addURIInNotes(self::URI_TYPE_PHOTO, $uri);
  }
1406

1407

Patrick Barroca's avatar
Patrick Barroca committed
1408 1409 1410
  public function setExternalUri($uri)  {
    return $this->addURIInNotes(self::URI_TYPE_EXTERNAL_URI, $uri);
  }
1411

1412

1413 1414 1415 1416 1417
  public function setEditeur($data) {
    throw new RunTimeException('Album::setEditeur does not exist anymore');
  }


Patrick Barroca's avatar
Patrick Barroca committed
1418 1419 1420 1421 1422 1423 1424 1425
  /**
   * @return string url
   */
  public function getExternalUri()  {
    if ($values = $this->getUnimarc856Values(self::URI_TYPE_EXTERNAL_URI))
      return $values[0];
    return '';
  }
1426

1427

Patrick Barroca's avatar
Patrick Barroca committed
1428 1429 1430 1431
  public function addURIInNotes($type, $uri) {
    $this->getMarc()->addZone('856', ['x' => $type, 'a' => $uri]);
    return $this;
  }
1432

Laurent's avatar
Laurent committed
1433

Patrick Barroca's avatar
Patrick Barroca committed
1434 1435 1436 1437 1438 1439
  /** @return string */
  public function getNotesForPseudoNotice() {
    $pseudo_marc = clone $this->getMarc();
    return $pseudo_marc->clearZones([self::AUTHOR_FIELD, self::EDITOR_FIELD])
                       ->serialized();
  }
1440 1441


Patrick Barroca's avatar
Patrick Barroca committed
1442 1443 1444 1445 1446 1447
  public function acceptVisitor($visitor) {
    $visitor->visitAlbum($this);
    $ressources = $this->getRessources();
    foreach($ressources as $index => $ressource)
      $visitor->visitRessource($ressource, $index);
  }
1448 1449 1450


  /**
Patrick Barroca's avatar
Patrick Barroca committed
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
   * @return boolean
   */
  public function isVisible() {
    return (bool)$this->getVisible();
  }


  public function isValidated() {
    return (self::STATUS_VALIDATED == $this->getStatus());
  }

1462

Patrick Barroca's avatar
Patrick Barroca committed
1463 1464 1465
  public function getIdEad() {
    return $this->hasIdOrigine() ? $this->getIdOrigine() : 'A' . $this->getId();
  }
1466

1467

Patrick Barroca's avatar
Patrick Barroca committed
1468 1469 1470
  public function getTitleInfo() {
    return '';
  }
1471 1472


Patrick Barroca's avatar
Patrick Barroca committed
1473 1474 1475
  public function getIcoInfoTitle() {
    return '';
  }
1476

1477

Patrick Barroca's avatar
Patrick Barroca committed
1478
  public function getIcoInfo() {
1479
    return 'liste';
Patrick Barroca's avatar
Patrick Barroca committed
1480
  }
1481

1482

Laurent's avatar
Laurent committed
1483
  /**
Patrick Barroca's avatar
Patrick Barroca committed
1484 1485 1486 1487 1488 1489
   * Main author
   * @return string
   */
  public function getMainAuthorName() {
    if (!$authors = $this->getAuthors())
      return '';
Laurent's avatar
Laurent committed
1490

Patrick Barroca's avatar
Patrick Barroca committed
1491 1492
    return array_shift($authors)->getName();
  }
Laurent's avatar
Laurent committed
1493 1494


Patrick Barroca's avatar
Patrick Barroca committed
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
  /**
   * Author names from unimarc container
   * @return array
   */
  public function getAuthorsNames() {
    $names = [];
    foreach($this->getAuthors() as $author)
      $names []= $author->getName();
    return $names;
  }


  /**
   * Author from unimarc container
   * @return array
   */
  public function getAuthors() {
    $authors = [];
    foreach ($this->getMarc()->getDatasOfField(self::AUTHOR_FIELD) as $item) {
      if (!isset($item['a']))
        continue;
      $authors []= new Class_Notice_Author($item['a'],
                                           isset($item['4']) ? $item['4'] : '');
    }
    return $authors;
  }


1523
  /**
Patrick Barroca's avatar
Patrick Barroca committed
1524
   * add author in unimarc container if not present with the same function
1525 1526
   * @param author string or Class_Notice_Author
   * @param function string
Patrick Barroca's avatar
Patrick Barroca committed
1527
   */
1528 1529
  public function addAuthor($author, $responsibility = '') {
    if (!$author)
Patrick Barroca's avatar
Patrick Barroca committed
1530 1531
      return $this;

1532 1533
    if (is_string($author))
      $author = new Class_Notice_Author($author, $responsibility);
Patrick Barroca's avatar
Patrick Barroca committed
1534

1535 1536 1537 1538 1539
    $datas = ['a' => $author->getName()];
    if ($author->getResponsibility())
      $datas['4'] = $author->getResponsibility();

    if (!in_array($author->getName(), $this->getAuthorsNames())) {
Patrick Barroca's avatar
Patrick Barroca committed
1540 1541 1542 1543
      $this->getMarc()->addZone(self::AUTHOR_FIELD, $datas);
      return $this;
    }

1544
    if (!$author->getResponsibility())
Patrick Barroca's avatar
Patrick Barroca committed
1545 1546 1547 1548
      return $this;

    $found = false;

1549 1550
    foreach($this->getAuthors() as $other) {
      if ($author->getName() == $other->getName() && $author->getResponsibility() == $other->getResponsibility()) {
Patrick Barroca's avatar
Patrick Barroca committed
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
        $found = true;
        break;
      }
    }

    if (!$found)
      $this->getMarc()->addZone(self::AUTHOR_FIELD, $datas);

    return $this;
  }


  /**
   * Editor from unimarc container
   * @return array
   */
  public function getEditors() {
    return $this->getMarc()->getSubfield(Class_Album::EDITOR_FIELD, 'c');
  }


1572 1573 1574 1575 1576 1577 1578 1579
  public function getFirstEditor() {
    if (!$items = $this->getEditors())
      return '';

    return array_shift($items);
  }


Patrick Barroca's avatar
Patrick Barroca committed
1580 1581 1582 1583 1584 1585 1586
  /**
   * Collection from unimarc container
   * @return array
   */
  public function getCollections() {
    return $this->getMarc()->getSubfield(self::COLLECTION_FIELD, 'a');
  }
1587

Patrick Barroca's avatar
Patrick Barroca committed
1588

1589 1590 1591 1592 1593 1594 1595 1596
  public function getFirstCollection() {
    if (!$items = $this->getCollections())
      return '';

    return array_shift($items);
  }


Patrick Barroca's avatar
Patrick Barroca committed
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
  public function getDistributor() {
    return $this->getMarc()->getNote(self::DISTRIBUTOR_FIELD);
  }


  public function setDistributor($distributor) {
    $this->getMarc()->addNote(self::DISTRIBUTOR_FIELD, $distributor);
    return $this;
  }


  /**
   * add editor in unimarc container if not present with the same function
   */
  public function addEditor($name) {
    if ($name)
      $this->getMarc()->addSubField('210', 'c', $name);
    return $this;
  }


  /**
   * add collection in unimarc container if not present with the same function
   */
  public function addCollection($name) {
    if ($name)
      $this->getMarc()->addZone(self::COLLECTION_FIELD, ['a' => $name]);
    return $this;
  }


  public function acceptClefAlphaVisitor($visitor) {
    $authors = $this->getAuthorsNames();
    $editors = $this->getEditors();

    $visitor->visitTitre($this->getTitre())
            ->visitComplementTitre($this->getSousTitre())
            ->visitAuteur(implode($authors))
            ->visitTypeDocId($this->getTypeDocId())
            ->visitEditeur(array_shift($editors))
            ->visitAnnee($this->getAnnee());
  }
1639 1640 1641 1642 1643


  public function renderOn($view) {
    return (new Class_Album_Renderer($this))->renderOn($view);
  }
1644 1645 1646 1647 1648


  public static function getPublicDomain() {
    return (new self())->_('Domaine public');
  }
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658


  public function getRootCategory() {
    return $this->getCategorie()->getRootParent();
  }


  public function getRootCategoryId() {
    return $this->getRootCategory()->getId();
  }
1659
}