protected function deleteSingleAssignment($bookAssignmentId) { try { $bookAssignment = $this->_em->find('DM:SchbasUserShouldLendBook', $bookAssignmentId); if (!$bookAssignment) { dieHttp('Buchzuweisung nicht gefunden', 400); } $this->_em->remove($bookAssignment); $this->_em->flush(); die('Buchzuweisung erfolgreich gelöscht.'); } catch (\Exception $e) { $this->_logger->logO('Could not delete a single book-assignment', ['sev' => 'error', 'moreJson' => $e->getMessage()]); dieHttp('Konnte die Buchzuweisung nicht löschen', 500); } }
public function execute($dataContainer) { $this->entryPoint($dataContainer); $selectors = $this->selectorQueryPartGet($_GET); $activeSchoolyear = $this->_em->getRepository('DM:SystemSchoolyears')->findOneByActive(true); if (!$activeSchoolyear) { dieHttp('Konnte aktives Schuljahr nicht finden.', 500); } try { $qb = $this->_em->createQueryBuilder()->select($selectors)->from('DM:SystemUsers', 'u'); //Additional data if (isset($_GET['includeShouldLendBooks'])) { $qb->leftJoin('u.booksToLend', 'btl'); $qb->leftJoin('btl.book', 'usb'); } if (isset($_GET['includeLendingBooks'])) { $qb->leftJoin('u.bookLending', 'bl'); $qb->leftJoin('bl.book', 'blb'); } //Filters if (isset($_GET['grade'])) { $grade = $this->_em->find('DM:SystemGrades', $_GET['grade']); if (!$grade) { dieHttp("Klasse mit Id {$_GET['grade']} nicht gefunden", 400); } $qb->innerJoin('u.attendances', 'grade_a', 'WITH', 'grade_a.schoolyear = :activeSchoolyear'); $qb->innerJoin('grade_a.grade', 'gg', 'WITH', 'gg = :grade'); $qb->setParameter('activeSchoolyear', $activeSchoolyear); $qb->setParameter('grade', $grade); } if (isset($_GET['gradelevel'])) { $gradelevel = filter_input(INPUT_GET, 'gradelevel', FILTER_VALIDATE_INT); if ($gradelevel === false || $gradelevel === null) { dieHttp("Klassenstufe '{$gradelevel}' nicht korrekt", 400); } $qb->innerJoin('u.attendances', 'gl_a', 'WITH', 'gl_a.schoolyear = :activeSchoolyear'); $qb->innerJoin('gl_a.grade', 'gl_g', 'WITH', 'gl_g.gradelevel = :gradelevel'); $qb->setParameter('activeSchoolyear', $activeSchoolyear); $qb->setParameter('gradelevel', $gradelevel); } $query = $qb->getQuery(); $users = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); dieJson($users); } catch (\Exception $e) { $this->_logger->logO('Could not search for users', ['sev' => 'error', 'moreJson' => $e->getMessage()]); dieHttp('Fehler beim Suchen.', 500); } }
protected function changeStatus($newStatus) { $statusEntry = $this->_em->getRepository('DM:SystemGlobalSettings')->findOneByName('isSchbasClaimEnabled'); if (!$statusEntry) { $this->_logger->logO('Could not find isSchbasClaimEnabled', ['sev' => 'error']); dieHttp('Konnte Einstellung nicht finden', 500); } if ($statusEntry->getValue() != $newStatus) { $val = $newStatus ? 1 : 0; $statusEntry->setValue($val); $this->_em->flush(); die('Status wurde erfolgreich verändert'); } else { die('Status hat gleichen Wert. Er wurde nicht verändert.'); } }
protected function preparationSchoolyearChange($id) { $schoolyear = $this->_em->find('DM:SystemSchoolyears', $id); if (!$schoolyear) { $this->_logger->log('Could not find the schoolyear', ['sev' => 'error', 'moreJson' => ['id' => $id]]); dieHttp('Das Schuljahr wurde nicht gefunden', 422); } $configEntry = $this->_em->getRepository('DM:SystemGlobalSettings')->findOneByName('schbasPreparationSchoolyearId'); if (!$configEntry) { $this->_logger->log('Could not find the ' . 'schbasPreparationSchoolyearId', 'error'); dieHttp('Die Einstellung wurde nicht gefunden', 500); } $configEntry->setValue($schoolyear->getId()); $this->_em->persist($configEntry); $this->_em->flush(); die('Schuljahr erfolgreich verändert.'); }
protected function assignmentsDeleteFor($delEntity, $bookId, $entityId, $schoolyearId) { $schoolyear = $this->_em->find('DM:SystemSchoolyears', $schoolyearId); $book = $this->_em->find('DM:SchbasBook', $bookId); if (!$schoolyear) { dieHttp('Schuljahr nicht gefunden', 400); } if (!$book) { dieHttp('Buch nicht gefunden', 400); } // DQL does not support delete with joins, so select them first // and delete them after that $qb = $this->_em->createQueryBuilder()->select('usb')->from('DM:SchbasUserShouldLendBook', 'usb'); switch ($delEntity) { case 'book': // We want to delete all assignments for the book, no filtering // necessary break; case 'gradelevel': $qb->innerJoin('usb.user', 'u')->innerJoin('u.attendances', 'a')->innerJoin('a.schoolyear', 's', 'WITH', 's = :schoolyear')->innerJoin('a.grade', 'g', 'WITH', 'g.gradelevel = :gradelevel')->setParameter('gradelevel', $entityId); break; case 'grade': $grade = $this->_em->getReference('DM:SystemGrades', $entityId); $qb->innerJoin('usb.user', 'u')->innerJoin('u.attendances', 'a')->innerJoin('a.schoolyear', 's', 'WITH', 's = :schoolyear')->innerJoin('a.grade', 'g', 'WITH', 'g = :grade'); $qb->setParameter('grade', $grade); break; case 'user': $user = $this->_em->getReference('DM:SystemUsers', $entityId); $qb->andWhere('usb.user = :user'); $qb->setParameter('user', $user); break; } $qb->andWhere('usb.book = :book'); $qb->andWhere('usb.schoolyear = :schoolyear'); $qb->setParameter('schoolyear', $schoolyear); $qb->setParameter('book', $book); $query = $qb->getQuery(); $entries = $query->getResult(); foreach ($entries as $entry) { $this->_em->remove($entry); } $this->_em->flush(); return count($entries); }
protected function searchByTitle($title, $entryCount) { try { $query = $this->_em->createQuery('SELECT b FROM DM:SchbasBook b WHERE b.title LIKE :title '); $query->setParameter('title', "%{$title}%"); $query->setMaxResults($entryCount); $books = $query->getResult(); $bookArray = []; foreach ($books as $book) { $bookArray[] = ['id' => $book->getId(), 'title' => $book->getTitle()]; } return $bookArray; } catch (\Exception $e) { $this->_logger->logO('Could not search the books by title', ['sev' => 'error', 'moreJson' => ['title' => $title, 'msg' => $e->getMessage()]]); dieHttp('Konnte nicht nach dem Buch suchen', 500); } }
protected function barcodesDelete($barcodeStrings) { foreach ($barcodeStrings as $barcodeStr) { $barcode = new \Babesk\Schbas\Barcode(); if (!$barcode->initByBarcodeString($barcodeStr)) { dieHttp("Der Barcode '{$barcodeStr}' ist nicht korrekt", 400); } $bookCopy = $barcode->getMatchingBookExemplar($this->_em); if ($bookCopy) { foreach ($bookCopy->getLending() as $lending) { $this->_em->remove($lending); } $this->_em->remove($bookCopy); } else { echo "<p>Kein Buchexemplar zu Barcode {$barcodeStr} gefunden. " . "</p>"; } } $this->_em->flush(); die('Die Exemplare wurden erfolgreich gelöscht'); }
protected function entryChange($name, $value) { try { $entry = $this->_em->getRepository('DM:SystemGlobalSettings')->findOneByName($name); } catch (Exception $e) { dieHttp('Konnte Eintrag nicht abrufen', 500); } if ($entry) { try { $entry->setValue($value); $this->_em->persist($entry); $this->_em->flush(); } catch (Exception $e) { dieHttp('Konnte Eintrag nicht ändern', 500); } } else { dieHttp('Eintrag nicht gefunden.', 400); } die('Eintrag erfolgreich geändert.'); }
protected function searchByGradename($gradename, $entryCount) { try { $query = $this->_em->createQuery('SELECT g FROM DM:SystemGrades g WHERE CONCAT(g.gradelevel, g.label) LIKE :gradename '); $query->setParameter('gradename', "%{$gradename}%"); $query->setMaxResults($entryCount); $grades = $query->getResult(); $gradeArray = []; if (count($grades)) { foreach ($grades as $grade) { $gradeArray[] = ['id' => $grade->getId(), 'gradename' => $grade->getGradelevel() . $grade->getLabel()]; } } return $gradeArray; } catch (\Exception $e) { $this->_logger->logO('Could not search the grades by gradename ', ['sev' => 'error', 'moreJson' => ['gradename' => $gradename, 'msg' => $e->getMessage()]]); dieHttp('Konnte nicht nach der Klasse suchen', 500); } }
protected function searchGradelevel($gradelevel, $entryCount) { try { $query = $this->_em->createQuery('SELECT g FROM DM:SystemGrades g WHERE g.gradelevel LIKE :gradelevel GROUP BY g.gradelevel '); $query->setParameter('gradelevel', "%{$gradelevel}%"); $query->setMaxResults($entryCount); $grades = $query->getResult(); $gradelevelArray = []; if (count($grades)) { foreach ($grades as $grade) { $gradelevelArray[] = ['gradelevel' => $grade->getGradelevel()]; } } return $gradelevelArray; } catch (\Exception $e) { $this->_logger->logO('Could not search the gradelevels', ['sev' => 'error', 'moreJson' => ['gradelevel' => $gradelevel, 'msg' => $e->getMessage()]]); dieHttp('Konnte nicht nach der Klassenstufe suchen', 500); } }
protected function bookDataSend($schoolyearId) { try { $schoolyears = $this->schoolyearDataGet($schoolyearId); } catch (Exception $e) { $this->_logger->logO('Could not fetch the schoolyears', ['sev' => 'error', 'moreJson' => $e->getMessage()]); dieHttp('Konnte die Schuljahre nicht abrufen', 500); } try { if (!$schoolyearId) { foreach ($schoolyears as $schoolyear) { if ($schoolyear['active']) { $schoolyearId = $schoolyear['id']; } } } $books = $this->bookDataGet($schoolyearId); } catch (Exception $e) { $this->_logger->logO('Could not fetch the books', ['sev' => 'error', 'moreJson' => $e->getMessage()]); dieHttp('Konnte die Buchzuweisungen nicht abrufen', 500); } dieJson(['schoolyears' => $schoolyears, 'books' => $books]); }
protected function searchByUsernameAndSchoolyear($username, $schoolyear, $entryCount) { try { $query = $this->_em->createQuery('SELECT u FROM DM:SystemUsers u INNER JOIN u.attendances a WITH a.schoolyear = :schoolyear WHERE u.username LIKE :username '); $query->setParameter('username', "%{$username}%"); $query->setParameter('schoolyear', $schoolyear); $query->setMaxResults($entryCount); $users = $query->getResult(); $userArray = []; if (count($users)) { foreach ($users as $user) { $userArray[] = ['id' => $user->getId(), 'username' => $user->getUsername()]; } } return $userArray; } catch (\Exception $e) { $this->_logger->logO('Could not search the users by username ' . 'and schoolyear', ['sev' => 'error', 'moreJson' => ['username' => $username, 'msg' => $e->getMessage()]]); dieHttp('Konnte nicht nach dem Benutzernamen suchen', 500); } }
/** * Adds the new book-assignments to the given entity * @param int $bookId The book-id of the book to assign * @param string $type The type of the entity to assign the books * to * @param int $id The identifier of the entity * @param int $schoolyearId The schoolyear-id of the entities and * assignments */ protected function assignmentsToEntityAdd($bookId, $type, $id, $schoolyearId) { $book = $this->_em->getReference('DM:SchbasBook', $bookId); $schoolyear = $this->_em->getReference('DM:SystemSchoolyears', $schoolyearId); try { $users = $this->usersGetByEntity($type, $id, $schoolyear); if ($users) { $addedCount = 0; $jumpedCount = 0; foreach ($users as $user) { $existingAssignments = $user->getBooksToLend(); foreach ($existingAssignments as $assignment) { $existingBook = $assignment->getBook(); if ($existingBook == $book) { $jumpedCount++; continue 2; } } $entry = new \Babesk\ORM\SchbasUserShouldLendBook(); $entry->setUser($user); $entry->setBook($book); $entry->setSchoolyear($schoolyear); $this->_em->persist($entry); $addedCount++; } $this->_em->flush(); $usercount = count($users); die("Die Zuweisungen wurden erfolgreich hinzugefügt.<br>" . "<b>{$addedCount}</b> wurden hinzugefügt,<br>" . "<b>{$jumpedCount}</b> wurden übersprungen"); } else { dieHttp('Konnte die Benutzer zum Hinzufügen nicht abrufen', 500); } } catch (\Exception $e) { $this->_logger->logO('Could not add the assignments', ['sev' => 'error', 'moreJson' => ['bookId' => $bookId, 'entityType' => $type, 'entityId' => $id, 'schoolyearId' => $schoolyearId, 'msg' => $e->getMessage()]]); dieHttp('Ein Fehler ist beim Hinzufügen der Zuweisungen ' . 'aufgetreten', 500); } }
/** * Search for a username in all users with a specific conflict type * It searches with the good ol' Levenshtein-method, so be easy on it. * Rendering a 200 with json on success, a 204 if no users found or an * error on error. * @param string $username The username to search for * @param string $type The conflict-type. Has to be one of * CsvOnlyConflict and DbOnlyConflict */ private function searchForUsernameInConflictsOfType($username, $type) { $limit = 30; if ($type == 'CsvOnlyConflict') { $joinQuery = 'INNER JOIN UserUpdateTempUsers u ' . 'ON u.ID = c.tempUserId'; } else { if ($type == 'DbOnlyConflict') { $joinQuery = 'INNER JOIN SystemUsers u ON u.ID = c.origUserId'; } else { $this->_logger->logO('Type not recognized', ['sev' => 'warning', ['moreJson'] => ['type' => $type]]); $this->_interface->dieError('Ein Fehler ist aufgetreten'); } } $query = "SELECT c.ID as conflictId, u.birthday AS userBirthday,\n\t\t\t\t\tCONCAT(u.forename, ' ', u.name) AS username\n\t\t\t\tFROM UserUpdateTempConflicts c\n\t\t\t\t{$joinQuery}\n\t\t\t\tWHERE c.type = :conflictType\n\t\t\t\tORDER BY LEVENSHTEIN_RATIO(username, :username) DESC, u.ID\n\t\t\t\tLIMIT :limit"; try { $stmt = $this->_em->getConnection()->prepare($query); $stmt->bindParam('conflictType', $type); $stmt->bindParam('username', $username); $stmt->bindParam('limit', $limit, \PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); } catch (\Exception $e) { $this->_logger->logO('Could not search usernames for conflicttype', ['sev' => 'error', 'moreJson' => ['msg' => $e->getMessage(), 'conflicttype' => $type]]); dieHttp('Fehler beim Suchen der ähnlichen Benutzer', 500); } if (count($result)) { $conflicts = []; foreach ($result as $row) { $formattedBirthday = date('d.m.Y', strtotime($row['userBirthday'])); $conflicts[] = ['id' => $row['conflictId'], 'label' => $row['username'] . " ({$formattedBirthday})"]; } dieJson($conflicts); } else { dieHttp('Keine ähnlichen Benutzer gefunden.', 204); } }
protected function assignmentsForSingleUserCreate($userId) { $user = $this->_em->find('DM:SystemUsers', $userId); if (!$user) { dieJson('Benutzer nicht gefunden', 400); } $loanHelper = new \Babesk\Schbas\Loan($this->_dataContainer); $loanGenerator = new \Babesk\Schbas\ShouldLendGeneration($this->_dataContainer); $schoolyear = $loanHelper->schbasPreparationSchoolyearGet(); $bookAssignments = $this->_em->getRepository('DM:SchbasUserShouldLendBook')->findBy(['user' => $user, 'schoolyear' => $schoolyear]); if (count($bookAssignments)) { foreach ($bookAssignments as $bookAssignment) { $this->_em->remove($bookAssignment); } $this->_em->flush(); } $res = $loanGenerator->generate(['onlyForUsers' => [$user], 'schoolyear' => $schoolyear]); if ($res) { die('Die Zuweisungen wurden erfolgreich erstellt.'); } else { $this->_logger->log('Could not create the assignments', 'error'); dieHttp('Konnte die Zuweisungen nicht erstellen!', 500); } }
protected function updateSingleUser() { $userId = filter_input(INPUT_POST, 'userId'); if ($userId) { $user = $this->_em->getReference('DM:SystemUsers', $userId); require_once 'PatchUser.php'; $patcher = new PatchUser($this->_dataContainer); $patcher->patch($user, $_POST); } else { dieHttp('Keine Benutzer-ID übergeben.', 400); } }
protected function sendIndexCheckInput($entriesPerPage) { if ($entriesPerPage < 0 || $entriesPerPage > 1000) { dieHttp('Inkorrekte Eingabe: Einträge pro Seite', 400); } }
public function executeModule() { try { $execCom = $this->_moduleExecutionParser->executionCommandGet(); $this->_smarty->assign('moduleExecCommand', $execCom); $genManager = $this->_acl->moduleGeneratorManagerGet(); $module = $genManager->moduleByPathGet($execCom->pathGet()); if ($module) { $this->_smarty->assign('moduleExecutedId', $module->getId()); } $this->_acl->moduleExecute($execCom, $this->dataContainerCreate()); } catch (AclAccessDeniedException $e) { if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { dieHttp('Keine Berechtigung', 401); } else { $this->_adminInterface->dieError('Keine Berechtigung!'); } } catch (AclModuleLockedException $e) { if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { dieHttp('Modul gesperrt', 423); } else { $this->_adminInterface->dieError('Modul gesperrt'); } } catch (Exception $e) { $this->_logger->log('Error executing a Module', 'Notice', Null, json_encode(array('command' => $execCom->pathGet(), 'userId' => $_SESSION['UID'], 'exceptionType' => get_class($e), 'msg' => $e->getMessage()))); if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { http_response_code(500); //It was an Ajax-Call, dont show the whole Website die('Konnte das Modul nicht ausführen!'); } else { $this->_adminInterface->dieError('Konnte das Modul nicht ausführen!'); } } }
/** * Adds new copies of books * Needs the given bookIds, it does not re-check if everything is correct * @param array $barcodeContainers */ protected function inventoryWithBooksAdd($barcodeContainers) { $barcodes = []; foreach ($barcodeContainers as $container) { if (!isset($container['bookId']) || !isset($container['barcode'])) { dieHttp('Inkorrekte Daten wurden übergeben.', 400); } $bookId = $container['bookId']; $barcodeStr = $container['barcode']; $barcode = new \Babesk\Schbas\Barcode(); if ($barcode->initByBarcodeString($barcodeStr)) { $barcodes[] = $barcode; } else { dieHttp("Der Barcode '{$barcodeStr}' ist nicht korrekt", 400); } $book = $this->_em->getReference('DM:SchbasBook', $bookId); $inventory = new \Babesk\ORM\SchbasInventory(); $inventory->setBook($book); $inventory->setYearOfPurchase($barcode->getPurchaseYear()); $inventory->setExemplar($barcode->getExemplar()); $this->_em->persist($inventory); } try { $this->_em->flush(); } catch (\Doctrine\DBAL\DBALException $e) { if ($e->getPrevious()->getCode() === '23000') { dieHttp('Ein oder mehrere angegebene Barcodes gibt es schon!', 400); } else { throw $e; } } die('Die Buch-Exemplare wurden erfolgreich hinzugefügt.'); }