Skip to content
Snippets Groups Projects
PhasePatrons.php 4.88 KiB
<?php
/**
 * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * 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).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * 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
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */

require_once('cosmogramme/php/classes/classe_abonne.php');

class Class_Cosmogramme_Integration_PhasePatrons extends Class_Cosmogramme_Integration_PhaseOnDataSource {
  const MY_ID = 8;

  protected $_label = 'Intégration des fichiers d\'abonnés';

  protected function _init($phase) { }


  /** one line of file has been read and should be processed here */
  protected function _processLine($line, $integration) {
    $line->withDataDo(
                      function($data) use ($integration){
                        $this->importPatronRecord($data, $integration);
                      });
  }


  protected function importPatronRecord($data, $integration) {
    $integrator = new abonne();
    $integrator->setParamsIntegration($integration->getIdBib(),
                                      $integration->getTypeOperation(),
                                      $integration->getProfil());


    $profil = $integration->getProfilDonnees();
    $integrator->importFiche(($profil->getFormat() == Class_IntProfilDonnees::FORMAT_XML)
                             ? $data
                             : $this->mapRecordColumns($integration, $data),

                             $profil->getFormat());
  }



  protected function mapRecordColumns($integration, $datas) {
    $fields = $this->getFields($integration->getProfilDonnees());
    $map = [];
    foreach($fields as $k => $name)
      $map[strtoupper($name)] = $datas[$k];

    return $map;
  }


  protected function getFields($profil_donnees) {
    $attribs = unserialize($profil_donnees->getAttributs());
    return ($profil_donnees->getFormat() == Class_IntProfilDonnees::FORMAT_XML)
      ? $attribs[5]['xml_champs_abonne']
      : array_map('trim', explode(';', $attribs[1]['champs']));
  }


  /** in case of full import, implements what is needed to be done before importing data */
  protected function _clean($integration) {
    $users = Class_Users::findAllBy(['role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                                     'statut not' => Class_Users::STATUT_TO_BE_DELETED]);
    array_map(
              function($user) {
                Class_Users::getLoader()->save($user->setStatut(1));
              },
              $users);
  }

  /** should return true if $line must not be processed */
  protected function _shouldIgnoreLine($line, $integration) {
    return $line->withDataDo([$this, 'isHeader']);
  }


  public function isHeader($data) {
    return
      is_array($data)
      &&
      (in_array('BIB_ABON_CARTE', $data) //pergame/nanook header
       ||
       in_array('nom', $data)); //microbib header
  }


  /** return true if given profil parameters are correct for this phase */
  protected function _validateProfil($profil) {
    if (!$profil->isPatrons())
      return false;

    $errors = [];
    $fields = $this->getFields($profil);

    if ($profil->getFormat() == Class_IntProfilDonnees::FORMAT_XML)
      $fields = array_keys(array_filter($fields));

    $errors = array_filter(
                           array_map(
                                     function($column) use ($fields) {
                                       if (false === array_search($column, $fields))
                                         return $this->_('Configuration: colonne %s requise',
                                                         $column);
                                     },
                                     ['IDABON']
                           ));

    array_map([$this->_log, 'addError'], $errors);

    return empty($errors);
  }

  /** hooked called after the file has been fully processed */
  protected function _afterFileProcessed($integration) {
    if (!$count = Class_Users::countBy(['role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                                        'statut' => Class_Users::STATUT_TO_BE_DELETED]))
      return;

    $this->_log->ecrire('<span class="violet">'
                        . $this->_('%d abonné(s) marqué(s) pour suppression',
                                   $count)
                        . '</span>');
  }
}