PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/add-ons/ext_gadgets/gadgets/gadgets.class.php

https://github.com/iconifyit/SkyBlue-1.1
PHP | 265 lines | 198 code | 38 blank | 29 comment | 27 complexity | 0f1a714dda6b6d74fee1f5f18a8268f9 MD5 | raw file
  1. <?php
  2. /**
  3. * @version Beta 1.1 2008-07-29 11:50:00 $
  4. * @package SkyBlueCanvas
  5. * @copyright Copyright (C) 2005 - 2008 Scott Edwin Lewis. All rights reserved.
  6. * @license GNU/GPL, see COPYING.txt
  7. * SkyBlueCanvas is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYING.txt for copyright notices and details.
  12. */
  13. defined('SKYBLUE') or die(basename(__FILE__));
  14. class gadgets extends manager
  15. {
  16. function __construct()
  17. {
  18. $this->Init();
  19. }
  20. function gadgets()
  21. {
  22. $this->__construct();
  23. }
  24. function InitProps()
  25. {
  26. $this->SetProp('headings', array('Name', 'Enabled', 'Tasks'));
  27. $this->SetProp('tasks', array('publish', TASK_SEPARATOR, 'edit', 'delete'));
  28. $this->SetProp('cols', array('name', 'published'));
  29. }
  30. function Trigger()
  31. {
  32. global $Core;
  33. switch ($this->button)
  34. {
  35. case 'add':
  36. case 'edit':
  37. case 'addgadgets':
  38. case 'editgadgets':
  39. $this->AddButton('Save');
  40. $this->InitSkin();
  41. $this->InitEditor();
  42. $this->Edit();
  43. break;
  44. case 'delete':
  45. case 'deletegadgets':
  46. if (DEMO_MODE) $Core->ExitDemoEvent($this->redirect);
  47. $this->DeleteItem();
  48. break;
  49. case 'back':
  50. case 'cancel':
  51. $Core->SBRedirect($this->redirect);
  52. break;
  53. case 'save':
  54. case 'savegadgets':
  55. if (DEMO_MODE) $Core->ExitDemoEvent($this->redirect);
  56. $this->SaveItems();
  57. break;
  58. case 'publish':
  59. case 'publishgadgets':
  60. case 'unpublish':
  61. case 'unpublishgadgets':
  62. $this->publish();
  63. break;
  64. default:
  65. $this->AddButton('Add');
  66. $this->InitProps();
  67. $this->ViewItems();
  68. break;
  69. }
  70. }
  71. function publish()
  72. {
  73. global $Core;
  74. $name = $this->obj->name;
  75. if ($name{0} == '_')
  76. {
  77. $Core->ExitEvent($Core->MoveFile(
  78. SB_SITE_DATA_DIR . "gadgets/$name",
  79. SB_SITE_DATA_DIR . "gadgets/" . str_replace('_', null, $name)
  80. ), $this->redirect);
  81. }
  82. else
  83. {
  84. $Core->ExitEvent($Core->MoveFile(
  85. SB_SITE_DATA_DIR . "gadgets/$name",
  86. SB_SITE_DATA_DIR . "gadgets/_$name"
  87. ), $this->redirect);
  88. }
  89. }
  90. function InitEditor()
  91. {
  92. global $Core;
  93. // Set the form message
  94. $this->SetFormMessage('name', 'Gadget');
  95. // Initialize the object properties to empty strings or
  96. // the properties of the object being edited
  97. $_OBJ = $this->InitObjProps($this->skin, $this->obj);
  98. // This step creates a $form array to pass to buildForm().
  99. // buildForm() merges the $obj properites with the form HTML.
  100. $form['ID'] = $this->GetItemID($_OBJ);
  101. $form['NAME'] = $this->GetObjProp($_OBJ,'name');
  102. $form['CONTENT'] = $this->GetgadgetsContent($_OBJ,'content');
  103. $form['ZONE'] = $this->GetZone($this->GetObjProp($_OBJ,'name'));
  104. $this->BuildForm($form);
  105. }
  106. function GetZone($name)
  107. {
  108. if (empty($name)) return null;
  109. if ($name{0} == '_') $name = substr($name, 1, strlen($name));
  110. return "&lt;!--#gadget:" . substr($name, 0, strlen($name)-3) . "--&gt;";
  111. }
  112. //////////////////////////////////////////////////////////////////////////
  113. //
  114. // PARENT CLASS OVER-RIDES && NON-STANDARD FUNCTIONS
  115. //
  116. // The functions below this point implement OOP polymorphism to over-ride
  117. // the functionality of the Manager parent class. This class requires some
  118. // extended functionality that does not exist in the abstract parent class.
  119. //
  120. // In some cases more than one function is used to change the default
  121. // functionality but the over-ride must always begin with a name that is
  122. // identical to the function in the parent class.
  123. //
  124. //////////////////////////////////////////////////////////////////////////
  125. function SaveItems()
  126. {
  127. global $Core;
  128. $name = $Core->GetVar($_POST, 'name', NULL);
  129. if (isset($_POST['content']) && !empty($_POST['content']))
  130. {
  131. $text = $_POST['content'];
  132. }
  133. $text = stripslashes($text);
  134. if (!empty($name) && !empty($text))
  135. {
  136. $name = $this->AddFileExtension($name, 'js');
  137. $file = SB_SITE_DATA_DIR . "gadgets/" . $name;
  138. $Core->ExitEvent($Core->WriteFile($file, $text, 1), $this->redirect);
  139. }
  140. else
  141. {
  142. $Core->ExitEvent(0, $this->redirect);
  143. }
  144. }
  145. function AddFileExtension($name, $ext)
  146. {
  147. $bits = explode('.', $name);
  148. if ($bits[count($bits)-1] !== $ext)
  149. {
  150. $bits[] = $ext;
  151. }
  152. return implode('.', $bits);
  153. }
  154. function GetGadgetsContent()
  155. {
  156. global $Core;
  157. $path = SB_SITE_DATA_DIR . "gadgets/";
  158. if (!empty($this->obj->name) &&
  159. file_exists($path.$this->obj->name))
  160. {
  161. return stripslashes($Core->SBReadFile($path.$this->obj->name));
  162. } else {
  163. return NULL;
  164. }
  165. }
  166. function InitObjs()
  167. {
  168. global $Core;
  169. $path = SB_SITE_DATA_DIR . "gadgets/";
  170. if (is_dir($path))
  171. {
  172. $files = $Core->ListFilesOptionalRecurse($path, 0, array());
  173. for ($i=0; $i<count($files); $i++)
  174. {
  175. $name = basename($files[$i]);
  176. $obj = new stdClass;
  177. $obj->id = $i + 1;
  178. $obj->name = $name;
  179. $obj->published = $name{0} == '_' ? 'No' : 'Yes' ;
  180. $this->objs[] = $obj;
  181. }
  182. }
  183. }
  184. function LoadObj()
  185. {
  186. global $Core;
  187. for ($i=0; $i<count($this->objs); $i++)
  188. {
  189. if ($this->objs[$i]->id == $this->id)
  190. {
  191. $this->obj = $this->objs[$i];
  192. }
  193. }
  194. if (!isset($this->obj->id))
  195. {
  196. $this->obj = array();
  197. }
  198. }
  199. function InitObjType()
  200. {
  201. $this->objtype = 'gadgets';
  202. }
  203. function InitDataSource()
  204. {
  205. if (!is_dir(SB_SITE_DATA_DIR . "gadgets/"))
  206. {
  207. if (mkdir(SB_SITE_DATA_DIR . "gadgets/"))
  208. {
  209. @chmod(SB_SITE_DATA_DIR . "gadgets/", 0755);
  210. }
  211. }
  212. return;
  213. }
  214. function DeleteItem()
  215. {
  216. global $Core;
  217. $name = $this->obj->name;
  218. $file = SB_SITE_DATA_DIR . "gadgets/" . $name;
  219. if (file_exists($file) && !is_dir($file))
  220. {
  221. $Core->ExitEvent(intval(unlink($file)), $this->redirect);
  222. } else {
  223. $Core->ExitEvent(0, $this->redirect);
  224. }
  225. }
  226. }
  227. ?>