TypeDoc.php 14.3 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
class TypeDocLoader extends Storm_Model_Loader  {
23 24
  use Trait_Translator;

Patrick Barroca's avatar
Patrick Barroca committed
25
  protected $_all_instances;
26

Patrick Barroca's avatar
Patrick Barroca committed
27 28 29
  public function __construct() {
    $this->findAll();
  }
30

31

Patrick Barroca's avatar
Patrick Barroca committed
32 33
  public function newInstance($attributes = null) {
    $instance = new Class_TypeDoc();
34

Patrick Barroca's avatar
Patrick Barroca committed
35 36
    if (!empty($attributes))
      $instance->updateAttributes($attributes);
37

Patrick Barroca's avatar
Patrick Barroca committed
38 39
    return $instance;
  }
40 41


Patrick Barroca's avatar
Patrick Barroca committed
42
  public function newInstanceWithId($id, $attributes = null) {
43 44
    $instance = $this->newInstance($attributes)
                     ->setId((string)$id);
45

46
    $this->_all_instances[(string) $id] = $instance;
Patrick Barroca's avatar
Patrick Barroca committed
47 48
    return $instance;
  }
49 50


Patrick Barroca's avatar
Patrick Barroca committed
51 52 53 54 55 56
  public function find($id) {
    $instances = $this->findAll();
    if (array_key_exists($id, $instances))
      return $instances[$id];
    return null;
  }
57 58


Patrick Barroca's avatar
Patrick Barroca committed
59 60 61 62
  public function reset() {
    $this->_all_instances = null;
    return $this;
  }
63

64

Patrick Barroca's avatar
Patrick Barroca committed
65 66 67
  public function findAll($select=null) {
    if (isset($this->_all_instances))
      return $this->_all_instances;
68

Patrick Barroca's avatar
Patrick Barroca committed
69
    $instances = [];
70

71 72 73 74 75 76 77
    if ($types_docs = Class_CosmoVar::find('types_docs'))
      $types_docs
        ->withLinesDo(
                      function($key, $label) use(&$instances) {
                        $instance = $this->newInstanceWithId($key)->setLabel($label);
                        $instances[$instance->getId()]= $instance;
                      });
78

Patrick Barroca's avatar
Patrick Barroca committed
79 80 81 82 83
    $default_types = Class_TypeDoc::getDefaultTypeDocs();
    foreach($default_types as $id => $label) {
      if (!isset($instances[$id]))
        $instances[$id] = Class_TypeDoc::newWithLabel($label)->setId($id);
    }
84

Patrick Barroca's avatar
Patrick Barroca committed
85 86
    return $this->_all_instances = $instances;
  }
87 88


Patrick Barroca's avatar
Patrick Barroca committed
89 90 91 92 93 94 95 96
  public function getUserFriendlyTypeDocs() {
    $type_docs = $this->findAll();
    $type_docs_as_array= [];
    $filtred = array_filter ( $type_docs, function($type_doc) {return $type_doc->isUserFriendly();});
    foreach($filtred as $type_doc)
      $type_docs_as_array[$type_doc->getId()] = $type_doc->getLabel();
    return $type_docs_as_array;
  }
97 98


Patrick Barroca's avatar
Patrick Barroca committed
99 100 101 102
  public function saveCodifTypeDoc($model)  {
    $model->getCodifTypeDoc()->save();
    return $this;
  }
103 104


Patrick Barroca's avatar
Patrick Barroca committed
105
  public function save($model) {
106
    $serialized = [];
Patrick Barroca's avatar
Patrick Barroca committed
107
    $this->saveCodifTypeDoc($model);
108

Patrick Barroca's avatar
Patrick Barroca committed
109 110 111 112 113 114
    foreach ($this->findAll() as $i => $type_doc) {
      if ($type_doc->getId() === $model->getId())
        $serialized [$i]= $this->serialize($model);
      else
        $serialized [$i]= $this->serialize($type_doc);
    }
115

Patrick Barroca's avatar
Patrick Barroca committed
116 117 118 119 120
    if ($model->isNew()) {
      $id = max(array_keys($serialized)) + 1;
      $model->setId($id);
      $serialized []= $this->serialize($model);
    }
121

122
    return $this->_saveSerialized($serialized);
Patrick Barroca's avatar
Patrick Barroca committed
123
  }
124

125

Patrick Barroca's avatar
Patrick Barroca committed
126 127
  public function delete($model) {
    $serialized = array();
128

Patrick Barroca's avatar
Patrick Barroca committed
129 130 131 132
    foreach ($this->findAll() as $i => $type_doc) {
      if ($type_doc->getId() !== $model->getId())
        $serialized [$i]= $this->serialize($type_doc);
    }
133

Patrick Barroca's avatar
Patrick Barroca committed
134 135
    return $this->_saveSerialized($serialized);
  }
136 137


Patrick Barroca's avatar
Patrick Barroca committed
138
  protected function serialize($type_doc) {
139
    return sprintf('%s:%s', $type_doc->getId(), $type_doc->getLabel());
Patrick Barroca's avatar
Patrick Barroca committed
140
  }
141 142


Patrick Barroca's avatar
Patrick Barroca committed
143 144
  protected function _saveSerialized($serialized) {
    $this->reset();
145 146 147 148 149

    if($doc_types = Class_CosmoVar::find('types_docs'))
      $doc_types
        ->setListe(implode("\r\n", $serialized))
        ->save();
Patrick Barroca's avatar
Patrick Barroca committed
150
  }
151 152


Patrick Barroca's avatar
Patrick Barroca committed
153
  public function findUsedTypeDocIds() {
154 155 156
    $rows = Class_Notice::getTable()
      ->getAdapter()
      ->fetchAll('select distinct(type_doc) from notices');
Patrick Barroca's avatar
Patrick Barroca committed
157 158 159
    return array_map(function($row){return $row['type_doc'];},
                     $rows);
  }
160 161 162 163 164 165 166


