ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
AppDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data\URI;
24 
29 {
30  protected const TABLE_NAME = 'wopi_app';
31 
32  public function __construct(
33  private \ilDBInterface $db
34  ) {
35  }
36 
37  public function getApps(ActionRepository $action_repository): array
38  {
39  $apps = [];
40  $query = 'SELECT * FROM ' . self::TABLE_NAME;
41  $result = $this->db->query($query);
42  while ($row = $this->db->fetchAssoc($result)) {
43  $apps[] = $this->fromDBRow($row, $action_repository);
44  }
45 
46  return $apps;
47  }
48 
49  private function fromDBRow(array $row, ActionRepository $action_repository): App
50  {
51  return new App(
52  (int) $row['id'],
53  (string) $row['name'],
54  $action_repository->getActionsForApp($row['id']),
55  $row['favicon'] ? new URI($row['favicon']) : null
56  );
57  }
58 
59  public function storeCollection(Apps $apps, ActionRepository $action_repository): void
60  {
61  $actions = [];
62  foreach ($apps->getApps() as $app) {
63  $actions = array_merge($actions, $app->getActions());
64  $this->store($app, $action_repository);
65  $actions += $app->getActions();
66  }
67  $action_repository->clearSuperfluous(...$actions);
68  }
69 
70  public function clear(ActionRepository $action_repository): void
71  {
72  $query = 'DELETE FROM ' . self::TABLE_NAME;
73  $this->db->manipulate($query);
74  $action_repository->clear();
75  }
76 
77  public function store(App $app, ActionRepository $action_repository): void
78  {
79  if ($app->getId() === 0) {
80  // check if there is an app with same name
81  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE name = %s';
82  $result = $this->db->queryF($query, ['text'], [$app->getName()]);
83  if ($this->db->numRows($result) > 0) {
84  $row = $this->db->fetchAssoc($result);
85  $app = new App(
86  (int) $row['id'],
87  $app->getName(),
88  $app->getActions(),
89  $app->getFavicon()
90  );
91  }
92  }
93 
94  if ($app->getId() === 0 || $this->db->queryF(
95  'SELECT * FROM ' . self::TABLE_NAME . ' WHERE id = %s',
96  ['integer'],
97  [$app->getId()]
98  )->numRows() === 0) {
99  $app = new App(
100  $this->db->nextId(self::TABLE_NAME),
101  $app->getName(),
102  $app->getActions(),
103  $app->getFavicon()
104  );
105 
106  $query = 'INSERT INTO ' . self::TABLE_NAME . ' (id, name, favicon) VALUES (%s, %s, %s)';
107  $this->db->manipulateF(
108  $query,
109  ['integer', 'text', 'text'],
110  [$app->getId(), $app->getName(), $app->getFavicon()]
111  );
112  } else {
113  $query = 'UPDATE ' . self::TABLE_NAME . ' SET name = %s, favicon = %s WHERE id = %s';
114  $this->db->manipulateF(
115  $query,
116  ['text', 'text', 'integer'],
117  [$app->getName(), $app->getFavicon(), $app->getId()]
118  );
119  }
120 
121  foreach ($app->getActions() as $action) {
122  $action_repository->store($action, $app);
123  }
124  }
125 
126 }
storeCollection(Apps $apps, ActionRepository $action_repository)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
fromDBRow(array $row, ActionRepository $action_repository)
store(App $app, ActionRepository $action_repository)
getApps(ActionRepository $action_repository)
clear(ActionRepository $action_repository)