<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: netbase_authperso.class.php,v 1.1.2.3.6.1 2026/08/20 14:33:08 rtigero Exp $
if (stristr($_SERVER['REQUEST_URI'], ".class.php")) die("no access");

global $class_path;
require_once ($class_path . "/indexations_collection.class.php");

class netbase_authperso extends netbase_authorities {
    
    protected static $object_type = AUT_TABLE_AUTHPERSO;
    
    protected static $id_authperso = 0;
    
    protected static $indexation_authpersos = [];
    
    public static function set_id_authperso($id_authperso) {
        static::$id_authperso = $id_authperso;
    }
    
    public static function get_index_query_count() {
        return "SELECT count(1) FROM authperso_authorities";
    }
    
    /**
     * Requete de recuperation d'un lot d'autorites perso a indexer
     *
     * Le tri porte sur authperso_authority_authperso_num pour regrouper les autorites
     * par type d'autorite perso, mais cette colonne n'est pas discriminante : le tri doit
     * imperativement etre complete par la cle primaire, sinon l'ordre des lignes ex aequo
     * n'est pas garanti d'un lot a l'autre (chaque lot est une requete independante) et des
     * autorites sont alors sautees tandis que d'autres sont traitees deux fois.
     *
     * @param int $start Borne de depart du lot
     * @param int $lot Taille du lot
     * @return string
     */
    public static function get_index_query($start, $lot) {
        $start = intval($start);
        $lot = intval($lot);

        return "SELECT id_authperso_authority as id, authperso_authority_authperso_num from authperso_authorities ORDER BY authperso_authority_authperso_num, id_authperso_authority LIMIT $start, $lot";
    }
    
    public static function index_from_interval($start, $count) {
        global $include_path;
        
        $lot = static::get_lot($count);
        $query = static::get_index_query($start, $lot);
        $result = pmb_mysql_query($query);
        if (pmb_mysql_num_rows($result)) {
            $id_authperso = 0;
            while($row = pmb_mysql_fetch_object($result)) {
                if(!$id_authperso || ($id_authperso != $row->authperso_authority_authperso_num)) {
                    $indexation_authperso = new indexation_authperso($include_path."/indexation/authorities/authperso/champs_base.xml", "authorities", (1000+$row->authperso_authority_authperso_num), $row->authperso_authority_authperso_num);
                    $indexation_authperso->set_deleted_index(true);
                    $id_authperso = $row->authperso_authority_authperso_num;
                }
                $indexation_authperso->maj($row->id);
            }
            pmb_mysql_free_result($result);
            return ($start + $lot);
        }
        return 0;
    }
    
    public static function index_from_interface($start, $count) {
        $temp_indexation_by_fields = static::$indexation_by_fields;
        
        static::$indexation_by_fields = false;
        $next = parent::index_from_interface($start, $count);
        static::$indexation_by_fields = $temp_indexation_by_fields;
        return $next;
    }
    
    public static function get_query_unrelated_authorities() {
        return "SELECT id_authority FROM authorities LEFT JOIN authperso_authorities ON num_object=id_authperso_authority WHERE type_object ='".static::$object_type."' AND id_authperso_authority IS NULL";
    }
    
    public static function get_indexation_authorities() {
        global $include_path;
        if(!isset(static::$indexation_authorities[static::$object_type][static::$id_authperso]) || static::$indexation_authorities[static::$object_type][static::$id_authperso] == null) {
            static::$indexation_authorities[static::$object_type][static::$id_authperso] = new indexation_authpersos($include_path."/indexation/authorities/authperso/champs_base.xml", "authorities", (1000+static::$id_authperso), static::$id_authperso);
        }
        return static::$indexation_authorities[static::$object_type][static::$id_authperso];
    }
    
}