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

/concreteOLD/models/attribute/type.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 261 lines | 225 code | 33 blank | 3 comment | 43 complexity | 5617d1e7eae4e629154ae69e0495e020 MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. class AttributeType extends Object {
  4. public function getAttributeTypeID() {return $this->atID;}
  5. public function getAttributeTypeHandle() {return $this->atHandle;}
  6. public function getAttributeTypeName() {return $this->atName;}
  7. public function getController() {return $this->controller;}
  8. public static function getByID($atID) {
  9. $db = Loader::db();
  10. $row = $db->GetRow('select atID, pkgID, atHandle, atName from AttributeTypes where atID = ?', array($atID));
  11. $at = new AttributeType();
  12. $at->setPropertiesFromArray($row);
  13. $at->loadController();
  14. return $at;
  15. }
  16. public function __destruct() {
  17. unset($this->controller);
  18. }
  19. public static function getList($akCategoryHandle = false) {
  20. $db = Loader::db();
  21. $list = array();
  22. if ($akCategoryHandle == false) {
  23. $r = $db->Execute('select atID from AttributeTypes order by atID asc');
  24. } else {
  25. $r = $db->Execute('select atID from AttributeTypeCategories inner join AttributeKeyCategories on AttributeTypeCategories.akCategoryID = AttributeKeyCategories.akCategoryID and AttributeKeyCategories.akCategoryHandle = ? order by atID asc', array($akCategoryHandle));
  26. }
  27. while ($row = $r->FetchRow()) {
  28. $list[] = AttributeType::getByID($row['atID']);
  29. }
  30. $r->Close();
  31. return $list;
  32. }
  33. public static function exportList($xml) {
  34. $attribs = AttributeType::getList();
  35. $db = Loader::db();
  36. $axml = $xml->addChild('attributetypes');
  37. foreach($attribs as $at) {
  38. $atype = $axml->addChild('attributetype');
  39. $atype->addAttribute('handle', $at->getAttributeTypeHandle());
  40. $atype->addAttribute('package', $at->getPackageHandle());
  41. $categories = $db->GetCol('select akCategoryHandle from AttributeKeyCategories inner join AttributeTypeCategories where AttributeKeyCategories.akCategoryID = AttributeTypeCategories.akCategoryID and AttributeTypeCategories.atID = ?', array($at->getAttributeTypeID()));
  42. if (count($categories) > 0) {
  43. $cat = $atype->addChild('categories');
  44. foreach($categories as $catHandle) {
  45. $cat->addChild('category')->addAttribute('handle', $catHandle);
  46. }
  47. }
  48. }
  49. }
  50. public function delete() {
  51. $db = Loader::db();
  52. if (method_exists($this->controller, 'deleteType')) {
  53. $this->controller->deleteType();
  54. }
  55. $db->Execute("delete from AttributeTypes where atID = ?", array($this->atID));
  56. $db->Execute("delete from AttributeTypeCategories where atID = ?", array($this->atID));
  57. }
  58. public static function getListByPackage($pkg) {
  59. $db = Loader::db();
  60. $list = array();
  61. $r = $db->Execute('select atID from AttributeTypes where pkgID = ? order by atID asc', array($pkg->getPackageID()));
  62. while ($row = $r->FetchRow()) {
  63. $list[] = AttributeType::getByID($row['atID']);
  64. }
  65. $r->Close();
  66. return $list;
  67. }
  68. public function getPackageID() { return $this->pkgID;}
  69. public function getPackageHandle() {
  70. return PackageList::getHandle($this->pkgID);
  71. }
  72. public function isAssociatedWithCategory($cat) {
  73. $db = Loader::db();
  74. $r = $db->GetOne("select count(akCategoryID) from AttributeTypeCategories where akCategoryID = ? and atID = ?", array($cat->getAttributeKeyCategoryID(), $this->getAttributeTypeID()));
  75. return $r > 0;
  76. }
  77. public static function getByHandle($atHandle) {
  78. // Handle legacy handles
  79. switch($atHandle) {
  80. case 'date':
  81. $atHandle = 'date_time';
  82. break;
  83. }
  84. $db = Loader::db();
  85. $row = $db->GetRow('select atID, pkgID, atHandle, atName from AttributeTypes where atHandle = ?', array($atHandle));
  86. $at = new AttributeType();
  87. $at->setPropertiesFromArray($row);
  88. $at->loadController();
  89. return $at;
  90. }
  91. public static function add($atHandle, $atName, $pkg = false) {
  92. $pkgID = 0;
  93. if (is_object($pkg)) {
  94. $pkgID = $pkg->getPackageID();
  95. }
  96. $db = Loader::db();
  97. $db->Execute('insert into AttributeTypes (atHandle, atName, pkgID) values (?, ?, ?)', array($atHandle, $atName, $pkgID));
  98. $id = $db->Insert_ID();
  99. $est = AttributeType::getByID($id);
  100. $path = $est->getAttributeTypeFilePath(FILENAME_ATTRIBUTE_DB);
  101. if ($path) {
  102. Package::installDB($path);
  103. }
  104. return $est;
  105. }
  106. public function getValue($avID) {
  107. $cnt = $this->getController();
  108. return $cnt->getValue($avID);
  109. }
  110. public function render($view, $ak = false, $value = false, $return = false) {
  111. // local scope
  112. Loader::library('attribute/view');
  113. $av = new AttributeTypeView($this, $ak, $value);
  114. $resp = $av->render($view, $return);
  115. if ($return) {
  116. return $resp;
  117. }
  118. }
  119. public function getAttributeTypeIconSRC() {
  120. $ff = '/' . FILENAME_BLOCK_ICON;
  121. if ($this->getPackageID() > 0) {
  122. $db = Loader::db();
  123. $h = $this->getPackageHandle();
  124. $url = (is_dir(DIR_PACKAGES . '/' . $h)) ? BASE_URL . DIR_REL : ASSETS_URL;
  125. $url = $url . '/' . DIRNAME_PACKAGES . '/' . $h . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $this->getAttributeTypeHandle() . $ff;
  126. } else if (file_exists(DIR_MODELS_CORE . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $this->getAttributeTypeHandle() . $ff)) {
  127. $url = ASSETS_URL . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $this->getAttributeTypeHandle() . $ff;
  128. } else if (file_exists(DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $this->getAttributeTypeHandle() . $ff)) {
  129. $url = BASE_URL . DIR_REL . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $this->getAttributeTypeHandle() . $ff;
  130. } else {
  131. $url = ASSETS_URL . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/default' . $ff;
  132. }
  133. return $url;
  134. }
  135. public function getAttributeTypeFilePath($_file) {
  136. $f = $this->mapAttributeTypeFilePath($_file);
  137. if (is_object($f)) {
  138. return $f->file;
  139. }
  140. }
  141. public function getAttributeTypeFileURL($_file) {
  142. $f = $this->mapAttributeTypeFilePath($_file);
  143. if (is_object($f)) {
  144. return $f->url;
  145. }
  146. }
  147. protected function mapAttributeTypeFilePath($_file) {
  148. $atHandle = $this->atHandle;
  149. if (file_exists(DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file)) {
  150. $file = DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file;
  151. $url = BASE_URL . DIR_REL . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file;
  152. } else if ($_file == FILENAME_ATTRIBUTE_CONTROLLER && file_exists(DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '.php')) {
  153. $file = DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '.php';
  154. }
  155. $pkgID = $this->pkgID;
  156. if (!isset($file) && $pkgID > 0) {
  157. $pkgHandle = PackageList::getHandle($pkgID);
  158. $dirp = is_dir(DIR_PACKAGES . '/' . $pkgHandle) ? DIR_PACKAGES . '/' . $pkgHandle : DIR_PACKAGES_CORE . '/' . $pkgHandle;
  159. if (file_exists($dirp . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file)) {
  160. $file = $dirp . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file;
  161. $url = BASE_URL . DIR_REL . '/' .DIRNAME_PACKAGES. '/' .$pkgHandle . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file;
  162. } else if ($_file == FILENAME_ATTRIBUTE_CONTROLLER && file_exists($dirp . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '.php')) {
  163. $file = $dirp . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '.php';
  164. }
  165. }
  166. if (!isset($file)) {
  167. if (file_exists(DIR_MODELS_CORE . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file)) {
  168. $file = DIR_MODELS_CORE . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file;
  169. $url = ASSETS_URL . '/' . DIRNAME_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '/' . $_file;
  170. } else if ($_file == FILENAME_ATTRIBUTE_CONTROLLER && file_exists(DIR_MODELS_CORE . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '.php')) {
  171. $file = DIR_MODELS_CORE . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle . '.php';
  172. }
  173. }
  174. if (isset($file)) {
  175. $obj = new stdClass;
  176. $obj->file = $file;
  177. $obj->url = $url;
  178. return $obj;
  179. } else {
  180. return false;
  181. }
  182. }
  183. protected function loadController() {
  184. // local scope
  185. $atHandle = $this->atHandle;
  186. $txt = Loader::helper('text');
  187. $className = $txt->camelcase($this->atHandle) . 'AttributeTypeController';
  188. $file = $this->mapAttributeTypeFilePath(FILENAME_ATTRIBUTE_CONTROLLER);
  189. if (!$file) {
  190. $cont = DIR_MODELS_CORE . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/default/' . FILENAME_ATTRIBUTE_CONTROLLER;
  191. $className = 'DefaultAttributeTypeController';
  192. } else {
  193. $cont = $file->file;
  194. }
  195. require_once($cont);
  196. $this->controller = new $className($this);
  197. }
  198. }
  199. class PendingAttributeType extends AttributeType {
  200. public static function getList() {
  201. $db = Loader::db();
  202. $atHandles = $db->GetCol("select atHandle from AttributeTypes");
  203. $dh = Loader::helper('file');
  204. $available = array();
  205. if (is_dir(DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES)) {
  206. $contents = $dh->getDirectoryContents(DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES);
  207. foreach($contents as $atHandle) {
  208. if (!in_array($atHandle, $atHandles)) {
  209. $available[] = PendingAttributeType::getByHandle($atHandle);
  210. }
  211. }
  212. }
  213. return $available;
  214. }
  215. public static function getByHandle($atHandle) {
  216. $th = Loader::helper('text');
  217. if (file_exists(DIR_MODELS . '/' . DIRNAME_ATTRIBUTES . '/' . DIRNAME_ATTRIBUTE_TYPES . '/' . $atHandle)) {
  218. $at = new PendingAttributeType();
  219. $at->atID = 0;
  220. $at->atHandle = $atHandle;
  221. $at->atName = $th->unhandle($atHandle);
  222. return $at;
  223. }
  224. }
  225. public function install() {
  226. $at = parent::add($this->atHandle, $this->atName);
  227. }
  228. }