protected static function select($a) { self::init(); if ($a === 'write') { if (self::$_writeConnected && self::$_last === 'write') { return; } foreach (self::$_mcs as $i => $c) { R::selectDatabase("write:{$i}"); if (R::testConnection()) { R::freeze(true); self::$_writeConnected = true; self::$_last = 'write'; return; } } throw new \Exception('Master DB have down'); } if ($a === 'read') { if (self::$_readConnected && self::$_last === 'read') { return; } foreach (self::$_scs as $i => $c) { R::selectDatabase("read:{$i}"); if (R::testConnection()) { R::freeze(true); self::$_readConnected = true; self::$_last = 'read'; return; } } throw new \Exception('Slave and master DB have down'); } }
/** * Connect to existing database handle with RedBeans */ public static function connectExisting() { static $connected = null; if (!$connected) { R::setup(connect::$dbh); $connected = true; } $freeze = conf::getMainIni('rb_freeze'); if ($freeze == 1) { R::freeze(true); } }
/** * @param $name * @return mixed */ public function setDb($name) { $this->options = $this->db[$name]; R::setAutoResolve(TRUE); R::selectDatabase($name); R::ext('xdispense', function ($type) { return R::getRedBean()->dispense($type); }); if ($this->cache) { R::useWriterCache(true); R::freeze(TRUE); } return $name; }
/** * Insert or update a facebook user. * * @param array $payload */ public static function upsert($payload) { R::freeze(false); // if no session set add it if (!isset($payload['session'])) { $payload['session'] = session_id(); } // Find an existing user in db by session $user = R::findOne(USER, 'session = :session AND ip = :ip ', array(':session' => $payload['session'], ':ip' => $payload['ip'])); // if we have a payload with id set the facebook id if (isset($payload['id'])) { // change the id into fbid. $payload['fbid'] = '' . $payload['id']; unset($payload['id']); } if (isset($payload['email'])) { $emailuser = R::findOne(USER, 'email = :email ', array(':email' => $payload['email'])); if ($emailuser) { $user = $emailuser; } } if (isset($payload['fbid'])) { $fbiduser = R::findOne(USER, 'fbid = :fbid ', array(':fbid' => $payload['fbid'])); if ($fbiduser) { $user = $fbiduser; } } // the date $currentDate = new \DateTime(); if (!$user) { $user = R::xdispense(USER); $user->createdate = $currentDate->format('Y-m-d H:i:s'); } $user->import($payload); // update the lastupdated timestamp $user->lastupdate = $currentDate->format('Y-m-d H:i:s'); R::store($user); }
/** */ function populate() { // do not execute this function return; R::freeze(false); R::nuke(); $quiz = R::xdispense(QUIZ); $quiz->theme = 'starwars'; $quiz->nextaction = '/'; R::store($quiz); $question = R::xdispense(QUESTION); $question->title = "Which Star Wars Character Are You?!"; R::store($question); $question->{QUIZ} = $quiz; R::store($question); $answer = R::xdispense(ANSWER); $answer->title = 'Han Solo'; $answer->message = "You have a strong and determined personality, but beneath your tough exterior you have a loving heart and an inner bravery that will help you through the tough times. You learn from your mistakes and always stay true to yourself!"; $answer->sharemessage = "I have a strong and determined personality, but beneath my tough exterior I have a loving heart and an inner bravery that helps me through the tough times. I learn from my mistakes and always stay true to myself! Who would you be?"; $answer->image = 'A'; $answer->{QUIZ} = $quiz; $answer->{QUESTION} = $question; R::store($answer); $answer = R::xdispense(ANSWER); $answer->title = 'Luke Skywalker'; $answer->message = "You're courageous, eager for adventure and an all-round hero. Sometimes you find it hard to control your emotions, but you always have the happiness and wellbeing of others at heart!"; $answer->sharemessage = "I'm courageous, eager for adventure and an all-round hero. Sometimes I find it hard to control my emotions, but I always have the happiness and wellbeing of others at heart! Who would you be?"; $answer->image = 'B'; $answer->{QUIZ} = $quiz; $answer->{QUESTION} = $question; R::store($answer); $answer = R::xdispense(ANSWER); $answer->title = 'Princess Leia'; $answer->image = 'C'; $answer->message = "You're level-headed, courageous, and with a sharp-tongued wit surpassed only by your beauty. You've been through some tough times but always come out stronger in the end!"; $answer->sharemessage = "I'm level-headed, courageous, and with a sharp-tongued wit surpassed only by my beauty. I've been through some tough times but always come out stronger in the end! Who would you be?"; $answer->{QUIZ} = $quiz; $answer->{QUESTION} = $question; R::store($answer); $answer = R::xdispense(ANSWER); $answer->title = 'Chewbacca'; $answer->image = 'D'; $answer->message = "You may look tough, and even scary to some people, but deep down you're a big softie. You're loyal, affectionate and humble - but if you think something isn't fair you're not afraid to say so! "; $answer->sharemessage = "I may look tough, and even scary to some people, but deep down I'm a big softie. I'm loyal, affectionate and humble - but if I think something isn't fair I'm not afraid to say so! Who would you be?"; $answer->{QUIZ} = $quiz; $answer->{QUESTION} = $question; R::store($answer); $answer = R::xdispense(ANSWER); $answer->title = 'Finn'; $answer->image = 'E'; $answer->message = "Despite what life has thrown at you, you have a good heart and true empathy for other people. You are brave, intelligent and with the strength to face down whatever life throws at you!"; $answer->sharemessage = "Despite what life has thrown at me, I have a good heart and true empathy for other people. I am brave, intelligent and with the strength to face down whatever life throws at me! Who would you be?"; $answer->{QUIZ} = $quiz; $answer->{QUESTION} = $question; R::store($answer); $answer = R::xdispense(ANSWER); $answer->title = 'Rey'; $answer->image = 'F'; $answer->message = "You have a heart full of generosity and a desire to help others, often putting their needs before your own. You have a great imagination that sets you apart from the majority of others!"; $answer->sharemessage = "I have a heart full of generosity and a desire to help others, often putting their needs before my own. I have a great imagination that sets me apart from the majority of others! Who would you be?"; $answer->{QUIZ} = $quiz; $answer->{QUESTION} = $question; R::store($answer); $quiz_user = array('createdate' => '2016-02-08 19:20:37', 'name' => 'Matteo Monti Matteo Monti Matteo Monti Matteo Monti Matteo Monti', 'email' => 'mmonti@gmail.commmonti@gmail.commmonti@gmail.commmonti@gmail.commmonti@', 'gender' => 'maleorfemale', 'ip' => '151.237.238.110', 'fbid' => '1a01544915asdasdasdasdasdasd51609838e16', 'lastupdate' => '2016-02-08 19:20:37', 'city' => 'Leccooranotherlocationwhatever', 'country' => 'Leccooranotherlocationwhatever', 'latitude' => '45.85a', 'longitude' => '9.38333a'); $user = R::xdispense(USER); $user->import($quiz_user); R::store($user); R::wipe(USER); R::freeze(true); return $quiz; }
$logger = $c->get('logger'); $session = $c->get('session'); $settings = $c->get('settings')['facebook']; $connect = new Connect($settings, $session, $logger); return $connect; }; $container['database'] = function ($c) { $logger = $c->get('logger'); $settings = $c->get('settings')['database']; define('QUESTION', 'quiz_question'); define('ANSWER', 'quiz_answer'); define('QUIZ', 'quiz_quiz'); define('USER', 'quiz_user'); // Create an extension to by-pass security check in R::dispense R::ext('xdispense', function ($type) { return R::getRedBean()->dispense($type); }); R::renameAssociation(['quiz_answer_quiz_question' => 'quiz_answer_question', 'quiz_answer_quiz_quiz' => 'quiz_answer_quiz', 'quiz_question_quiz_quiz' => 'quiz_question_quiz']); define('REDBEAN_MODEL_PREFIX', '\\App\\Model\\'); $connectionString = 'mysql:host=' . $settings['host'] . ';dbname=' . $settings['dbname']; R::setup($connectionString, $settings['username'], $settings['password']); R::useWriterCache(true); //R::debug(true); // // NEVER REMOVE THIS! R::freeze(true); return R::getRedBean(); }; $container['quiz'] = function ($c) { $database = $c->get('database'); return new QuizService(); };
/** * Connect to database with a connection * @param resource $dsn */ public function __construct($dbh, $freeze = true) { R::setup($dbh); R::freeze($freeze); connect::$dbh = $dbh; }
public function editRota(Request $request, Response $response, array $args) { $id = $this->authenticator->getIdentity(); if (strtolower($id['name']) != 'admin') { $this->flash->addMessage('flash', 'Access Denied'); return $response->withRedirect($this->router->pathFor('homepage')); } $name = $args['name']; if (empty($name)) { $this->flash->addMessage('flash', 'No rota specified'); return $response->withRedirect($this->router->pathFor('rotas')); } if ($name != 'new') { $rota = R::findOrCreate('rotas', ['name' => $name]); } else { $rota = R::dispense('rotas'); } if ($request->isPost()) { $data = $request->getParams(); //$username = $request->getParam('username'); $rota->import($data, 'name,fullname,title,comment'); $rota->sharedUsersList = []; foreach ($data['users'] as $checkUserID) { $rotaUser = R::load('users', $checkUserID); $rota->sharedUsersList[] = $rotaUser; } $id = R::store($rota); try { $fieldtest = R::inspect($rota->name); } catch (\Exception $e) { //thaw for creation R::freeze(['users']); $rotaUser = R::load('users', 1); $rotaDay = R::findOrCreate($rota->name, ['day' => 29, 'month' => 2, 'year' => 2015]); $rotaUser = R::load('users', 1); $rotaDay->name = $rotaUser; $rotaDay->who = $rotaUser; $rotaDay->stamp = date("Y-m-d H:i:s"); R::store($rotaDay); R::freeze(true); } $this->flash->addMessage('flash', "{$rota->name} updated"); return $response->withRedirect($this->router->pathFor('rotas')); } $userList = R::findAll('users'); $data = $rota->export(); $data['userList'] = $userList; $users = []; $userRota = $rota->sharedUsersList; foreach ($userRota as $userCheck) { $users[$userCheck->id] = 'checked'; } $data['userCheck'] = $users; $this->view->render($response, 'rota.twig', $data); return $response; }