PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/cms-admin/classes/Pages.php

https://github.com/allanfreitas/CandyCMS
PHP | 213 lines | 122 code | 78 blank | 13 comment | 10 complexity | b45b2de7a432438b11a33262513eb0dd MD5 | raw file
  1. <?php
  2. /**
  3. * @package CandyCMS
  4. * @version 1.0
  5. * @since 0.1
  6. * @copyright Copyright 2012 (C) Cocoon Design Ltd. - All Rights Reserved
  7. *
  8. * Methods for editing pages in the admin panel
  9. */
  10. class Pages {
  11. public static function listPages(){
  12. return CandyDB::results('SELECT * FROM '. DB_PREFIX .'pages');
  13. }
  14. public static function inNav(){
  15. return CandyDB::results('SELECT * FROM '. DB_PREFIX .'pages WHERE innav = 1 ORDER BY navpos');
  16. }
  17. public static function sortPages(){
  18. $pages = Candy::Options('nav');
  19. $order = json_decode($pages); // Holy bajeesus, what in the living hell is this?
  20. $html = '<ol>';
  21. foreach ($order as $page) {
  22. $title = CandyDB::val('SELECT page_title FROM '. DB_PREFIX .'pages WHERE page_id = :page_id', array('page_id' => $page->id));
  23. $html .= "<li class='dd-item' data-id='{$page->id}'><div class='dd-handle'>{$title}<button class='icon-remove rm-nav right' value='{$page->id}'></button></div>";
  24. if (isset($page->children)) {
  25. $html .= '<ol>';
  26. foreach ($page->children as $child) {
  27. $title = CandyDB::val('SELECT page_title FROM '. DB_PREFIX .'pages WHERE page_id = :page_id', array('page_id' => $child->id));
  28. $html .= "<li class='dd-item' data-id='{$child->id}'> <div class='dd-handle'>{$title}<button class='icon-remove rm-nav right' value='{$child->id}'></button></div>";
  29. if (isset($child->children)) {
  30. $html .= '<ol>';
  31. foreach ($child->children as $grandchild) {
  32. $title = CandyDB::val('SELECT page_title FROM '. DB_PREFIX .'pages WHERE page_id = :page_id', array('page_id' => $grandchild->id));
  33. $html .= "<li class='dd-item' data-id='{$grandchild->id}'> <div class='dd-handle'>{$title}<button class='icon-remove rm-nav right' value='{$grandchild->id}'></button></div></li>";
  34. }
  35. $html .= '</ol>';
  36. }
  37. $html .= "</li>";
  38. }
  39. $html .= '</ol>';
  40. }
  41. $html .= "</li>";
  42. }
  43. $html .= '</ol>';
  44. echo $html;
  45. }
  46. public static function saveNav($nav){
  47. $decode = json_decode($nav);
  48. // Candy::Options()->set('nav', $nav);
  49. CandyDB::q('UPDATE '.DB_PREFIX.'options SET option_value = :value WHERE option_key = :key', array('value' => $nav, 'key' => 'nav'));
  50. }
  51. public static function dropdownPages($name = "pages", $selected = false){
  52. $pages = self::listPages();
  53. $html = "<select name='$name'>";
  54. foreach ($pages as $page) {
  55. $html .= ($selected == $page->rewrite) ? "<option value='{$page->rewrite}' selected='selected'>" : "<option value='{$page->rewrite}'>";
  56. $html .= $page->page_title;
  57. $html .= '</option>';
  58. }
  59. $html .= "</select>";
  60. echo $html;
  61. }
  62. public static function pagesTable(){
  63. $pages = self::listPages();
  64. $html = '<table>';
  65. $html .= '<thead><tr><th>Page Title</th><th></th><th></th></tr></thead>';
  66. foreach ($pages as $page) {
  67. $html .= '<tr>';
  68. $html .= '<td>'.$page->page_title.'</td>';
  69. $html .= '<td><a href="dashboard.php?page=pages&edit='.$page->page_id.'" title="Edit Page">Edit</a></td>';
  70. $html .= '<td><a class="delete" href="dashboard.php?page=pages&delete='.$page->page_id.'" title="'.$page->page_title.'">[x]</a></td>';
  71. $html .= '</tr>';
  72. }
  73. $html .= '</table>';
  74. echo $html;
  75. }
  76. public static function pageInfo($page){
  77. return CandyDB::results('SELECT * FROM '. DB_PREFIX .'pages WHERE page_id = :page', compact('page'));
  78. }
  79. public static function updatePage($title, $body, $rewrite, $template, $innav, $id, $cfields = false){
  80. $innav = ($innav == 'on') ? 1 : 0;
  81. $dbh = new CandyDB();
  82. $sth = $dbh->prepare('UPDATE '. DB_PREFIX .'pages SET page_title="'. $title .'", page_body="'. addslashes($body) .'", page_template="'. $template .'", rewrite="'. $rewrite .'" WHERE page_id="' . $id . '"');
  83. $sth->execute();
  84. if (isset($_POST['cf-update'])) {
  85. foreach ($_POST['cf-update'] as $key => $value) {
  86. $value = addslashes($value);
  87. $sth = $dbh->prepare("UPDATE ".DB_PREFIX."fields SET field_value='$value' WHERE field_name='$key' AND post_id='$id'");
  88. $sth->execute();
  89. }
  90. }
  91. // Insert the custom fields
  92. if ($cfields != false) {
  93. foreach ($cfields as $key => $value) {
  94. $data = addslashes($_POST['cf-update'][$key]);
  95. $title = addslashes($_POST['cf-title'][$key]);
  96. $desc = addslashes($_POST['cf-desc'][$key]);
  97. $sth = $dbh->prepare("INSERT INTO ".DB_PREFIX."fields (post_id, field_name, field_type, field_value, field_title, field_desc) VALUES ($id, '$key', '$value', '$data', '$title', '$desc')");
  98. $sth->execute();
  99. }
  100. }
  101. }
  102. public static function addPage($title, $body, $template, $rewrite, $innav, $cfields = false){
  103. $innav = ($innav == 'on') ? 1 : 0;
  104. // Insert the post
  105. $dbh = new CandyDB();
  106. $sth = $dbh->prepare('INSERT INTO '. DB_PREFIX .'pages (page_title, page_body, page_template, rewrite) VALUES ("'. $title .'", "'. addslashes($body) .'", "'. $template .'", "'. $rewrite .'")');
  107. $sth->execute();
  108. // Get the last inserted ID
  109. $sth = $dbh->prepare("SELECT page_id FROM ".DB_PREFIX."pages WHERE page_title='$title' AND rewrite='$rewrite' AND page_template='$template'");
  110. $sth->execute();
  111. $id = $sth->fetchColumn();
  112. // Insert the custom fields
  113. if ($cfields != false) {
  114. foreach ($cfields as $key => $value) {
  115. $data = addslashes($_POST['cf-update'][$key]);
  116. $title = addslashes($_POST['cf-title'][$key]);
  117. $desc = addslashes($_POST['cf-desc'][$key]);
  118. $sth = $dbh->prepare("INSERT INTO ".DB_PREFIX."fields (post_id, field_name, field_type, field_value, field_title, field_desc) VALUES ($id, '$key', '$value', '$data', '$title', '$desc')");
  119. $sth->execute();
  120. }
  121. }
  122. }
  123. public static function deletePage($id){
  124. CandyDB::q('DELETE FROM ' . DB_PREFIX . 'pages WHERE page_id = :id', compact('id'));
  125. }
  126. public static function listAddPages(){
  127. $pages = self::listPages();
  128. $html = '<ul class="add-pages-ul">';
  129. foreach ($pages as $key => $value) {
  130. $html .= '<li><label for="navpage-'.$value->page_id.'">'.$value->page_title.'</label><input type="checkbox" id="navpage-'.$value->page_id.'" value="'.$value->page_id.'" /></li>';
  131. }
  132. $html .= '</ul>';
  133. echo $html;
  134. }
  135. }