/app/modules/content/back/models/Content.php

https://bitbucket.org/astrikovd/plumproject · PHP · 92 lines · 53 code · 6 blank · 33 comment · 5 complexity · 39f7eea8d919c55a98f2c2d0ff6351e8 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2012 by Dmitry Astrikov / www.astrikov.ru
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * Content model (DAO)
  25. * @package modules/content/back/models
  26. */
  27. class Content
  28. {
  29. /**
  30. * Get Main menu for backend
  31. * @param integer $user_id
  32. * @return array
  33. */
  34. public static function getMainMenu($user_id, $request_uri)
  35. {
  36. //select all non empty cmp groups for backend
  37. $pref = Yii::app()->db->tablePrefix;
  38. $q = "SELECT *, char_id AS grp_char_id, NAME AS grp_name
  39. FROM {$pref}adm_cmp_grp acg
  40. WHERE (SELECT COUNT(*) FROM {$pref}adm_cmp ac WHERE ac.adm_cmp_grp_id = acg.adm_cmp_grp_id) > 0
  41. ORDER BY adm_cmp_grp_id";
  42. $command = Yii::app()->db->createCommand($q);
  43. $adm_cmp_grps = $command->queryAll();
  44. //select all user components for backend
  45. $q = "SELECT
  46. `ac`.`adm_cmp_id` AS `adm_cmp_id`,
  47. `ac`.`char_id` AS `cmp_char_id`,
  48. `ac`.`name` AS `cmp_name`,
  49. `acg`.`adm_cmp_grp_id` AS `adm_cmp_grp_id`,
  50. `acg`.`char_id` AS `grp_char_id`,
  51. `acg`.`name` AS `grp_name`,
  52. `auac`.`adm_user_id` AS `adm_user_id`
  53. FROM ((`{$pref}adm_cmp` `ac` JOIN `{$pref}adm_cmp_grp` `acg`) JOIN `{$pref}adm_user_adm_cmp` `auac`)
  54. WHERE (
  55. (`ac`.`adm_cmp_grp_id` = `acg`.`adm_cmp_grp_id`)
  56. AND
  57. (`ac`.`adm_cmp_id` = `auac`.`adm_cmp_id`)
  58. AND
  59. (`auac`.`adm_user_id` = :user_id)
  60. AND
  61. (`acg`.`adm_cmp_grp_id` != 7)
  62. )
  63. ORDER BY `ac`.`adm_cmp_id`";
  64. $command = Yii::app()->db->createCommand($q);
  65. $command->bindParam(":user_id", $user_id);
  66. $user_cmps = $command->queryAll();
  67. foreach ($adm_cmp_grps as $grp) {
  68. $active = false;
  69. $items = array();
  70. foreach ($user_cmps as $cmp) {
  71. if ($cmp['grp_char_id'] == $grp['grp_char_id']) {
  72. if (!$active)
  73. $active = (strpos($request_uri, $cmp['cmp_char_id']) !== false) ? true : false;
  74. $items[] = array('label' => $cmp['cmp_name'],
  75. 'url' => Yii::app()->params['back_url'] . "/{$cmp['cmp_char_id']}/");
  76. }
  77. }
  78. if (!empty($items)) {
  79. $result[] = array('label' => $grp['grp_name'],
  80. 'url' => '#', 'items' => $items, 'active' => $active);
  81. }
  82. }
  83. //return result array
  84. return $result;
  85. }
  86. }