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

/development/example_to_fix_and_replace_zip/fortunes/mcp.fortunes.php

https://bitbucket.org/ellislab/expressionengine-user-guide/
PHP | 487 lines | 249 code | 119 blank | 119 comment | 27 complexity | b5cd34ebafd37ea4eb287a93e4182666 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2003 - 2004 EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/docs/license.html
  13. =====================================================
  14. File: mcp.fortunes.php
  15. -----------------------------------------------------
  16. Purpose: Fortunes module - CP
  17. =====================================================
  18. */
  19. class Fortunes_mcp {
  20. var $version = '1.0';
  21. var $view_limit = 25; // Number of words to show in View Fortunes
  22. // -------------------------
  23. // Constructor
  24. // -------------------------
  25. function Fortunes_mcp( $switch = TRUE )
  26. {
  27. global $IN;
  28. if ($switch)
  29. {
  30. switch($IN->GBL('P'))
  31. {
  32. case 'view' : $this->view_fortunes();
  33. break;
  34. case 'delete' : $this->delete_fortune();
  35. break;
  36. case 'add' : $this->modify_fortune();
  37. break;
  38. case 'modify' : $this->modify_fortune();
  39. break;
  40. case 'update' : $this->update_fortune();
  41. break;
  42. default : $this->fortunes_home();
  43. break;
  44. }
  45. }
  46. }
  47. // END
  48. // ----------------------------------------
  49. // Module Homepage
  50. // ----------------------------------------
  51. function fortunes_home()
  52. {
  53. global $DSP, $LANG;
  54. // -------------------------------------------------------
  55. // HTML Title and Navigation Crumblinks
  56. // -------------------------------------------------------
  57. $DSP->title = $LANG->line('fortunes_module_name');
  58. $DSP->crumb = $DSP->anchor(BASE.
  59. AMP.'C=modules'.
  60. AMP.'M=fortunes',
  61. $LANG->line('fortunes_module_name'));
  62. $DSP->crumb .= $DSP->crumb_item($LANG->line('fortunes_menu'));
  63. // -------------------------------------------------------
  64. // Page Heading
  65. // -------------------------------------------------------
  66. $DSP->body .= $DSP->heading($LANG->line('fortunes_menu'));
  67. // -------------------------------------------------------
  68. // Main Menu Links - Add Fortune or View Fortunes
  69. // -------------------------------------------------------
  70. $DSP->body .= $DSP->qdiv('itemWrapper', $DSP->heading($DSP->anchor(BASE.
  71. AMP.'C=modules'.
  72. AMP.'M=fortunes'.
  73. AMP.'P=add',
  74. $LANG->line('add_fortune')),
  75. 5));
  76. $DSP->body .= $DSP->qdiv('itemWrapper', $DSP->heading($DSP->anchor(BASE.
  77. AMP.'C=modules'.
  78. AMP.'M=fortunes'.
  79. AMP.'P=view',
  80. $LANG->line('view_fortunes')),
  81. 5));
  82. }
  83. // END
  84. // -------------------------
  85. // View Fortunes
  86. // -------------------------
  87. function view_fortunes($msg='')
  88. {
  89. global $DSP, $LANG, $DB, $FNS;
  90. // -------------------------------------------------------
  91. // Select All Fortunes from Database
  92. // -------------------------------------------------------
  93. $query = $DB->query("SELECT * FROM exp_fortunes");
  94. if ($query->num_rows == 0)
  95. {
  96. $DSP->body .= $DSP->error_message($LANG->line('no_fortunes'));
  97. return;
  98. }
  99. // -------------------------------------------------------
  100. // HTML Title and Navigation Crumblinks
  101. // -------------------------------------------------------
  102. $DSP->title = $LANG->line('fortunes_module_name');
  103. $DSP->crumb = $DSP->anchor(BASE.
  104. AMP.'C=modules'.
  105. AMP.'M=fortunes',
  106. $LANG->line('fortunes_module_name'));
  107. $DSP->crumb .= $DSP->crumb_item($LANG->line('view_fortunes'));
  108. // -------------------------------------------------------
  109. // Page Heading and Success Message, if any
  110. // -------------------------------------------------------
  111. $r = $DSP->heading($LANG->line('view_fortunes'));
  112. if ($msg != '')
  113. {
  114. $r .= $DSP->qdiv('success', $msg).BR;
  115. }
  116. // -------------------------------------------------------
  117. // Table and Table Headers
  118. // -------------------------------------------------------
  119. $r .= $DSP->table('tableBorder', '0', '0', '100%').
  120. $DSP->tr().
  121. $DSP->td('tablePad');
  122. $r .= $DSP->table('', '0', '', '100%').
  123. $DSP->tr().
  124. $DSP->table_qcell('tableHeadingBold',
  125. array(
  126. $LANG->line('fortune_text'),
  127. $LANG->line('modify_fortune'),
  128. $LANG->line('delete_fortune')
  129. )
  130. ).
  131. $DSP->tr_c();
  132. $i = 0;
  133. // -------------------------------------------------------
  134. // Display Rows of Fortunes
  135. // -------------------------------------------------------
  136. foreach($query->result as $row)
  137. {
  138. $style = ($i++ % 2) ? 'tableCellOne' : 'tableCellTwo';
  139. $r .= $DSP->tr();
  140. $r .= $DSP->table_qcell($style, $FNS->word_limiter($row['fortune_text'],$this->view_limit),'70%','top');
  141. $r .= $DSP->table_qcell($style, $DSP->anchor(BASE.
  142. AMP.'C=modules'.
  143. AMP.'M=fortunes'.
  144. AMP.'P=modify'.
  145. AMP.'fortune_id='.
  146. $row['fortune_id'],
  147. $LANG->line('modify_fortune')),'15%','top');
  148. $r .= $DSP->table_qcell($style, $DSP->anchor(BASE.
  149. AMP.'C=modules'.
  150. AMP.'M=fortunes'.
  151. AMP.'P=delete'.
  152. AMP.'fortune_id='.
  153. $row['fortune_id'],
  154. $LANG->line('delete_fortune')),'15%','top');
  155. $r .= $DSP->tr_c();
  156. }
  157. // -------------------------------------------------------
  158. // Close Table and Output to $DSP->body
  159. // -------------------------------------------------------
  160. $r .= $DSP->table_c();
  161. $r .= $DSP->td_c()
  162. .$DSP->tr_c()
  163. .$DSP->table_c();
  164. $DSP->body .= $r;
  165. }
  166. // END
  167. // -------------------------
  168. // Add/Modify Fortune Form
  169. // -------------------------
  170. function modify_fortune($msg='')
  171. {
  172. global $DSP, $LANG, $DB, $IN;
  173. // -------------------------------------------------------
  174. // If Modfying Existing Fortune, Retrieve Text
  175. // -------------------------------------------------------
  176. if ( ! $IN->GBL('fortune_id'))
  177. {
  178. $fortune_text = '';
  179. }
  180. else
  181. {
  182. $query = $DB->query("SELECT fortune_text
  183. FROM exp_fortunes
  184. WHERE fortune_id = '".$IN->GBL('fortune_id')."'");
  185. if ($query->num_rows == 0)
  186. {
  187. $DSP->body .= $DSP->error_message($LANG->line('invalid_fortune_id'));
  188. return;
  189. }
  190. $fortune_text = $query->row['fortune_text'];
  191. }
  192. // -------------------------------------------------------
  193. // HTML Title and Navigation Crumblinks
  194. // -------------------------------------------------------
  195. $DSP->title = $LANG->line('fortunes_module_name');
  196. $DSP->crumb = $DSP->anchor(BASE.
  197. AMP.'C=modules'.
  198. AMP.'M=fortunes',
  199. $LANG->line('fortunes_module_name'));
  200. $DSP->crumb .= $DSP->crumb_item(($fortune_text != '') ? $LANG->line('update_fortune') : $LANG->line('add_fortune'));
  201. // -------------------------------------------------------
  202. // Page Heading and Success Message, if any
  203. // -------------------------------------------------------
  204. $r = $DSP->heading(($fortune_text != '') ? $LANG->line('update_fortune') : $LANG->line('add_fortune'));
  205. if ($msg != '')
  206. {
  207. $r .= $DSP->qdiv('success', $msg).BR;
  208. }
  209. // -------------------------------------------------------
  210. // Declare Form
  211. // -------------------------------------------------------
  212. $r .= $DSP->form('C=modules'.AMP.'M=fortunes'.AMP.'P=update', 'target');
  213. // -------------------------------------------------------
  214. // Modifying Existing Fortune, Hidden Form Field for ID
  215. // -------------------------------------------------------
  216. if ($fortune_text != '')
  217. {
  218. $r .= $DSP->input_hidden('fortune_id', $IN->GBL('fortune_id'));
  219. }
  220. // -------------------------------------------------------
  221. // Table For Textarea
  222. // -------------------------------------------------------
  223. $r .= $DSP->table('tableBorder', '0', '0', '100%').
  224. $DSP->tr();
  225. $r .= $DSP->table_qcell('tableCellOne', $DSP->input_textarea('fortune_text', $fortune_text, 15, 'textarea', '100%'));
  226. $r .= $DSP->tr_c()
  227. .$DSP->table_c();
  228. // -------------------------------------------------------
  229. // Submit Button Text - Add/Update
  230. // -------------------------------------------------------
  231. if ($fortune_text != '')
  232. {
  233. $r .= $DSP->qdiv('itemWrapper', BR.$DSP->input_submit($LANG->line('update_fortune')));
  234. }
  235. else
  236. {
  237. $r .= $DSP->qdiv('itemWrapper', BR.$DSP->input_submit($LANG->line('add_fortune')));
  238. }
  239. // -------------------------------------------------------
  240. // Close Form
  241. // -------------------------------------------------------
  242. $r .= $DSP->form_c();
  243. $DSP->body .= $r;
  244. }
  245. // END
  246. // -------------------------
  247. // Update/Create Fortune
  248. // -------------------------
  249. function update_fortune()
  250. {
  251. global $LANG, $DB, $IN, $DSP;
  252. // -------------------------------------------------------
  253. // Valid Fortune Text Received?
  254. // -------------------------------------------------------
  255. $fortune_text = ( ! isset($_POST['fortune_text'])) ? '' : trim($_POST['fortune_text']);
  256. if ($fortune_text == '')
  257. {
  258. $DSP->body .= $DSP->error_message($LANG->line('invalid_fortune_text'));
  259. return;
  260. }
  261. // -------------------------------------------------------
  262. // Insert or Update Depending on Fortune ID
  263. // -------------------------------------------------------
  264. if ( ! isset($_POST['fortune_id']))
  265. {
  266. $data = array('fortune_id' => '',
  267. 'fortune_text' => trim($fortune_text));
  268. $DB->query($DB->insert_string('exp_fortunes', $data));
  269. // ---------------------------------------------------
  270. // Return to Form with Success Message
  271. // ---------------------------------------------------
  272. return $this->modify_fortune($LANG->line('fortune_added'));
  273. }
  274. else
  275. {
  276. $data = array('fortune_text' => trim($fortune_text));
  277. $DB->query($DB->update_string('exp_fortunes', $data, "fortune_id ='".$_POST['fortune_id']."'"));
  278. // ---------------------------------------------------
  279. // Return to View Fortunes with Success Message
  280. // ---------------------------------------------------
  281. return $this->view_fortunes($LANG->line('fortune_updated'));
  282. }
  283. }
  284. // END
  285. // -------------------------
  286. // Delete Fortune
  287. // -------------------------
  288. function delete_fortune()
  289. {
  290. global $LANG, $DB, $IN, $DSP;
  291. // -------------------------------------------------------
  292. // Fortune ID is required
  293. // -------------------------------------------------------
  294. if ( ! $IN->GBL('fortune_id'))
  295. {
  296. $DSP->body .= $DSP->error_message($LANG->line('invalid_fortune_id'));
  297. return;
  298. }
  299. else
  300. {
  301. $query = $DB->query("DELETE FROM exp_fortunes
  302. WHERE fortune_id = '".$IN->GBL('fortune_id')."'");
  303. if ($DB->affected_rows == 0)
  304. {
  305. $DSP->body .= $DSP->error_message($LANG->line('invalid_fortune_id'));
  306. return;
  307. }
  308. }
  309. // -------------------------------------------------------
  310. // Return to View Fortunes with Success Message
  311. // -------------------------------------------------------
  312. return $this->view_fortunes($LANG->line('fortune_deleted'));
  313. }
  314. // END
  315. // ----------------------------------------
  316. // Module installer
  317. // ----------------------------------------
  318. function fortunes_module_install()
  319. {
  320. global $DB;
  321. $sql[] = "INSERT INTO exp_modules (module_id,
  322. module_name,
  323. module_version,
  324. has_cp_backend)
  325. VALUES
  326. ('',
  327. 'Fortunes',
  328. '$this->version',
  329. 'y')";
  330. $sql[] = "CREATE TABLE IF NOT EXISTS `exp_fortunes` (
  331. `fortune_id` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
  332. `fortune_text` TEXT NOT NULL ,
  333. PRIMARY KEY (`fortune_id`));";
  334. foreach ($sql as $query)
  335. {
  336. $DB->query($query);
  337. }
  338. return true;
  339. }
  340. // END
  341. // ----------------------------------------
  342. // Module de-installer
  343. // ----------------------------------------
  344. function fortunes_module_deinstall()
  345. {
  346. global $DB;
  347. $query = $DB->query("SELECT module_id
  348. FROM exp_modules
  349. WHERE module_name = 'Fortunes'");
  350. $sql[] = "DELETE FROM exp_module_member_groups
  351. WHERE module_id = '".$query->row['module_id']."'";
  352. $sql[] = "DELETE FROM exp_modules
  353. WHERE module_name = 'Fortunes'";
  354. $sql[] = "DELETE FROM exp_actions
  355. WHERE class = 'Fortunes'";
  356. $sql[] = "DELETE FROM exp_actions
  357. WHERE class = 'Fortunes_mcp'";
  358. $sql[] = "DROP TABLE IF EXISTS exp_fortunes";
  359. foreach ($sql as $query)
  360. {
  361. $DB->query($query);
  362. }
  363. return true;
  364. }
  365. // END
  366. }
  367. // END CLASS
  368. ?>