/lib/sources/template.class.php

https://github.com/saePixel/jcore · PHP · 294 lines · 230 code · 49 blank · 15 comment · 18 complexity · 95d1daa24f480759a9107a87027fe436 MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. * template.class.php
  4. *
  5. * Jul 05, 07:00:00 2009
  6. * Copyright 2009 Istvan Petres (aka P.I.Julius)
  7. * me@pijulius.com
  8. * For licensing, see LICENSE or http://jcore.net/license
  9. ****************************************************************************/
  10. if (defined('WEBSITE_TEMPLATE') && WEBSITE_TEMPLATE)
  11. define('TEMPLATE_URL', url::site().'template/'.WEBSITE_TEMPLATE.'/');
  12. else
  13. define('TEMPLATE_URL', url::site().'template/');
  14. class _template {
  15. static $selected = null;
  16. static function populate() {
  17. if (!defined('WEBSITE_TEMPLATE') || !WEBSITE_TEMPLATE)
  18. return false;
  19. $row = sql::fetch(sql::run(
  20. " SELECT * FROM `{templates}`" .
  21. " WHERE `Name` = '".sql::escape(WEBSITE_TEMPLATE)."'"));
  22. if (!$row)
  23. return false;
  24. $handled = api::callHooks(API_HOOK_BEFORE,
  25. 'template::populate', $_ENV);
  26. if (isset($handled)) {
  27. api::callHooks(API_HOOK_AFTER,
  28. 'template::populate', $_ENV, $handled);
  29. return $handled;
  30. }
  31. template::$selected = $row;
  32. if (@is_file('template/'.WEBSITE_TEMPLATE.'/template.php'))
  33. include_once('template/'.WEBSITE_TEMPLATE.'/template.php');
  34. api::callHooks(API_HOOK_AFTER,
  35. 'template::populate', $_ENV, $row);
  36. return $row;
  37. }
  38. function install() {
  39. if (!isset($this->templateID) || !$this->templateID)
  40. return false;
  41. $handled = api::callHooks(API_HOOK_BEFORE,
  42. 'template::install', $this);
  43. if (isset($handled)) {
  44. api::callHooks(API_HOOK_AFTER,
  45. 'template::install', $this, $handled);
  46. return $handled;
  47. }
  48. files::$debug = true;
  49. sql::$debug = true;
  50. ob_start();
  51. $obcontent = null;
  52. $successfiles = $this->installFiles();
  53. $obcontent = ob_get_contents();
  54. ob_end_clean();
  55. $this->displayInstallResults(
  56. __("Writing files"),
  57. $obcontent,
  58. $successfiles);
  59. ob_start();
  60. $obcontent = null;
  61. $successsql = $this->installSQL();
  62. $successcustom = $this->installCustom();
  63. $obcontent = ob_get_contents();
  64. ob_end_clean();
  65. $this->displayInstallResults(
  66. __("Running SQL Queries"),
  67. $obcontent,
  68. $successsql);
  69. files::$debug = false;
  70. sql::$debug = false;
  71. if (!$successfiles || !$successsql || !$successcustom) {
  72. tooltip::display(
  73. __("Template couldn't be installed!")." " .
  74. __("Please see detailed error messages above and try again."),
  75. TOOLTIP_ERROR);
  76. api::callHooks(API_HOOK_AFTER,
  77. 'template::install', $this);
  78. return false;
  79. }
  80. if (JCORE_VERSION >= '0.9') {
  81. sql::run(
  82. " UPDATE `{templates}` SET " .
  83. " `Installed` = 1" .
  84. " WHERE `ID` = '".$this->templateID."'");
  85. if (sql::error()) {
  86. api::callHooks(API_HOOK_AFTER,
  87. 'template::install', $this);
  88. return false;
  89. }
  90. }
  91. api::callHooks(API_HOOK_AFTER,
  92. 'template::install', $this, $successfiles);
  93. return true;
  94. }
  95. function uninstall() {
  96. if (!isset($this->templateID) || !$this->templateID)
  97. return false;
  98. $handled = api::callHooks(API_HOOK_BEFORE,
  99. 'template::uninstall', $this);
  100. if (isset($handled)) {
  101. api::callHooks(API_HOOK_AFTER,
  102. 'template::uninstall', $this, $handled);
  103. return $handled;
  104. }
  105. files::$debug = true;
  106. sql::$debug = true;
  107. ob_start();
  108. $obcontent = null;
  109. $this->uninstallFiles();
  110. $obcontent = ob_get_contents();
  111. ob_end_clean();
  112. $this->displayInstallResults(
  113. __("Deleting files"),
  114. $obcontent,
  115. null);
  116. ob_start();
  117. $obcontent = null;
  118. $this->uninstallSQL();
  119. $this->uninstallCustom();
  120. $obcontent = ob_get_contents();
  121. ob_end_clean();
  122. $this->displayInstallResults(
  123. __("Running SQL Queries"),
  124. $obcontent,
  125. null);
  126. files::$debug = false;
  127. sql::$debug = false;
  128. $result = true;
  129. if (JCORE_VERSION >= '0.9') {
  130. sql::run(
  131. " UPDATE `{templates}` SET " .
  132. " `Installed` = 0" .
  133. " WHERE `ID` = '".$this->templateID."'");
  134. if (sql::error())
  135. $result = false;
  136. }
  137. api::callHooks(API_HOOK_AFTER,
  138. 'template::uninstall', $this, $result);
  139. return $result;
  140. }
  141. // Class should be overwritten from template's own class
  142. function installSQL() {
  143. echo "<p>".__("No SQL queries to run.")."</p>";
  144. return true;
  145. }
  146. // Class should be overwritten from template's own class
  147. function installFiles() {
  148. echo "<p>".__("No files to install.")."</p>";
  149. return true;
  150. }
  151. // Class should be overwritten from template's own class
  152. function installCustom() {
  153. return true;
  154. }
  155. function installjQueryPlugins($plugins = null) {
  156. if (!isset($this->templateID) || !$this->templateID)
  157. return false;
  158. sql::run(
  159. " UPDATE `{templates}`" .
  160. " SET `jQueryPlugins` = '".sql::escape($plugins)."'" .
  161. " WHERE `ID` = '".$this->templateID."'");
  162. return (sql::affected() != -1);
  163. }
  164. // Class should be overwritten from template's own class
  165. function uninstallSQL() {
  166. echo "<p>".__("No SQL queries to run.")."</p>";
  167. return true;
  168. }
  169. // Class should be overwritten from template's own class
  170. function uninstallFiles() {
  171. echo "<p>".__("No files to uninstall.")."</p>";
  172. return true;
  173. }
  174. // Class should be overwritten from template's own class
  175. function uninstallCustom() {
  176. return true;
  177. }
  178. // ************************************************ Admin Part
  179. function displayInstallResults($title, $results, $success = false) {
  180. $handled = api::callHooks(API_HOOK_BEFORE,
  181. 'template::displayInstallResults', $this, $title, $results, $success);
  182. if (isset($handled)) {
  183. api::callHooks(API_HOOK_AFTER,
  184. 'template::displayInstallResults', $this, $title, $results, $success, $handled);
  185. return $handled;
  186. }
  187. echo
  188. "<div tabindex='0' class='fc" .
  189. (isset($success) && !$success?
  190. " expanded":
  191. null) .
  192. "'>" .
  193. "<a class='fc-title'>" .
  194. (isset($success)?
  195. ($success?
  196. " <span class='align-right'>[".strtoupper(__("Success"))."]</span>":
  197. " <span class='align-right'>[".strtoupper(__("Error"))."]</span>"):
  198. null) .
  199. $title .
  200. "</a>" .
  201. "<div class='fc-content'>" .
  202. $results .
  203. "</div>" .
  204. "</div>";
  205. api::callHooks(API_HOOK_AFTER,
  206. 'template::displayInstallResults', $this, $title, $results, $success);
  207. }
  208. // ************************************************ Client Part
  209. static function getSelected() {
  210. return template::$selected;
  211. }
  212. static function getSelectedID () {
  213. if (template::$selected)
  214. return template::$selected['ID'];
  215. return 0;
  216. }
  217. }
  218. ?>