PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Intraface/modules/modulepackage/ModulePackage.php

https://github.com/intraface/intraface.dk
PHP | 281 lines | 169 code | 34 blank | 78 comment | 34 complexity | f14d87df58bf77f97d310ac3d06e0238 MD5 | raw file
  1. <?php
  2. /**
  3. * This class groups all modules in to different packages which can be added for each intranet
  4. * It makes it easier to control which modules each intranet has and for long time they have
  5. * paid for it.
  6. *
  7. * @package Intraface_modules_modulepackage
  8. * @author sune
  9. * @version 0.0.1
  10. */
  11. class Intraface_modules_modulepackage_ModulePackage extends Intraface_Standard
  12. {
  13. /**
  14. * @var object database
  15. */
  16. private $db;
  17. /**
  18. * @var object dbquery
  19. */
  20. public $dbquery;
  21. /**
  22. * @var id id on modulepackage
  23. */
  24. protected $id;
  25. /**
  26. * @var object error
  27. */
  28. public $error;
  29. /**
  30. * Init function
  31. *
  32. * @param object kernel The kernel object
  33. * @param int id on a ModulePackage
  34. *
  35. * @return void
  36. */
  37. public function __construct($id = 0)
  38. {
  39. $this->db = MDB2::singleton(DB_DSN);
  40. $this->id = (int)$id;
  41. $this->error = new Intraface_Error;
  42. if ($this->id != 0) {
  43. $this->load();
  44. }
  45. }
  46. /**
  47. * Loads information about the modulepackage
  48. *
  49. * @return integer id
  50. */
  51. public function load()
  52. {
  53. $result = $this->db->query("SELECT module_package.id, module_package.product_id, module_package_group.group_name AS ".$this->db->quoteIdentifier('group').", module_package_plan.plan, module_package_plan.plan_index, module_package_group.id AS group_id " .
  54. "FROM module_package " .
  55. "INNER JOIN module_package_group ON module_package.module_package_group_id = module_package_group.id " .
  56. "INNER JOIN module_package_plan ON module_package.module_package_plan_id = module_package_plan.id " .
  57. "WHERE module_package.id = ".$this->db->quote($this->id, 'integer'));
  58. if (PEAR::isError($result)) {
  59. throw new Exception("Error in db query in ModulePackage->load(): ".$result->getUserInfo());
  60. exit;
  61. }
  62. if (!$this->value = $result->fetchRow()) {
  63. $this->id = 0;
  64. $this->value['id'] = 0;
  65. return $this->id;
  66. }
  67. $this->value['modules'] = $this->getModules();
  68. return $this->id;
  69. }
  70. /**
  71. * Returns modules in a modulepackage
  72. *
  73. * @param integer id Optional you can provide an id otherwise it get it takes modules for the current modulepackage.
  74. *
  75. * @return array Modules
  76. */
  77. private function getModules($id = 0)
  78. {
  79. if ($id == 0) {
  80. $id = $this->id;
  81. } else {
  82. $id = intval($id);
  83. }
  84. $result = $this->db->query("SELECT id, module, limiter " .
  85. "FROM module_package_module " .
  86. "WHERE module_package_module.module_package_id = ".$this->db->quote($id, 'integer'));
  87. if (PEAR::isError($result)) {
  88. throw new Exception("Error in db query in ModulePackage->getmModules(): ".$result->getUserInfo());
  89. exit;
  90. }
  91. $modules = array();
  92. $i = 0;
  93. while ($row = $result->fetchRow()) {
  94. $modules[$i] = $row;
  95. $modules[$i]['limiters'] = $this->parseLimiters($row['limiter'], $row['module']);
  96. $i++;
  97. }
  98. return $modules;
  99. }
  100. /**
  101. * Parse the serialized array of limiters that is saved in the database.
  102. * The limiters are checked whether they are valid accoring to the Intraface/config/setting_limiters.php definition
  103. *
  104. * @param string string_limiter The serialized array string
  105. * @param string module The identifier of the module that the limiters belongs to
  106. *
  107. * @return array Array with limiters.
  108. */
  109. private function parseLimiters($string_limiters, $module)
  110. {
  111. if (trim($string_limiters) == '') {
  112. return array();
  113. }
  114. $limiters = unserialize($string_limiters);
  115. if ($limiters === false) {
  116. throw new Exception('Unable to unserialize string "'.$string_limiters.'" in ModulePackage->parseLimiters');
  117. return array();
  118. }
  119. $_limiter = array();
  120. // the array is filled in with values in the include file.
  121. require('Intraface/config/setting_limiters.php');
  122. // We make sure it is valid limiters
  123. $i = 0;
  124. $return_limiters = array();
  125. foreach ($limiters as $limiter => $limit) {
  126. if (isset($_limiter[$module][$limiter])) {
  127. $return_limiters[$i] = $_limiter[$module][$limiter];
  128. $return_limiters[$i]['limiter'] = $limiter;
  129. $return_limiters[$i]['limit'] = $limit;
  130. switch ($return_limiters[$i]['limit_type']) {
  131. case 'file_size':
  132. $return_limiters[$i]['limit_readable'] = filesize_readable($limit);
  133. break;
  134. }
  135. $i++;
  136. } else {
  137. throw new Exception('limiter '.$limiter.' in tabel module_package_module for module '.$module.' is not valid!');
  138. }
  139. }
  140. return $return_limiters;
  141. }
  142. /**
  143. * Creates the dbquery object.
  144. *
  145. * @param object kernel
  146. *
  147. * @return void
  148. */
  149. public function getDBQuery($kernel = null)
  150. {
  151. if ($this->dbquery) {
  152. return $this->dbquery;
  153. }
  154. if ($kernel == null) {
  155. throw new Exception('You need to provide kernel the first time you are calling getDBQuery');
  156. }
  157. $this->dbquery = new Intraface_DBQuery($kernel, 'module_package', 'module_package.active = 1');
  158. $this->dbquery->setJoin('INNER', 'module_package_group', 'module_package.module_package_group_id = module_package_group.id', 'module_package_group.active = 1');
  159. $this->dbquery->setJoin('INNER', 'module_package_plan', 'module_package.module_package_plan_id = module_package_plan.id', 'module_package_plan.active = 1');
  160. return $this->dbquery;
  161. }
  162. /**
  163. * Returns a list of possible packages
  164. *
  165. * @param string list_type Can be either list or matrix.
  166. *
  167. * @return array containing packages
  168. */
  169. public function getList($list_type = 'list')
  170. {
  171. if (!in_array($list_type, array('list', 'matrix'))) {
  172. throw new Exception('Invalid list type '.$list_type.' in ModulePackage->getList()');
  173. }
  174. $this->dbquery->setSorting('module_package_group.sorting_index, module_package_plan.plan_index');
  175. $list = array();
  176. $product_ids = array();
  177. $i = 0;
  178. $db = $this->dbquery->getRecordset('module_package.id, module_package_plan.plan, module_package_plan.id AS plan_id, module_package.product_id, module_package_group.group_name, module_package_group.id AS group_id');
  179. while ($db->nextRecord()) {
  180. $list[$i]['id'] = $db->f('id');
  181. $list[$i]['plan'] = $db->f('plan');
  182. $list[$i]['plan_id'] = $db->f('plan_id');
  183. $list[$i]['group'] = $db->f('group_name');
  184. $list[$i]['group_id'] = $db->f('group_id');
  185. $list[$i]['product_id'] = $db->f('product_id');
  186. if ($db->f('product_id') != 0) {
  187. $product_ids[] = $db->f('product_id');
  188. }
  189. $list[$i]['modules'] = $this->getModules($db->f('id'));
  190. $list[$i]['product'] = array();
  191. $i++;
  192. }
  193. // get all products in one request and add them to the array
  194. require_once 'Intraface/modules/modulepackage/ShopExtension.php';
  195. $shopextension = new Intraface_modules_modulepackage_ShopExtension;
  196. $products = $shopextension->getProduct((array)$product_ids);
  197. // we apply the products to the aray
  198. if (is_array($products) && count($products) > 0) {
  199. for ($i = 0, $max = count($list); $i < $max; $i++) {
  200. if ($list[$i]['product_id'] != 0) {
  201. foreach ($products['products'] as $product) {
  202. if ($product['id'] == $list[$i]['product_id']) {
  203. $list[$i]['product'] = $product;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. if ($list_type == 'list') {
  210. return $list;
  211. } elseif ($list_type == 'matrix') {
  212. $matrix = array();
  213. foreach ($list as $entry) {
  214. $matrix[$entry['group_id']][$entry['plan_id']] = $entry;
  215. }
  216. return $matrix;
  217. }
  218. }
  219. /**
  220. * Returns the available plans
  221. *
  222. * @return array Array with plans
  223. */
  224. function getPlans()
  225. {
  226. $result = $this->db->query("SELECT id, plan FROM module_package_plan WHERE active = 1 ORDER BY plan_index");
  227. if (PEAR::isError($result)) {
  228. throw new Exception('Error in db query: '.$result->getUserInfo());
  229. exit;
  230. }
  231. return $result->fetchAll();
  232. }
  233. /**
  234. * Returns the available groups
  235. *
  236. * @return array Array with groups
  237. */
  238. function getGroups()
  239. {
  240. $result = $this->db->query("SELECT id, group_name AS ".$this->db->quoteIdentifier('group')." FROM module_package_group WHERE active = 1 ORDER BY sorting_index");
  241. if (PEAR::isError($result)) {
  242. throw new Exception('Error in db query: '.$result->getUserInfo());
  243. exit;
  244. }
  245. return $result->fetchAll();
  246. }
  247. }