  public function getLabelFor($type_doc_id) {
    return ($type_doc = Class_TypeDoc::find($type_doc_id))
      ? $type_doc->getLabel()
      : $this->_('inconnu');
  }
167 168 169


  public function isDigital($doc_type) {
170
    return array_key_exists($doc_type, Class_TypeDoc::getDigitalDocTypes())
171
      || ((int) $doc_type >= Class_TypeDoc::DIGITAL_RESOURCE_THRESHOLD);
172
  }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206


  public function getMultiOptions() {
    $items = ['' => $this->_('tous')];

    $used_ids = Class_TypeDoc::findUsedTypeDocIds();
    $types = array_filter(Class_TypeDoc::findAll(),
                          function ($type_doc) use ($used_ids) {
                            return in_array($type_doc->getId(),
                                            $used_ids);
                          });

    usort($types,
          function($a, $b)
          {
            return strtolower($a->getLabel()) > strtolower($b->getLabel());
          });

    $profil = Class_Profil::getCurrentProfil();

    $filter = [];
    if ($selection = $profil->getSelTypeDoc()) {
      $filter = explode(';', $selection);
    }

    foreach ($types as $type) {
      if (0 < count($filter) && !in_array($type->getId(), $filter))
        continue;

      $items[$type->getId()] = ucfirst($type->getLabel());
    }

    return $items;
  }
207 208


209 210 211 212 213 214 215 216
  public function getMultiOptionsFacets() {
    $multi_options = $this->getMultiOptions();
    $multi_options_facets = ['' => $this->_('tous')];
    foreach ($multi_options as $id => $label) {
      if (!$doctype = Class_TypeDoc::find($id))
        continue;
      $multi_options_facets[$doctype->getFacetCode()] = $doctype->getLabel();
    }
Ghislain Loas's avatar
Ghislain Loas committed
217

218 219 220 221
    return $multi_options_facets;
  }


222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 294 295 296 297 298
  public function getIdField() {
    return 'id';
  }


