MaterioFlagCleaning.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Drupal\materio_flag\Commands;
  3. use Drush\Commands\DrushCommands;
  4. use Drupal\Component\DependencyInjection\ContainerInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\flag_lists\FlagListsService;
  7. use Drupal\Core\Session\AccountProxyInterface;
  8. class MaterioFlagCleaning extends DrushCommands {
  9. /**
  10. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  11. */
  12. protected $entityTypeManager;
  13. /**
  14. * @var \Drupal\flag_lists\FlagListsService
  15. */
  16. protected $flagListsService;
  17. /**
  18. * @var \Drupal\user\User
  19. */
  20. protected $user;
  21. // /**
  22. // * {@inheritdoc}
  23. // */
  24. // public static function create(ContainerInterface $container) {
  25. // return new static(
  26. // $container->get('entity_type.manager'),
  27. // $container->get('flaglists'),
  28. // $container->get('current_user')
  29. // );
  30. // }
  31. /**
  32. * Constructs a new MaterioFlagController object.
  33. */
  34. public function __construct(EntityTypeManagerInterface $entity_type_manager, FlagListsService $flag_lists_service, AccountProxyInterface $account) {
  35. $this->entityTypeManager = $entity_type_manager;
  36. $this->flagListsService = $flag_lists_service;
  37. $this->user = $account;
  38. }
  39. /**
  40. * Try to recreate missing related flags.
  41. *
  42. * @command materio_flag:recreate_missing_related_flags
  43. * @aliases mf-rmrf
  44. */
  45. public function recreate_missing_related_flags() {
  46. $database = \Drupal::database();
  47. $this->output()->writeln("");
  48. $this->output()->writeln("Missing related flag Flagging collection cleaning");
  49. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  50. ->fields('fcfd', ['id', 'relatedflag']);
  51. $fcfd_result = $fcfd_query->execute();
  52. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections");
  53. $fc_missing_relatedflag = 0;
  54. foreach ($fcfd_result as $fc) {
  55. $flag_query = $database->select('config', 'c')
  56. ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  57. ->fields('c', ['name']);
  58. // $conf_result = $flag_query->execute();
  59. $count = $flag_query->countQuery()->execute()->fetchField();
  60. if(!$count){
  61. $fc_missing_relatedflag ++;
  62. $flagList = $this->flagListsService->getFlaggingCollectionById($fc->id);
  63. $this->output()->writeln(' flagging_collection ' . $flagList->getName() . ' ('. $fc->id . ') has ' . $count . " relatedflag");
  64. $flagList->save();
  65. }
  66. }
  67. $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
  68. }
  69. /**
  70. * Clean multiple flag list for one related flag.
  71. *
  72. * @command materio_flag:clean_duplicates_related_flags
  73. * @aliases mf-cdrf
  74. */
  75. public function clean_duplicates_related_flags() {
  76. // searching for duplicates relatedflags
  77. $database = \Drupal::database();
  78. $this->output()->writeln("");
  79. $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
  80. $dup_query = $database->select('flagging_collection_field_data', 'fcfd')
  81. ->fields('fcfd', ['relatedflag'])
  82. ->groupBy('fcfd.relatedflag')
  83. ->having('COUNT(*) > 1');
  84. $dup_query->addExpression('COUNT(*)', 'count');
  85. // $results = $query->execute()->fetchAll();
  86. $dup_result = $dup_query->execute();
  87. $this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags");
  88. foreach ($dup_result as $dup){
  89. // $this->output()->writeln($dup->relatedflag . " is dup");
  90. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  91. ->condition('fcfd.relatedflag', $dup->relatedflag)
  92. ->fields('fcfd', ['id', 'relatedflag']);
  93. $fcfd_result = $fcfd_query->execute();
  94. $count = $fcfd_query->countQuery()->execute()->fetchField();
  95. $this->output()->writeln($dup->relatedflag . ' relatedflag has ' . $count . ' flagging collection');
  96. foreach ($fcfd_result as $fc) {
  97. $flagList = $this->flagListsService->getFlaggingCollectionById($fc->id);
  98. $flagList->save();
  99. // delete_flagingcollection($fc, $database, $delete_all ? true : $delete_dups);
  100. }
  101. }
  102. }
  103. }