@@ -303,18 +303,9 @@ public function onGlpiLocationPreUpdate(CommonDBTM $item, Geocoder $geocoder)
303303 */
304304 protected function enableCarbonIntensityDownload (CommonDBTM $ item ): bool
305305 {
306- if (!in_array ('country ' , array_keys ($ item ->fields ))) {
307- return false ;
308- }
309- $ zone = Zone::getByLocation ($ item );
310- if ($ zone === null ) {
311- return false ;
312- }
313306 $ source_zone = new Source_Zone ();
314- $ source_zone ->getFromDBByCrit ([
315- Zone::getForeignKeyField () => $ zone ->fields ['id ' ],
316- Source::getForeignKeyField () => $ zone ->fields ['plugin_carbon_sources_id_historical ' ],
317- ]);
307+ /** @var GlpiLocation $item */
308+ $ source_zone ->getFromDbByLocation ($ item );
318309 if ($ source_zone ->isNewItem ()) {
319310 return false ;
320311 }
@@ -329,65 +320,61 @@ protected function enableCarbonIntensityDownload(CommonDBTM $item): bool
329320 */
330321 public function isCarbonIntensityDownloadEnabled (CommonDBTM $ item ): bool
331322 {
332- $ zone = Zone::getByLocation ($ item );
333- if ($ zone === null ) {
334- return false ;
335- }
336323 $ source_zone = new Source_Zone ();
337- $ source_zone ->getFromDBByCrit ([
338- Zone::getForeignKeyField () => $ zone ->fields ['id ' ],
339- Source::getForeignKeyField () => $ zone ->fields ['plugin_carbon_sources_id_historical ' ],
340- ]);
341- if ($ source_zone ->isNewItem ()) {
324+ /** @var GlpiLocation $item */
325+ if (!$ source_zone ->getFromDbByLocation ($ item )) {
342326 return false ;
343327 }
344328 return $ source_zone ->fields ['is_download_enabled ' ] === 1 ;
345329 }
346330
347331 /**
348- * Tells id a location has fallback carbon intensity data
332+ * Tells if a location has fallback carbon intensity data
349333 *
350- * @param CommonDBTM $item
334+ * @param GlpiLocation $item
351335 * @return boolean
352336 */
353- public function hasFallbackCarbonIntensityData (CommonDBTM $ item ): bool
337+ public function hasFallbackCarbonIntensityData (GlpiLocation $ item ): bool
354338 {
355339 /** @var DBmysql $DB */
356340 global $ DB ;
357341
358- $ zone = Zone::getByLocation ($ item );
359- if ($ zone === null ) {
360- return false ;
361- }
362342 $ carbon_intensity_table = CarbonIntensity::getTable ();
363- $ carbon_intensity_source_zone_table = Source_Zone::getTable ();
364- $ carbon_intensity_source_table = Source::getTable ();
343+ $ source_zone_table = Source_Zone::getTable ();
344+ $ source_table = Source::getTable ();
345+ $ location_table = Location::getTable ();
365346 $ request = [
366347 'COUNT ' => 'count ' ,
367348 'FROM ' => $ carbon_intensity_table ,
368349 'INNER JOIN ' => [
369- $ carbon_intensity_source_zone_table => [
350+ $ source_zone_table => [
370351 'FKEY ' => [
371352 $ carbon_intensity_table => 'plugin_carbon_zones_id ' ,
372- $ carbon_intensity_source_zone_table => 'plugin_carbon_zones_id ' ,
353+ $ source_zone_table => 'plugin_carbon_zones_id ' ,
373354 ]
374355 ],
375- $ carbon_intensity_source_table => [
356+ $ location_table => [
357+ 'FKEY ' => [
358+ $ location_table => 'plugin_carbon_sources_zones_id ' ,
359+ $ source_zone_table => 'id '
360+ ]
361+ ],
362+ $ source_table => [
376363 'FKEY ' => [
377- $ carbon_intensity_source_zone_table => 'plugin_carbon_sources_id ' ,
378- $ carbon_intensity_source_table => 'id ' ,
364+ $ source_zone_table => 'plugin_carbon_sources_id ' ,
365+ $ source_table => 'id ' ,
379366 ]
380- ]
367+ ],
381368 ],
382369 'WHERE ' => [
383370 Source::getTableField ('is_fallback ' ) => 1 ,
384- Source_Zone ::getTableField ('plugin_carbon_zones_id ' ) => $ zone ->getID (),
371+ Location ::getTableField ('locations_id ' ) => $ item ->getID (),
385372 ],
386373 'ORDER ' => CarbonIntensity::getTableField ('date ' ) . ' DESC ' ,
387374 'LIMIT ' => 1 ,
388375 ];
389376 $ result = $ DB ->request ($ request );
390- return ($ result ->count () > 0 );
377+ return ($ result ->current ()[ ' count ' ] > 0 );
391378 }
392379
393380 /**
0 commit comments