  public function count() {
    return count(Class_TypeDoc::findAll());
  }


  public function findAllBy($params) {
    $doc_types = Class_TypeDoc::findAllByWitoutLimit($params);
    $doc_types = Class_Typedoc::orderBy($doc_types, $params);

    if(!isset($params['limitPage']))
      return $doc_types;

    $limit = $params['limitPage'];
    $page = (int) $limit[0];
    $length = (int) $limit[1];
    $offset = (2 > $page)
      ? 0
      : (($page -1) * $length);

    return array_slice($doc_types, $offset, $length);
  }


  public function orderBy($doc_types, $params) {
    if(!isset($params['order']))
      return $doc_types;

    $order = $params['order'];
    usort($doc_types, function($a, $b) use ($order)
          {
            $order .= ' ';
            list($attrib, $desc) = explode(' ', $order);

            if('libelle' == $attrib && !$desc)
              return strtolower($a->getLibelle()) > strtolower($b->getLibelle());

            if('libelle' == $attrib && $desc)
              return strtolower($a->getLibelle()) < strtolower($b->getLibelle());

            if('id' == $attrib && !$desc)
              return $a->getId() > $b->getId();

            if('id' == $attrib && $desc)
              return $a->getId() < $b->getId();
          });

    return $doc_types;
  }


  public function findAllByWitoutLimit($params) {
    $doc_types = Class_TypeDoc::findAll();
    if (!isset($params['where']))
      return $doc_types;

    preg_match('/%(\S*)%/', $params['where'], $searched_value);
    $value = $searched_value[1];
    return array_filter($doc_types,
                        function($doc_type) use ($value)
                        {
                          return (false !== strpos(strtolower($doc_type->getId()),
                                                   strtolower($value)))
                            || (false !== strpos(strtolower($doc_type->getLibelle()),
                                                 strtolower($value)));
                        });
  }



  public function countBy($params) {
    return count(Class_TypeDoc::findAllBy($params));
  }
299 300 301
}


302

303
class Class_TypeDoc extends Storm_Model_Abstract {
304

305
  use Trait_Facetable, Trait_Translator;
306

307 308 309
  const
    CODE_FACETTE = 'T',
    DIGITAL_RESOURCE_THRESHOLD = 100;
Patrick Barroca's avatar
Patrick Barroca committed
310 311 312

  protected $_loader_class = 'TypeDocLoader';
  protected $_belongs_to = ['codif_type_doc' =>
313
                            ['model' => 'Class_CodifTypeDoc',
Patrick Barroca's avatar
Patrick Barroca committed
314
                             'referenced_in' => 'id' ]];
Ghislain Loas's avatar
Ghislain Loas committed
315
  const UNKNOWN = 0;
Patrick Barroca's avatar
Patrick Barroca committed
316 317 318 319 320 321 322 323
  const LIVRE = 1;
  const PERIODIQUE = 2;
  const DISQUE = 3;
  const DVD = 4;
  const LOGICIEL = 5;
  const ARTICLE = 8;
  const RSS = 9;
  const SITE = 10;
324 325
  const PERIODIQUE_ARTICLE = 'per_art';
  const PERIODIQUE_TITLE = 'per_title';
326
  const SERIAL_ARTICLE = 100;
Patrick Barroca's avatar
Patrick Barroca committed
327 328 329 330 331 332 333 334 335 336 337 338 339
  const LIVRE_NUM = 100;
  const DIAPORAMA = 101;
  const EPUB = 102;
  const OAI = 103;
  const ARTEVOD = 104;
  const VODECLIC = 105;
  const NUMILOG = 106;
  const AUDIO_RECORD = 109;
  const NUMERIQUEPREMIUM = 110;
  const CYBERLIBRIS = 111;
  const DILICOM = 112;
  const ORPHEA = 113;
  const JAMENDO = 115;
340
  const WEBSITE = 116;
341
  const SOUNDCLOUD = 117;
342
  const CITEDELAMUSIQUE = 119;
Patrick Barroca's avatar
Patrick Barroca committed
343

344

Patrick Barroca's avatar
Patrick Barroca committed
345
  public static function getDefaultTypeDocs() {
346
    $instance = new Class_TypeDoc;
347
    return [self::LIVRE => $instance->_('Livres'),
348
            self::PERIODIQUE => $instance->_('Périodiques'),
349 350
            self::PERIODIQUE_TITLE => $instance->_('Titre de périodique'),
            self::PERIODIQUE_ARTICLE => $instance->_('Article de périodique'),
351 352 353 354 355 356
            self::DISQUE => $instance->_('Disques'),
            self::DVD => $instance->_('DVD'),
            self::LOGICIEL => $instance->_('Logiciel'),
            self::ARTICLE => $instance->_('Articles'),
            self::RSS => $instance->_('Flux RSS'),
            self::SITE => $instance->_('Sites')]
357
      + static::getDigitalDocTypes();
358 359 360 361
  }


