model.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * The model file of space module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL (http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Jianhua Wang <wangjianhua@easycorp.ltd>
  8. * @package space
  9. * @link https://www.zentao.net
  10. */
  11. class spaceModel extends model
  12. {
  13. /**
  14. * 获取用户的空间列表。
  15. * Get space list by user account.
  16. *
  17. * @param string $account
  18. * @access public
  19. * @return array
  20. */
  21. public function getSpacesByAccount($account)
  22. {
  23. return $this->dao->select('*')->from(TABLE_SPACE)
  24. ->where('deleted')->eq(0)
  25. ->andWhere('owner')->eq($account)
  26. ->fetchAll();
  27. }
  28. /**
  29. * 获取用户的默认空间。
  30. * Get user's default space by user account.
  31. *
  32. * @param string $account
  33. * @access public
  34. * @return object
  35. */
  36. public function defaultSpace($account)
  37. {
  38. $default = $this->dao->select('*')->from(TABLE_SPACE)
  39. ->where('deleted')->eq(0)
  40. ->andWhere('owner')->eq($account)
  41. ->orderBy('default desc')
  42. ->fetch();
  43. if(empty($default)) return $this->createDefaultSpace($account);
  44. return $default;
  45. }
  46. /**
  47. * 获取用户的系统空间。
  48. * Get system space.
  49. *
  50. * @param string $account
  51. * @access public
  52. * @return object
  53. */
  54. public function getSystemSpace($account)
  55. {
  56. if(empty($account)) $account = $this->dao->select('*')->from(TABLE_USER)->where('deleted')->eq(0)->fetch('account');
  57. $sysSpace = $this->dao->select('*')->from(TABLE_SPACE)
  58. ->where('k8space')->eq($this->config->k8space)
  59. ->andWhere('owner')->eq($account)
  60. ->andWhere('deleted')->eq(0)
  61. ->fetch();
  62. if($sysSpace) return $sysSpace;
  63. $spaceData = new stdClass;
  64. $spaceData->name = $this->lang->space->systemSpace;
  65. $spaceData->owner = $account;
  66. $spaceData->k8space = $this->config->k8space;
  67. $spaceData->default = 0;
  68. $spaceData->createdAt = date('Y-m-d H:i:s');
  69. $this->dao->insert(TABLE_SPACE)->data($spaceData)->autoCheck()->exec();
  70. return $this->fetchByID($this->dao->lastInsertId());
  71. }
  72. /**
  73. * 创建默认空间。
  74. * Create default space by account.
  75. *
  76. * @param string $account
  77. * @access public
  78. * @return object
  79. */
  80. public function createDefaultSpace($account)
  81. {
  82. if(empty($account)) $account = $this->dao->select('*')->from(TABLE_USER)->where('deleted')->eq(0)->fetch('account');
  83. $default = new stdclass;
  84. $default->name = $this->lang->space->defaultSpace;
  85. $default->k8space = $this->config->k8sAppSpace;
  86. $default->owner = $account;
  87. $default->default = true;
  88. $default->createdAt = date('Y-m-d H:i:s');
  89. $this->dao->insert(TABLE_SPACE)->data($default)->autoCheck()->exec();
  90. return $this->fetchByID($this->dao->lastInsertId());
  91. }
  92. /**
  93. * 获取用户空间的应用列表。
  94. * Get app list in space by space id.
  95. *
  96. * @param int $spaceID
  97. * @param string $status all|running|stopped|abnormal
  98. * @param string $searchName
  99. * @param object $pager
  100. * @access public
  101. * @return array
  102. */
  103. public function getSpaceInstances($spaceID, $status = 'all', $searchName = '', $pager = null)
  104. {
  105. $instances = $this->dao->select('*')->from(TABLE_INSTANCE)
  106. ->where('deleted')->eq(0)
  107. ->beginIF($spaceID)->andWhere('space')->eq($spaceID)->fi()
  108. ->beginIF($status !== 'all')->andWhere('status')->eq($status)->fi()
  109. ->beginIF(!empty($searchName))->andWhere('name')->like("%{$searchName}%")->fi()
  110. ->orderBy('id desc')
  111. ->page($pager)
  112. ->fetchAll('id', false);
  113. if(empty($instances)) return array();
  114. if($this->config->inQuickon) $instances = $this->loadModel('store')->batchSetLatestVersions($instances);
  115. return $instances;
  116. }
  117. /**
  118. * 根据ID获取空间。
  119. * Get space by id.
  120. *
  121. * @param int $spaceID
  122. * @access public
  123. * @return object|false
  124. */
  125. public function getByID($spaceID)
  126. {
  127. return $this->dao->select('*')->from(TABLE_SPACE)
  128. ->where('deleted')->eq(0)
  129. ->andWhere('id')->eq($spaceID)
  130. ->andWhere('owner')->eq($this->app->user->account)
  131. ->fetch();
  132. }
  133. /**
  134. * 获取应用市场应用对应的外部应用。
  135. * Get External app By store app.
  136. *
  137. * @param object $instance
  138. * @access public
  139. * @return object|false
  140. */
  141. public function getExternalAppByApp($instance)
  142. {
  143. $server = $this->dao->select('*')->from(TABLE_PIPELINE)
  144. ->where('deleted')->eq('0')
  145. ->andWhere('instanceID')->eq($instance->id)
  146. ->fetch();
  147. if($server) return $server;
  148. $server = $this->dao->select('*')->from(TABLE_PIPELINE)
  149. ->where('deleted')->eq('0')
  150. ->andWhere('createdBy')->eq('system')
  151. ->andWhere('url')->like("%{$instance->domain}%")
  152. ->fetch();
  153. if(!$server) return false;
  154. $this->dao->update(TABLE_PIPELINE)->set('instanceID')->eq($instance->id)->where('id')->eq($server->id)->exec();
  155. return $server;
  156. }
  157. /**
  158. * 获取用户空间的应用列表AppID。
  159. * Get app list AppID in space by space id.
  160. *
  161. * @param int $spaceID
  162. * @access public
  163. * @return array
  164. */
  165. public function getSpaceInstancesAppIDs($spaceID)
  166. {
  167. return $this->dao->select('id, appID')->from(TABLE_INSTANCE)
  168. ->where('deleted')->eq(0)
  169. ->beginIF($spaceID)->andWhere('space')->eq($spaceID)->fi()
  170. ->fetchAll('id', false);
  171. }
  172. }