PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/src/Gathering/Item/Template/Template.php

http://github.com/concrete5/concrete5
PHP | 360 lines | 301 code | 56 blank | 3 comment | 31 complexity | 4c876cdf7e367c4112077a93abb17e65 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Core\Gathering\Item\Template;
  3. use Concrete\Core\Feature\Assignment\GatheringItemAssignment;
  4. use Concrete\Core\Feature\Feature;
  5. use Concrete\Core\Gathering\Item\Item;
  6. use Database;
  7. use Concrete\Core\Package\PackageList;
  8. use Core;
  9. use Concrete\Core\Foundation\ConcreteObject;
  10. abstract class Template extends ConcreteObject
  11. {
  12. abstract public function gatheringItemTemplateControlsSlotDimensions();
  13. protected $feTotalScore;
  14. protected $feHandles;
  15. public function getGatheringItemTemplateFeatureHandles()
  16. {
  17. if (!isset($this->feHandles)) {
  18. $db = Database::connection();
  19. $this->feHandles = $db->GetCol('select distinct feHandle from GatheringItemTemplateFeatures at inner join Features fe on at.feID = fe.feID where gatID = ?', array($this->gatID));
  20. }
  21. return $this->feHandles;
  22. }
  23. public static function getByID($gatID)
  24. {
  25. $db = Database::connection();
  26. $row = $db->GetRow('select GatheringItemTemplates.*, GatheringItemTemplateTypes.gatTypeHandle from GatheringItemTemplates inner join GatheringItemTemplateTypes on GatheringItemTemplateTypes.gatTypeID = GatheringItemTemplates.gatTypeID where GatheringItemTemplates.gatID = ?', array($gatID));
  27. if (isset($row['gatID'])) {
  28. $ns = Core::make('helper/text')->camelcase($row['gatTypeHandle']);
  29. $class = 'Template';
  30. if ($row['gatHasCustomClass']) {
  31. $class = Core::make('helper/text')->camelcase($row['gatHandle']) . $class;
  32. }
  33. $className = '\\Concrete\\Core\\Gathering\\Item\\Template\\' . $ns . '\\' . $class;
  34. $agt = Core::make($className);
  35. $agt->setPropertiesFromArray($row);
  36. return $agt;
  37. }
  38. }
  39. public static function getByHandle($gatHandle)
  40. {
  41. $db = Database::connection();
  42. $row = $db->GetRow('select gatID from GatheringItemTemplates where gatHandle = ?', array($gatHandle));
  43. if (isset($row['gatID'])) {
  44. return static::getByID($row['gatID']);
  45. }
  46. }
  47. public static function getListByPackage($pkg)
  48. {
  49. $db = Database::connection();
  50. $list = array();
  51. $r = $db->executeQuery('select gatID from GatheringItemTemplates where pkgID = ? order by gatID asc', array($pkg->getPackageID()));
  52. while ($row = $r->FetchRow()) {
  53. $agt = static::getByID($row['gatID']);
  54. if (is_object($agt)) {
  55. $agt[] = $agt;
  56. }
  57. }
  58. $r->Close();
  59. return $list;
  60. }
  61. public static function getListByType(\Concrete\Core\Gathering\Item\Template\Type $type)
  62. {
  63. $db = Database::connection();
  64. $list = array();
  65. $r = $db->executeQuery('select gatID from GatheringItemTemplates where gatTypeID = ? order by gatName asc', array($type->getGatheringItemTemplateTypeID()));
  66. while ($row = $r->FetchRow()) {
  67. $agt = static::getByID($row['gatID']);
  68. if (is_object($agt)) {
  69. $list[] = $agt;
  70. }
  71. }
  72. $r->Close();
  73. return $list;
  74. }
  75. public static function getList()
  76. {
  77. $db = Database::connection();
  78. $list = array();
  79. $r = $db->executeQuery('select gatID from GatheringItemTemplates order by gatName asc');
  80. while ($row = $r->FetchRow()) {
  81. $agt = static::getByID($row['gatID']);
  82. if (is_object($agt)) {
  83. $list[] = $agt;
  84. }
  85. }
  86. $r->Close();
  87. return $list;
  88. }
  89. public function getGatheringItemTemplateID()
  90. {
  91. return $this->gatID;
  92. }
  93. public function getGatheringItemTemplateHandle()
  94. {
  95. return $this->gatHandle;
  96. }
  97. public function getGatheringItemTemplateName()
  98. {
  99. return $this->gatName;
  100. }
  101. public function getPackageID()
  102. {
  103. return $this->pkgID;
  104. }
  105. public function gatheringItemTemplateHasCustomClass()
  106. {
  107. return $this->gatHasCustomClass;
  108. }
  109. public function gatheringItemTemplateIsAlwaysDefault()
  110. {
  111. return $this->gatForceDefault;
  112. }
  113. public function getPackageHandle()
  114. {
  115. return PackageList::getHandle($this->pkgID);
  116. }
  117. public function getGatheringItemTemplateFixedSlotWidth()
  118. {
  119. return $this->gatFixedSlotWidth;
  120. }
  121. public function getGatheringItemTemplateFixedSlotHeight()
  122. {
  123. return $this->gatFixedSlotHeight;
  124. }
  125. public function getGatheringItemTemplateTypeObject()
  126. {
  127. return Type::getByID($this->gatTypeID);
  128. }
  129. public function getGatheringItemTemplateTypeID()
  130. {
  131. return $this->gatTypeID;
  132. }
  133. public function getGatheringItemTemplateMinimumSlotHeight(GatheringItem $item)
  134. {
  135. return 1;
  136. }
  137. public function getGatheringItemTemplateMaximumSlotHeight(GatheringItem $item)
  138. {
  139. return 2;
  140. }
  141. public function getGatheringItemTemplateMinimumSlotWidth(GatheringItem $item)
  142. {
  143. return 1;
  144. }
  145. public function getGatheringItemTemplateMaximumSlotWidth(GatheringItem $item)
  146. {
  147. return 3;
  148. }
  149. public function getGatheringItemTemplateIconSRC()
  150. {
  151. $env = Environment::get();
  152. $type = $this->getGatheringItemTemplateTypeObject();
  153. $path = $env->getURL(DIRNAME_ELEMENTS . '/' . DIRNAME_GATHERING . '/' . DIRNAME_GATHERING_ITEM_TEMPLATES . '/' . $type->getGatheringItemTemplateTypeHandle() . '/' . $this->getGatheringItemTemplateHandle() . '/' . FILENAME_GATHERING_ITEM_TEMPLATE_ICON);
  154. return $path;
  155. }
  156. /**
  157. * This method is called by GatheringItem when setting defaults.
  158. */
  159. public function getGatheringItemTemplateSlotWidth(GatheringItem $item)
  160. {
  161. if ($this->getGatheringItemTemplateFixedSlotWidth()) {
  162. return $this->getGatheringItemTemplateFixedSlotWidth();
  163. }
  164. $w = 0;
  165. $handles = $this->getGatheringItemTemplateFeatureHandles();
  166. $assignments = \Concrete\Core\Feature\Assignment\GatheringItemAssignment::getList($item);
  167. foreach ($assignments as $as) {
  168. if (in_array($as->getFeatureDetailHandle(), $handles)) {
  169. $fd = $as->getFeatureDetailObject();
  170. if ($fd->getGatheringItemSuggestedSlotWidth() > 0 && $fd->getGatheringItemSuggestedSlotWidth() > $w) {
  171. $w = $fd->getGatheringItemSuggestedSlotWidth();
  172. }
  173. }
  174. }
  175. if ($w) {
  176. return $w;
  177. }
  178. $wb = $this->getGatheringItemTemplateMinimumSlotWidth($item);
  179. $wt = $this->getGatheringItemTemplateMaximumSlotWidth($item);
  180. return mt_rand($wb, $wt);
  181. }
  182. public function getGatheringItemTemplateSlotHeight(Item $item)
  183. {
  184. if ($this->getGatheringItemTemplateFixedSlotHeight()) {
  185. return $this->getGatheringItemTemplateFixedSlotHeight();
  186. }
  187. $h = 0;
  188. $handles = $this->getGatheringItemTemplateFeatureHandles();
  189. $assignments = GatheringItemAssignment::getList($item);
  190. foreach ($assignments as $as) {
  191. if (in_array($as->getFeatureDetailHandle(), $handles)) {
  192. $fd = $as->getFeatureDetailObject();
  193. if ($fd->getGatheringItemSuggestedSlotHeight() > 0 && $fd->getGatheringItemSuggestedSlotHeight() > $h) {
  194. $h = $fd->getGatheringItemSuggestedSlotHeight();
  195. }
  196. }
  197. }
  198. if ($h) {
  199. return $h;
  200. }
  201. $hb = $this->getGatheringItemTemplateMinimumSlotHeight($item);
  202. $ht = $this->getGatheringItemTemplateMaximumSlotHeight($item);
  203. return mt_rand($hb, $ht);
  204. }
  205. public function addGatheringItemTemplateFeature($fe)
  206. {
  207. $db = Database::connection();
  208. $no = $db->fetchColumn("select count(gfeID) from GatheringItemTemplateFeatures where gatID = ? and feID = ?", array($this->gatID, $fe->getFeatureID()));
  209. if ($no < 1) {
  210. $db->executeQuery('insert into GatheringItemTemplateFeatures (gatID, feID) values (?, ?)', array($this->getGatheringItemTemplateID(), $fe->getFeatureID()));
  211. }
  212. }
  213. public function getGatheringTemplateFeaturesTotalScore()
  214. {
  215. if (!isset($this->feTotalScore)) {
  216. $db = Database::connection();
  217. $this->feTotalScore = $db->fetchColumn('select sum(feScore) from Features fe inner join GatheringItemTemplateFeatures af on af.feID = fe.feID where af.gatID = ?', array($this->getGatheringItemTemplateID()));
  218. }
  219. return $this->feTotalScore;
  220. }
  221. public function getGatheringItemTemplateFeatureObjects()
  222. {
  223. $db = Database::connection();
  224. $r = $db->executeQuery('select feID from GatheringItemTemplateFeatures where gatID = ?', array($this->getGatheringItemTemplateID()));
  225. $features = array();
  226. while ($row = $r->FetchRow()) {
  227. $fe = Feature::getByID($row['feID']);
  228. if (is_object($fe)) {
  229. $features[] = $fe;
  230. }
  231. }
  232. return $features;
  233. }
  234. public static function add(\Concrete\Core\Gathering\Item\Template\Type $type, $gatHandle, $gatName, $gatFixedSlotWidth, $gatFixedSlotHeight, $gatHasCustomClass = false, $gatForceDefault = false, $pkg = false)
  235. {
  236. $db = Database::connection();
  237. $pkgID = 0;
  238. if (is_object($pkg)) {
  239. $pkgID = $pkg->getPackageID();
  240. }
  241. $db->executeQuery('insert into GatheringItemTemplates (gatTypeID, gatHandle, gatName, gatFixedSlotWidth, gatFixedSlotHeight, gatHasCustomClass, gatForceDefault, pkgID) values (?, ?, ?, ?, ?, ?, ?, ?)', array($type->getGatheringItemTemplateTypeID(), $gatHandle, $gatName, $gatFixedSlotWidth, $gatFixedSlotHeight, intval($gatHasCustomClass), intval($gatForceDefault), $pkgID));
  242. $id = $db->lastInsertId();
  243. $agt = static::getByID($id);
  244. return $agt;
  245. }
  246. public function export($axml)
  247. {
  248. $agt = $axml->addChild('gatheringitemtemplate');
  249. $type = $this->getGatheringItemTemplateTypeObject();
  250. $agt->addAttribute('handle', $this->getGatheringItemTemplateHandle());
  251. $agt->addAttribute('name', Core::make('helper/text')->entities($this->getGatheringItemTemplateName()));
  252. $agt->addAttribute('type', $type->getGatheringItemTemplateTypeHandle());
  253. if ($this->gatheringItemTemplateHasCustomClass()) {
  254. $agt->addAttribute('has-custom-class', true);
  255. } else {
  256. $agt->addAttribute('has-custom-class', false);
  257. }
  258. $agt->addAttribute('package', $this->getPackageHandle());
  259. $features = $this->getGatheringItemTemplateFeatureObjects();
  260. foreach ($features as $fe) {
  261. $fe->export($agt, false);
  262. }
  263. return $agt;
  264. }
  265. public static function exportList($xml)
  266. {
  267. $axml = $xml->addChild('gatheringitemtemplates');
  268. $db = Database::connection();
  269. $r = $db->executeQuery('select gatID from GatheringItemTemplates order by gatID asc');
  270. $list = array();
  271. while ($row = $r->FetchRow()) {
  272. $agt = static::getByID($row['gatID']);
  273. if (is_object($agt)) {
  274. $list[] = $agt;
  275. }
  276. }
  277. foreach ($list as $agt) {
  278. $agt->export($axml);
  279. }
  280. }
  281. public function delete()
  282. {
  283. $db = Database::connection();
  284. $db->executeQuery('delete from GatheringItemTemplates where gatID = ?', array($this->gatID));
  285. }
  286. public function getGatheringItemTemplateData(Item $item)
  287. {
  288. $assignments = GatheringItemAssignment::getList($item);
  289. $data = array();
  290. foreach ($assignments as $as) {
  291. $fd = $as->getFeatureDetailObject();
  292. $key = $as->getFeatureDetailHandle();
  293. if (is_array($data[$key])) {
  294. $data[$key][] = $fd;
  295. } elseif (array_key_exists($key, $data)) {
  296. $tmp = $data[$key];
  297. $data[$key] = array($tmp, $fd);
  298. } else {
  299. $data[$key] = $fd;
  300. }
  301. }
  302. return $data;
  303. }
  304. }