  public static function getDigitalDocTypes() {
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    return [self::LIVRE_NUM => 'Livres numérisés',
            self::DIAPORAMA => 'Diaporamas',
            self::EPUB => 'E-Books',
            self::OAI => 'OAI',
            self::ARTEVOD => 'Arte VOD',
            self::VODECLIC => 'Formation Vodeclic',
            self::NUMILOG => 'Livres numériques',
            self::AUDIO_RECORD => 'Enregistrement audio',
            self::NUMERIQUEPREMIUM => 'Numérique Premium',
            self::CYBERLIBRIS => 'Cyberlibris',
            self::DILICOM => 'Livre numérique (PNB)',
            self::ORPHEA => 'Orphea',
            self::JAMENDO => 'Jamendo',
            self::WEBSITE => 'Site web',
            self::SOUNDCLOUD => 'SoundCloud',
            self::CITEDELAMUSIQUE => 'Cite De La Musique']
      + Class_DigitalResource::getInstance()->getDocTypes();
Patrick Barroca's avatar
Patrick Barroca committed
379 380 381 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
  }


  /**
   * @param String label
   * @return Class_Type_Doc
   */
  public static function newWithLabel($label) {
    $instance = new self();
    return $instance->setLabel($label);
  }


  /**
   * @param Array types_docs
   * @return array
   */
  public static function toIdLabelArray($types_docs) {
    $id_label_array = array();
    foreach($types_docs as $type_doc)
      $id_label_array[$type_doc->getId()] = $type_doc->getLabel();
    return $id_label_array;
  }


  /**
   * @param Array langues
   * @return array
   */
  public static function allByIdLabel() {
    return self::toIdLabelArray(self::getLoader()->findAll());
  }


  /**
   * @param Array langues
   * @return array
   */
  public static function allByIdLabelForAlbum() {
    $all = self::allByIdLabel();
    $for_album = array();
    foreach($all as $id => $label) {
      if ($id >= self::LIVRE_NUM)
        $for_album[$id] = $label;
    }
424

425 426
    return $for_album
      + Class_DigitalResource::getInstance()->getDocTypes();
Patrick Barroca's avatar
Patrick Barroca committed
427 428 429 430 431 432
  }


