Advertisement
Guest User

Untitled

a guest
May 27th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "vendor/autoload.php";
  4.  
  5. $app = new \Slim\Slim();
  6.  
  7. $app->get('/', function () use ($app) {
  8.  
  9. $view = new \view\Home();
  10. echo $view->render();
  11. });
  12.  
  13. $app->get('/mysql/', function () use ($app) {
  14. $start = microtime(true);
  15. $family_id = $app->request()->get('family');
  16. \config\Connexion::connection("config.ini");
  17. $view = new \view\Families();
  18. if ($family_id == null) {
  19. $data[0] = \model\Family::where('id_parent', '=', 0)->get();
  20. $view->addVar('current', $data[0][0]);
  21. } else {
  22. $res = \model\Family::where('id_parent', '=', $family_id)->get();
  23. if (sizeof($res) == 0) {
  24. $parent = \model\Family::find($family_id);
  25. $data[0] = \model\Family::with('articles')->where('id_parent', '=', $parent->id_parent)->get();
  26. foreach ($data[0] as $d) {
  27. if ($d->id == $parent->id) {
  28. $view->addVar('current', $d);
  29. $d->colored = true;
  30. break;
  31. }
  32. }
  33. } else {
  34. $data[0] = $res;
  35. $view->addVar('current', $res);
  36. }
  37. $parent = \model\Family::find($data[0][0]->id_parent);
  38. while ($parent != null) {
  39. $data[sizeof($data)] = \model\Family::where('id_parent', '=', $parent->id_parent)->get();
  40. foreach ($data[sizeof($data) - 1] as $d) {
  41. if ($d->id == $parent->id) {
  42. $d->colored = true;
  43. break;
  44. }
  45. }
  46. $parent = \model\Family::find($data[sizeof($data) - 1][0]->id_parent);
  47. }
  48. }
  49.  
  50. $end = microtime(true);
  51. $view->addVar('time', $end - $start);
  52. $view->addVar('categories', $data);
  53. echo $view->render();
  54. });
  55.  
  56. $app->get('/mongodb/', function () use ($app) {
  57.  
  58. $start = microtime(true);
  59.  
  60. try {
  61. $conn = new MongoClient('localhost');
  62. $db = $conn->catalogue;
  63. $families = $db->families;
  64. $family_id = $app->request()->get('family');
  65. $view = new \view\Families();
  66. $view->addVar('current', $family_id);
  67.  
  68. if ($family_id == null) {
  69. $data[0][0] = $families->findOne(array('parents' => []));
  70. $view->addVar('current', $data[0][0]);
  71. } else {
  72. $fam_cur = $families->findOne(array('id' => intval($family_id)));
  73. $fam_cur['colored'] = true;
  74. $view->addVar('current', $fam_cur);
  75. $children = $fam_cur['childs'];
  76. $level_child = $fam_cur['depth'];
  77. $data = array();
  78. foreach ($children as $child) {
  79. if ($child['depth'] == $fam_cur['depth'] + 1) {
  80. $res = $families->findOne(array('id' => $child['id']));
  81. $data[0][] = $res;
  82. }
  83. }
  84.  
  85. $parents = $fam_cur['parents'];
  86. if (sizeof($fam_cur['parents']) == 0) {
  87. $bn = $level_child + 1 - $fam_cur['depth'];
  88. $data[$bn][] = $fam_cur;
  89. }
  90. foreach ($parents as $parent) {
  91. $aaa = $families->findOne(array('id' => intval($parent['id'])));
  92. if (sizeof($aaa['parents']) == 0) {
  93. $bn = $level_child + 1 - $aaa['depth'];
  94. $data[$bn][] = $aaa;
  95. }
  96. foreach ($aaa['childs'] as $lol) {
  97. if ($lol['depth'] == $aaa['depth'] + 1) {
  98. $mdr = $families->findOne(array('id' => intval($lol['id'])));
  99. $bn = $level_child + 1 - $mdr['depth'];
  100. $data[$bn][] = $mdr;
  101. }
  102. }
  103.  
  104. foreach ($fam_cur['parents'] as $ccc) {
  105. $mdr = $families->findOne(array('id' => intval($ccc['id'])));
  106. foreach ($data as $key => $ddd) {
  107. foreach ($ddd as $k => $eee) {
  108. if ($eee['id'] == $mdr['id'] || $eee['id'] == $fam_cur['id']) {
  109. $data[$key][$k]['colored'] = true;
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. ksort($data);
  117. $view->addVar('categories', $data);
  118. $end = microtime(true);
  119. $view->addVar('time', $end - $start);
  120. echo $view->render();
  121. $conn->close();
  122. } catch (MongoConnectionException $e) {
  123. die('Error connecting to MongoDB server');
  124. } catch (MongoException $e) {
  125. die('Error: ' . $e->getMessage());
  126. }
  127. });
  128.  
  129. $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement