register(new Silex\Provider\UrlGeneratorServiceProvider()); $app -> register(new Silex\Provider\DoctrineServiceProvider(), array('db.options' => array('driver' => 'pdo_mysql', 'dbhost' => 'localhost', 'dbname' => 'rebh', 'user' => 'rebh', 'password' => 'SUVbSmQ4dj6y6pwx', 'charset' => 'utf8',), )); $app -> register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/views', )); $app -> register(new Silex\Provider\SessionServiceProvider()); use Symfony\Component\HttpFoundation\Response; // ... definitions $app -> get('/repeat/', function() use ($app) { $sql = "Select * from question where Qisanswer and Qdisplay and Qrepeat order by Qid "; $main = $app['db'] -> fetchAll($sql); $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); foreach ($main as &$question) { //var_dump($qcat["questions"]); if (!isset($question["Qsubject"]) || strlen($question["Qsubject"]) < 5) { $question["show"] = mb_substr($question["Qcontent"], 0, 75,"utf8"); } else { $question["show"] = mb_substr($question["Qsubject"], 0, 75,"utf8"); } } return $app['twig'] -> render('repeat.twig', array('rights' => $rights, 'main' => $main, )); }) -> bind("repeat"); $app -> get('/ftawaDetail/{id}', function($id) use ($app) { if(!is_numeric($id)) $app->abort(404, "خطأ في رقم الفتوى."); $sql = "Select * from question where Qid= ? and Qisanswer = 1"; $main = $app['db'] -> fetchAll($sql, array($id)); if(count($main) != 1) $app->abort(404, "السؤال غير موجود في قاعدة البيانات."); $sql = "UPDATE question SET Qvistor= ? where Qid= ? "; $app['db'] -> executeUpdate($sql, array($id, $main[0]["Qvistor"] + 1)); $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); foreach ($main as &$question) { //var_dump($qcat["questions"]); if (!isset($question["Qsubject"]) || strlen($question["Qsubject"]) < 5) { $question["show"] = mb_substr($question["Qcontent"], 0, 75,"utf8"); } else { $question["show"] = mb_substr($question["Qsubject"], 0, 75,"utf8"); } } return $app['twig'] -> render('ftawaDetail.twig', array('rights' => $rights, 'main' => $main, )); }) -> bind("ftawaDetail"); $app -> get('/researchDetail/{id}', function($id) use ($app) { if(!is_numeric($id)) $app->abort(404, "خطأ في رقم البحث."); $sql = "Select * from research where Rid=?"; $main = $app['db'] -> fetchAll($sql, array($id)); if(count($main) != 1) $app->abort(404, "البحث غير موجود في قاعدة البيانات."); $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); return $app['twig'] -> render('researchDetail.twig', array('rights' => $rights, 'main' => $main, )); }) -> bind("researchDetail"); ; $app -> get('/research', function() use ($app) { $sql = "Select * from research where Rdisplay order by Rid"; $main = $app['db'] -> fetchAll($sql, array()); $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); return $app['twig'] -> render('research.twig', array('rights' => $rights, 'main' => $main, )); }) -> bind("research"); ; $app -> get('/main/{id}', function($id) use ($app) { if(!is_numeric($id)) $app->abort(404, "خطأ في رقم الصفحة."); $sql = "Select * from main where Mid= ? and Mdisplay"; $main = $app['db'] -> fetchAll($sql, array($id)); if(count($main) != 1) $app->abort(404, "الصفحة غير موجود في قاعدة البيانات."); $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); return $app['twig'] -> render('main.twig', array('rights' => $rights, 'main' => $main, )); }) -> bind("main"); ; $app -> get('/', function() use ($app) { return $app->redirect($app['url_generator']->generate('default')); }); $app -> get('/default', function() use ($app) { $sql = "select * from selection where Sdisplay ORDER BY rand() LIMIT 1"; $selc = $app['db'] -> fetchAll($sql); $selc = $selc[0]; $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); $sql = "Select *, (SELECT count(*) FROM question q where q.QCid = qc.QCid) numOfQ from QuestionCat qc where QCdisplay = 1"; $qcats = $app['db'] -> fetchAll($sql); foreach ($qcats as &$qcat) { $sql = "Select * from question where QCid = ? and Qdisplay order by Qdate desc limit 2"; $qcat["questions"] = $app['db'] -> fetchAll($sql, array($qcat["QCid"])); foreach ($qcat["questions"] as &$question) { //var_dump($qcat["questions"]); if (!isset($question["Qsubject"]) || strlen($question["Qsubject"]) < 5) { $question["show"] = mb_substr($question["Qcontent"], 0, 75, "utf-8") . "..."; } else { $question["show"] = mb_substr($question["Qsubject"], 0, 75, "utf-8") . "..."; } $question["isnew"] = ((time() - $question["Qdate"]) <= 15) ? TRUE : FALSE; } // // } return $app['twig'] -> render('default.twig', array('rights' => $rights, 'selection' => $selc, 'qcats' => $qcats, )); }) -> bind("default"); $app -> get('/ftawaCat/', function() use ($app) { $sql = "Select *,(Select count(*) from question q where qc.QCid= q.QCid) count from QuestionCat qc order by QCid"; $main = $app['db'] -> fetchAll($sql); $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); return $app['twig'] -> render('ftawaCat.twig', array('rights' => $rights, 'main' => $main)); }) -> bind("ftawaCat"); $app -> get('/ftawa/{id}/{page}', function($id, $page) use ($app) { if(!is_numeric($id)) $app->abort(404, "خطأ في رقم الفتوى."); if(!is_numeric($page)) $app->abort(404, "خطأ في رقم الصفحة."); // set the number of items to display per page $items_per_page = 10; $page = intval($page); $id = intval($id); // build query $offset = ($page - 1) * $items_per_page; $sql = "Select * from question where qcid = ? and Qisanswer and Qdisplay order by Qid LIMIT " . $offset . "," . $items_per_page; ; $main = $app['db'] -> fetchAll($sql, array($id)); if(count($main) == 0) $app->abort(404, "تصنيف الفتاوى غير موجود في قاعدة البيانات."); $sql = "Select count(*) lim from question where qcid = ? and Qisanswer and Qdisplay"; $limit = $app['db'] -> fetchAll($sql, array($id)); $limit = $limit[0]; $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); foreach ($main as &$question) { //var_dump($qcat["questions"]); if ($question["Qsubject"] == "") { $question["show"] = $question["Qcontent"]; } else { $question["show"] = $question["Qsubject"]; } } return $app['twig'] -> render('ftawa.twig', array('rights' => $rights, 'main' => $main, 'id' => $id, "page" => $page, "limit" => intval($limit["lim"] / $items_per_page) + 1, 'c' => ($page - 1) * $items_per_page + 1)); }) -> value('page', 1) -> bind("ftawa"); $app -> get('/visitor/{page}', function($page) use ($app) { // set the number of items to display per page $items_per_page = 10; $page = intval($page); // build query $offset = ($page - 1) * $items_per_page; $sql = "Select * from visitor where Vdisplay order by Vid desc limit " . $offset . "," . $items_per_page; ; $main = $app['db'] -> fetchAll($sql); $sql = "Select count(*) lim from visitor where Vdisplay"; $limit = $app['db'] -> fetchAll($sql); $limit = $limit[0]; $sql = "Select * from main where Mdisplay=True order by Mid"; $rights = $app['db'] -> fetchAll($sql); return $app['twig'] -> render('visitor.twig', array('rights' => $rights, 'main' => $main, "page" => $page, "limit" => intval($limit["lim"] / $items_per_page) + 1, 'c' => ($page - 1) * $items_per_page + 1)); }) -> value('page', 1) -> bind("visitor"); $app['debug'] = true; $app -> error(function(\Exception $e, $code) use ($app) { switch ($code) { case 404 : $message = $app['twig'] -> render('error.twig',array("msg"=>$e->getMessage())); return new Response($message, $code); break; } if ($app['debug']) { return; } return new Response($e); }); $app -> run();