  public function getCodifTypeDoc() {
    if ($codif=parent::_get('codif_type_doc'))
      return $codif;
Laurent's avatar
Laurent committed
433

Patrick Barroca's avatar
Patrick Barroca committed
434 435
    $codif=Class_CodifTypeDoc::newInstance( ['type_doc_id' => $this->getId(),
                                             'famille_id' => $this->getId()]);
Laurent's avatar
Laurent committed
436

Patrick Barroca's avatar
Patrick Barroca committed
437 438 439 440 441
    $codif->save();
    $codif->setId($this->getId());
    $this->setCodifTypeDoc($codif);
    return $codif;
  }
442

443

Patrick Barroca's avatar
Patrick Barroca committed
444 445 446
  public function getLibelleFamille() {
    return $this->getCodifTypeDoc()->getLibelle();
  }
efalcy's avatar
efalcy committed
447 448


Patrick Barroca's avatar
Patrick Barroca committed
449 450 451
  public function getFamilleId() {
    return $this->getCodifTypeDoc()->getFamilleId();
  }
efalcy's avatar
efalcy committed
452 453


Patrick Barroca's avatar
Patrick Barroca committed
454 455 456
  public function getBibliotheques() {
    return $this->getCodifTypeDoc()->getBibliotheques();
  }
457

458

Patrick Barroca's avatar
Patrick Barroca committed
459 460 461
  public function setBibliotheques($bibs) {
    $this->getCodifTypeDoc()->setBibliotheques($bibs);
  }
462

463

Patrick Barroca's avatar
Patrick Barroca committed
464 465 466
  public function getAnnexes() {
    return $this->getCodifTypeDoc()->getAnnexes();
  }
467

468

Patrick Barroca's avatar
Patrick Barroca committed
469 470 471
  public function setAnnexes($annexes) {
    $this->getCodifTypeDoc()->setAnnexes($annexes);
  }
472 473


Patrick Barroca's avatar
Patrick Barroca committed
474 475 476
  public function getSections() {
    return $this->getCodifTypeDoc()->getSections();
  }
477

478

Patrick Barroca's avatar
Patrick Barroca committed
479 480 481
  public function setSections($sections) {
    $this->getCodifTypeDoc()->setSections($sections);
  }
482

483

Patrick Barroca's avatar
Patrick Barroca committed
484 485 486
  public function getLibelle() {
    return $this->getLabel();
  }
487

488

Patrick Barroca's avatar
Patrick Barroca committed
489 490 491 492
  public function setFamilleId($id) {
    $this->getCodifTypeDoc()->setFamilleId($id);
    return $this;
  }
493

494

Patrick Barroca's avatar
Patrick Barroca committed
495 496 497 498 499
  public function toArray() {
    return ['id'=> $this->getId(),
            'famille_id' => $this->getFamilleId(),
            'label' => $this->getLabel()];
  }
efalcy's avatar
efalcy committed
500

501

502 503 504 505 506 507
  public function isRessourceNumerique() {
    return ($this->getId() >= static::LIVRE_NUM && !$this->isBokeh())
      || $this->isDigitalResource();
  }


508 509 510 511 512 513 514 515 516 517 518 519 520
  public function isThirdPartyDigitalResource() {
    return $this->isLegacyDigitalResource() || $this->isDigitalResource();
  }


  public function isLegacyDigitalResource() {
    return in_array($this->getId(), [static::ARTEVOD, static::VODECLIC, static::NUMILOG,
                                     static::NUMERIQUEPREMIUM, static::CYBERLIBRIS, static::DILICOM,
                                     static::ORPHEA, static::JAMENDO, static::SOUNDCLOUD,
                                     static::CITEDELAMUSIQUE]);
  }


521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
  public function isSigb() {
    return $this->getId() < static::LIVRE_NUM
      && !$this->isBokeh()
      && !$this->isDigitalResource();
  }


  public function isBokeh() {
    return in_array($this->getId(), [static::ARTICLE, static::RSS, static::SITE]);
  }


  public function isDigitalResource() {
    return Class_DigitalResource::getInstance()->isPluginDocType($this->getId());
  }


Patrick Barroca's avatar
Patrick Barroca committed
538 539 540
  public function isSonore() {
    return $this->getCodifTypeDoc()->isSonore();
  }
541

542

543 544 545 546 547
  public function isBook() {
    return $this->getCodifTypeDoc()->isBook();
  }


Patrick Barroca's avatar
Patrick Barroca committed
548 549 550
  public function isUserFriendly() {
    return $this->getFamilleId() > Class_CodifTypeDoc::INCONNU;
  }
551
}