<?php //------------------------------------------------------------------------------/ // UPDATE EVENT //------------------------------------------------------------------------------/ /** * Tim service, it's a tim success if the update is done, * and a tim failure otherwise. */ use QuickPdo\QuickPdo; use Tim\TimServer\TimServer; use Tim\TimServer\TimServerInterface; require_once __DIR__ . "/../../init.php"; TimServer::create()->start(function (TimServerInterface $server) { if (isset($_POST['id']) && isset($_POST['title']) && isset($_POST['description']) && isset($_POST['start_date']) && isset($_POST['end_date'])) { if (true === QuickPdo::update('the_events', ['title' => $_POST['title'], 'description' => $_POST['description'], 'start_date' => $_POST['start_date'], 'end_date' => $_POST['end_date']], [['id', '=', $_POST['id']]])) { $server->success('ok'); } else { appLog("[app]/www/service/update-event: pdo error: {pdoError}", ['pdoError' => QuickPdo::getLastError()]); $server->error('An error occurred with the database, please retry later.'); } } })->output();
/** * I try to put any db interaction in this file, * so that you have only one file to update to adapt the database to your needs. */ function fetchAllEventsByDateRange($start, $end) { $stmt = 'select * from the_events where start_date >= :start and end_date <= :end'; return QuickPdo::fetchAll($stmt, ['start' => $start, 'end' => $end]); }
<?php use QuickPdo\QuickPdo; require_once __DIR__ . "/functions/az.php"; require_once __DIR__ . "/functions/main.php"; require_once __DIR__ . '/classes/BeeAutoloader.php'; require_once __DIR__ . '/classes/ButineurAutoloader.php'; //------------------------------------------------------------------------------/ // PHP CONFIG //------------------------------------------------------------------------------/ ini_set('error_reporting', -1); mb_internal_encoding('UTF-8'); ini_set('display_errors', 1); //------------------------------------------------------------------------------/ // AUTOLOAD //------------------------------------------------------------------------------/ ButineurAutoLoader::getInst()->addLocation(__DIR__ . "/modules")->start(); //------------------------------------------------------------------------------/ // INIT DATABASE //------------------------------------------------------------------------------/ QuickPdo::setConnection("mysql:dbname=calendar;host=127.0.0.1", 'root', 'root', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
<?php //------------------------------------------------------------------------------/ // DISPLAY EVENT INFO FROM THE DATABASE IN JSON FORMAT //------------------------------------------------------------------------------/ use QuickPdo\QuickPdo; use Tim\TimServer\TimServer; use Tim\TimServer\TimServerInterface; require_once __DIR__ . "/../../init.php"; require_once __DIR__ . "/../../functions/db.php"; TimServer::create()->start(function (TimServerInterface $server) { if (isset($_POST['id'])) { $stmt = 'select * from the_events where id=:id'; $eventInfo = QuickPdo::fetch($stmt, ['id' => $_POST['id']]); $server->success($eventInfo); } })->output();
<?php //------------------------------------------------------------------------------/ // DISPLAY EVENTS FROM THE DATABASE IN JSON FORMAT //------------------------------------------------------------------------------/ use QuickPdo\QuickPdo; require_once __DIR__ . "/../../init.php"; require_once __DIR__ . "/../../functions/db.php"; if (isset($_GET['start']) && isset($_GET['end'])) { $stmt = 'select * from the_events where start_date >= :start and end_date <= :end'; $_events = QuickPdo::fetchAll($stmt, ['start' => $_GET['start'], 'end' => $_GET['end']]); $events = []; foreach ($_events as $e) { $events[] = ['id' => $e['id'], 'title' => $e['title'], 'start' => dateMysqlTime2Iso8601($e['start_date']), 'end' => dateMysqlTime2Iso8601($e['end_date'])]; } echo json_encode($events); }
<?php //------------------------------------------------------------------------------/ // CREATE TODAY'S EVENTS //------------------------------------------------------------------------------/ /** * Launch this script to clear and refresh today's event. */ use QuickPdo\QuickPdo; require_once __DIR__ . "/../init.php"; //------------------------------------------------------------------------------/ // SCRIPT //------------------------------------------------------------------------------/ $lorem = "Lorem ipsum dolor sit amet, id nam munere fabulas. Ei soluta scribentur signiferumque mel. Ea vel prima albucius deleniti, te semper principes pro, cu duis illud nobis cum. Sensibus tincidunt ne duo. Vel ut porro essent."; $day = date('Y-m-d '); $events = [['title' => 'event 1', 'description' => $lorem, 'start_date' => $day . '10:00:00', 'end_date' => $day . '10:35:00'], ['title' => 'event 2', 'description' => $lorem, 'start_date' => $day . '10:05:36', 'end_date' => $day . '10:05:56'], ['title' => 'event 3', 'description' => $lorem, 'start_date' => $day . '11:02:00', 'end_date' => $day . '11:12:30'], ['title' => 'event 4', 'description' => $lorem, 'start_date' => $day . '13:15:00', 'end_date' => $day . '15:15:00']]; a("clear the the_events table"); a(QuickPdo::delete('the_events')); a("inserting events"); foreach ($events as $event) { a(QuickPdo::insert('the_events', $event)); }
require_once __DIR__ . "/../../../../init.php"; use Meredith\Exception\MeredithException; use Meredith\Supervisor\MeredithSupervisor; use QuickPdo\QuickPdo; use Tim\TimServer\TimServer; use Tim\TimServer\TimServerInterface; TimServer::create()->start(function (TimServerInterface $server) { if (isset($_POST['formId']) && isset($_POST['ids']) && is_array($_POST['ids'])) { $ids = $_POST['ids']; $view = (string) $_POST['formId']; if (true === MeredithSupervisor::inst()->isGranted($view, 'delete')) { array_walk($ids, function (&$v) { $v = (int) $v; }); if ($ids) { if (false !== ($nbDelete = QuickPdo::delete($view, "id in (" . implode(", ", $ids) . ")"))) { $server->success("ok"); } else { $server->error(MeredithSupervisor::inst()->translate("An error occurred with the database, please retry later")); } } else { $server->success("ok"); } } else { throw new MeredithException("Permission not granted to access rows with {$view}"); } } else { $server->error(MeredithSupervisor::inst()->translate("Invalid data")); } })->output();
$requestFields = $lh->getRequestFields(); $stmt .= "select " . implode(', ', $requestFields) . " from {$fromClause}"; $stmt .= $sWhere; $stmt .= $sOrder; $stmt .= " limit {$start}, {$length}"; if (false !== ($res = QuickPdo::fetchAll($stmt, $markers))) { $ret['data'] = []; foreach ($res as $k => $row) { $row = ['DT_RowId' => $row['id']] + $row; $ret['data'][$k] = $row; } } // recordFiltered $stmtCountFiltered = "select count(*) as count from {$fromClause}"; $stmtCountFiltered .= $sWhere; if (false !== ($info = QuickPdo::fetch($stmtCountFiltered, $markers))) { $ret['recordsFiltered'] = (int) $info['count']; } } } } catch (\Exception $e) { $ret['error'] = MeredithSupervisor::inst()->translate("Oops! An error occurred, please retry later"); MeredithSupervisor::inst()->log("Oops! An error occurred, please retry later: {$stmt} -- " . $e->getMessage()); } } } else { throw new MeredithException("Permission not granted to access rows with {$formId}"); } } else { throw new \Exception("An error occurred"); // but we don't care
ButineurAutoloader::getInst()->addLocation(__DIR__ . "/planets")->start(); //------------------------------------------------------------------------------/ // GENERAL //------------------------------------------------------------------------------/ define('APP_DIR', __DIR__); //------------------------------------------------------------------------------/ // DB SETTINGS //------------------------------------------------------------------------------/ define("MYSQL_DBNAME", "adaman"); define("PDOCONF_DSN", "mysql:dbname=" . MYSQL_DBNAME . ";host=127.0.0.1"); define("PDOCONF_USER", "root"); define("PDOCONF_PASS", "root"); define('COOKIE_DOMAIN', ""); // Initialize pdo connection now, since we will need it for almost every request. //------------------------------------------------------------------------------/ QuickPdo::setConnection(PDOCONF_DSN, PDOCONF_USER, PDOCONF_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)); //------------------------------------------------------------------------------/ // MEREDITH plugin //------------------------------------------------------------------------------/ MeredithSupervisor::inst()->setGetMainControllerCb(function ($formId) { $mc = MainControllerProvider::create()->setDir(APP_DIR . "/pages/meredith/main-controllers")->getMainController($formId); // getMainController either works as expected, or throws an Exception in your face ... return $mc; })->setIsGranted(function ($formId, $action) { // your application permission system here... return true; })->setTranslateCb(function ($msg) { if (false !== ($tr = EnglishDictionary::inst()->search($msg))) { return $tr; } return $msg;
<?php require_once __DIR__ . "/../../../../init.php"; use Meredith\Exception\MeredithException; use Meredith\Supervisor\MeredithSupervisor; use QuickPdo\QuickPdo; use Tim\TimServer\TimServer; use Tim\TimServer\TimServerInterface; TimServer::create()->start(function (TimServerInterface $server) { if (isset($_POST['formId']) && isset($_POST['id'])) { $id = (int) $_POST['id']; $view = (string) $_POST['formId']; if (true === MeredithSupervisor::inst()->isGranted($view, 'fetch')) { if (false !== ($info = QuickPdo::fetch("select * from {$view} where id={$id}"))) { $server->success($info); } else { $server->error(MeredithSupervisor::inst()->translate("An error occurred with the database, please retry later")); } } else { throw new MeredithException("Permission not granted to access rows with {$view}"); } } else { $server->error(MeredithSupervisor::inst()->translate("Invalid data")); } })->output();
if (true === QuickPdo::update($table, $nac2Values, $where)) { $msg = $mc->getFormDataProcessor()->getSuccessMessage($formId, 'update'); if (false === $msg) { $msg = MeredithSupervisor::inst()->translate("The record has been successfully updated"); } $server->success(['msg' => $msg]); } else { $server->error(MeredithSupervisor::inst()->translate("An error occurred with the database, please retry later")); } } else { throw new MeredithException("Permission not granted to update with {$formId}"); } } else { // insert if (true === MeredithSupervisor::inst()->isGranted($formId, 'insert')) { if (false !== ($id = QuickPdo::insert($table, $nac2Values))) { $msg = $mc->getFormDataProcessor()->getSuccessMessage($formId, 'insert'); if (false === $msg) { $msg = MeredithSupervisor::inst()->translate("The record has been successfully recorded"); } $server->success(['msg' => $msg]); } else { $server->error(MeredithSupervisor::inst()->translate("An error occurred with the database, please retry later")); } } else { throw new MeredithException("Permission not granted to insert with {$formId}"); } } } catch (\PDOException $e) { if ('23000' === $e->getCode()) { // integrity constraint violation