amélioration du type Interface Objet

pull/1/head
paidge 4 months ago
parent ab24581bed
commit 6cbb6334e4

@ -4,10 +4,10 @@ if (!defined('_ECRIRE_INC_VERSION')) {
}
include_spip('base/objets');
define('_GRAPHQL_OBJETS_NON_CONFIGURABLES', ['forums']);
define('_GRAPHQL_OBJETS_NON_CONFIGURABLES', []);
function formulaires_configurer_collections_saisies_dist() {
$collections_non = array_merge(_GRAPHQL_OBJETS_NON_CONFIGURABLES, ['depots', 'paquets', 'plugins']);
$collections_non = array_merge(_GRAPHQL_OBJETS_NON_CONFIGURABLES, ['forums', 'depots', 'paquets', 'plugins']);
$saisies = [];
$tables = lister_tables_objets_sql();
ksort($tables);
@ -22,9 +22,7 @@ function formulaires_configurer_collections_saisies_dist() {
$champs = [];
foreach ($infos["field"] as $nom_champ => $def) {
if (
$nom_champ != $champ_id
&& (in_array($collection, GRAPHQL_COLLECTIONS_NON_OBJET)
|| !in_array($nom_champ, GRAPHQL_CHAMPS_INTERFACE_OBJET))
$nom_champ != $champ_id && !in_array($nom_champ, array_merge(GRAPHQL_CHAMPS_COMMUNS, ['nom', 'bio', 'id_vignette', 'nom_site']))
) {
$champs[$nom_champ] = $nom_champ;
}
@ -160,7 +158,7 @@ function formulaires_configurer_collections_traiter_dist() {
$objets_editoriaux[$collection] = [
'pagination' => _request($collection . '_pagination'),
'champs' => _request($collection . '_champs') ? _request($collection . '_champs') : [],
'liaisons' => _request($collection . '_liaisons'),
'liaisons' => _request($collection . '_liaisons') ? _request($collection . '_liaisons') : [],
];
}
}

@ -4,6 +4,4 @@ if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
define('GRAPHQL_COLLECTIONS_NON_OBJET', ['auteurs', 'syndic', 'documents']);
define('GRAPHQL_CHAMPS_COMMUNS', ['id', 'slug', 'typeCollection', 'points', 'rang']);
define('GRAPHQL_CHAMPS_INTERFACE_OBJET', ['titre', 'texte', 'maj']);
define('GRAPHQL_CHAMPS_COMMUNS', ['id', 'titre', 'descriptif', 'logo', 'maj', 'slug', 'typeCollection', 'points', 'rang']);

@ -100,9 +100,6 @@ class ReponseSPIP {
// Récupération des champs
foreach ($objet as $champ => $value) {
switch ($champ) {
case $champ_id:
$objet['id'] = $value;
break;
case "titre":
$objet[$champ] = supprimer_numero($value);
$objet['rang'] = (preg_match("#^([0-9]+)[.][[:space:]]#", $value, $matches)) ?
@ -139,9 +136,6 @@ class ReponseSPIP {
$champ = preg_replace('#(id_)#', '', $champ);
$objet[$champ] = ($value !== 0 && $value != $id) ? self::findObjet((int) $value, $type_objet) : null;
break;
case "id_vignette":
$objet['vignette'] = ($value !== 0) ? self::findObjet((int) $value, 'document') : null;
break;
case "id_groupe":
$objet['groupe'] = ($value !== 0) ? self::findObjet((int) $value, 'groupe_mots') : null;
break;
@ -159,10 +153,17 @@ class ReponseSPIP {
$objet['typeCollection'] = strtoupper($collection);
// Récupération du logo de l'objet s'il existe
$chercher_logo = charger_fonction('chercher_logo', 'inc');
if ($logo = $chercher_logo($id, $type_objet, 'on')) {
if (!is_null($logo[0])) {
$objet['logo'] = lire_config("adresse_site") . "/" . $logo[0];
include_spip('public/quete');
if ($collection == 'documents' && $objet['id_vignette'] !== '0') {
// Si on est sur un document
include_spip('inc/documents');
$objet['logo'] = lire_config("adresse_site") . "/" . vignette_logo_document($objet);
} else {
// Si on est sur un objet éditorial
$logo = quete_logo_objet($objet['id'], $type_objet, 'on');
if (array_key_exists('chemin', $logo)) {
$objet['logo'] = lire_config("adresse_site") . "/" . $logo['chemin'];
}
}
@ -241,18 +242,36 @@ class ReponseSPIP {
}
private static function getSelect(string $table): array {
$select = ['collection.' . id_table_objet($table) . ' as id'];
if (!in_array(table_objet($table), GRAPHQL_COLLECTIONS_NON_OBJET)) {
foreach (GRAPHQL_CHAMPS_INTERFACE_OBJET as $champ_interface) {
$select[] = 'collection.' . $champ_interface;
}
$select = ['collection.' . id_table_objet($table) . ' as id', 'collection.maj'];
// Détermination des champs à sélectionner par défaut en fonction de la collection
switch ($table) {
case 'spip_documents':
$select[] = 'collection.titre';
$select[] = 'collection.descriptif';
$select[] = 'collection.id_vignette';
break;
case 'spip_auteurs':
$select[] = 'collection.nom as titre';
$select[] = 'collection.bio as descriptif';
break;
case 'spip_syndic':
$select[] = 'collection.nom_site as titre';
$select[] = 'collection.descriptif';
break;
default:
$select[] = 'collection.titre';
$select[] = 'collection.descriptif';
break;
}
$select = array_merge(
$select,
lire_config('/meta_graphql/objets_editoriaux/' . table_objet($table) . '/champs', [])
);
// Récupération des champs sélectionnés par le webmestre
$champs_selectionnes =
lire_config('/meta_graphql/objets_editoriaux/' . table_objet($table) . '/champs', []);
foreach ($champs_selectionnes as $champ_selectionne) {
$select[] = 'collection.' . $champ_selectionne;
}
return $select;
}

@ -22,10 +22,49 @@ class SchemaSPIP {
public array $metas_autorisees = [];
private array $types = [];
private InterfaceType $interface;
public function __construct() {
$this->collections_autorisees = lire_config('/meta_graphql/objets_editoriaux', []);
$this->metas_autorisees = lire_config('/meta_graphql/meta', []);
$champs_interface = [];
foreach (GRAPHQL_CHAMPS_COMMUNS as $champ) {
switch ($champ) {
case 'id':
$champs_interface[$champ] = Type::id();
break;
case 'maj':
$champs_interface[$champ] = $this->get('Date');
break;
case 'typeCollection':
$champs_interface[$champ] = $this->get('Collection');
break;
case 'rang':
case 'points':
$champs_interface[$champ] = Type::int();
break;
default:
$champs_interface[$champ] = Type::string();
break;
}
}
$this->interface = new InterfaceType([
'name' => 'Objet',
'description' => _T('graphql:desc_type_objet'),
'fields' => $champs_interface,
'resolveType' => function ($value, $context, ResolveInfo $info): ObjectType {
// switch ($value->type ?? null) {
// case 'human':
// return MyTypes::human();
// case 'droid':
// return MyTypes::droid();
// // default: throw new Exception("Unknown Character type: {$value->type ?? null}");
// }
return new ObjectType([]);
}
]);
}
// Query, Date, MetaList, Article, SearchResult...
@ -38,25 +77,6 @@ class SchemaSPIP {
$typeDefinition = ['name' => $name];
switch ($name) {
case 'Node':
// Interface Objet pour mutualiser des champs entre objets
$typeDefinition['description'] = _T('graphql:desc_type_base');
$typeDefinition['fields'] = function () {
return $this->champsCommuns(GRAPHQL_CHAMPS_COMMUNS);
};
$typeDefinition['resolveType'] = function ($value, $context, ResolveInfo $info) {
// TODO : écrire le resolveType
// switch ($info->fieldDefinition->name ?? null) {
// case 'human': return MyTypes::human();
// case 'droid': return MyTypes::droid();
// default: throw new Exception("Unknown Character type: {$value->type ?? null}");
// }
};
return new InterfaceType($typeDefinition);
break;
break;
case 'Collection':
// Liste des collections exposées
$typeDefinition['description'] = _T('graphql:desc_type_collection');
@ -89,29 +109,6 @@ class SchemaSPIP {
};
return new ObjectType($typeDefinition);
break;
case 'Objet':
// Interface Objet pour mutualiser des champs entre objets
$typeDefinition['description'] = _T('graphql:desc_type_objet');
$typeDefinition['fields'] = function () {
return $this->champsCommuns(
array_merge(
GRAPHQL_CHAMPS_COMMUNS,
GRAPHQL_CHAMPS_INTERFACE_OBJET
)
);
};
$typeDefinition['resolveType'] = function ($value, $context, ResolveInfo $info) {
// TODO : écrire le resolveType
// switch ($info->fieldDefinition->name ?? null) {
// case 'human': return MyTypes::human();
// case 'droid': return MyTypes::droid();
// default: throw new Exception("Unknown Character type: {$value->type ?? null}");
// }
};
return new InterfaceType($typeDefinition);
break;
case 'Query':
// Les requêtes disponibles
$typeDefinition['description'] = _T('graphql:desc_type_query');
@ -145,19 +142,15 @@ class SchemaSPIP {
if (array_key_exists($collection, $this->collections_autorisees)) {
$typeDefinition['description'] = _T('graphql:desc_type_objet') . ' ' . $name;
$typeDefinition['interfaces'] = [$this->interface];
$typeDefinition['fields'] = function () use (&$collectionType, $collection): array {
// On récupère les champs partagés
$graphQLfields = $this->champsCommuns(GRAPHQL_CHAMPS_COMMUNS);
$typeDefinition['interfaces'] = [$this->get('Node')];
if (!in_array($collection, GRAPHQL_COLLECTIONS_NON_OBJET)) {
$typeDefinition['interfaces'][] = $this->get('Objet');
$graphQLfields = array_merge(
$graphQLfields,
$this->champsCommuns(GRAPHQL_CHAMPS_INTERFACE_OBJET)
);
// On récupère les champs partagés
// $graphQLfields = $this->champsCommuns(GRAPHQL_CHAMPS_COMMUNS);
$graphQLfields = [];
foreach (GRAPHQL_CHAMPS_COMMUNS as $champ_commun) {
$graphQLfields[] = $this->interface->getField($champ_commun);
}
// On récupère les champs autorisés de la collection
@ -166,7 +159,6 @@ class SchemaSPIP {
$info_champs = $collection_infos['champs'];
foreach ($this->collections_autorisees[$collection]['champs'] as $champ) {
$fieldType = null;
$isString = false;
$typeSQL = $info_champs[$champ];
switch (true) {
@ -182,10 +174,6 @@ class SchemaSPIP {
case (in_array($champ, ['id_trad', 'id_parent'])):
$fieldType = $collectionType;
break;
case ($champ == 'id_vignette'):
if (array_key_exists('documents', $this->collections_autorisees))
$fieldType = $this->get('Document');
break;
// Ensuite, on gère les types scalaires
case (stripos($typeSQL, 'double') !== false):
$fieldType = Type::float();
@ -199,21 +187,12 @@ class SchemaSPIP {
case (stripos($typeSQL, 'datetime') !== false):
$fieldType = $this->get('Date');
break;
// case (stripos($typeSQL, 'char') !== false):
// case (stripos($typeSQL, 'longtext') !== false):
// case (stripos($typeSQL, 'mediumtext') !== false):
// case (stripos($typeSQL, 'tinytext') !== false):
// case (stripos($typeSQL, 'text') !== false):
default:
$isString = true;
$fieldType = Type::string();
break;
}
if (!is_null($fieldType)) {
// $fieldType = (strpos($typeSQL, 'NOT NULL') && !$isString) ?
// new NonNull($fieldType) :
// $fieldType;
$champ = preg_replace('#(id_)#', '', $champ);
$graphQLfields[$champ] = $fieldType;
}
@ -262,8 +241,6 @@ class SchemaSPIP {
return $graphQLfields;
};
$collectionType = new ObjectType($typeDefinition);
return $collectionType;
@ -352,34 +329,6 @@ class SchemaSPIP {
return $queryFields;
}
// Retourne un tableau de champs avec leur type graphQL à partir d'une liste de champs
// Utilisé avec les constantes GRAPHQL_CHAMPS_COMMUNS et GRAPHQL_CHAMPS_INTERFACE_OBJET
// pour générer la liste des champs en communs
private function champsCommuns(array $champs_options): array {
foreach ($champs_options as $champ) {
switch ($champ) {
case 'id':
$champs[$champ] = Type::id();
break;
case 'maj':
$champs[$champ] = $this->get('Date');
break;
case 'typeCollection':
$champs[$champ] = $this->get('Collection');
break;
case 'rang':
case 'points':
$champs[$champ] = Type::int();
break;
default:
$champs[$champ] = Type::string();
break;
}
}
return $champs;
}
private function collectionArgs(int $pagination) {
return [
'where' => [

Loading…
Cancel
Save