PageRenderTime 26ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/models/entry.php

https://github.com/kenichiromiya/webwritecms
PHP | 253 lines | 144 code | 27 blank | 82 comment | 23 complexity | 8a0dd613ec1b088dc7935e59a46a4c81 MD5 | raw file
  1. <?php
  2. class EntryModel extends Model {
  3. public function __construct() {
  4. parent::__construct();
  5. }
  6. public function getentryarchives() {
  7. $sql = "SELECT count(*) AS count,DATE_FORMAT(adddate,'%%Y%%m') AS adddate FROM ".$this->table." GROUP BY DATE_FORMAT(adddate, '%%Y%%m') ORDER BY adddate DESC";
  8. $sth = $this->dbh->query($sql);
  9. return $sth->fetchAll();
  10. /*
  11. $result = mysql_query($sql,$this->db);
  12. $archives = array();
  13. while ($archive=mysql_fetch_assoc($result)){
  14. array_push($archives,$archive);
  15. }
  16. return $archives;
  17. */
  18. }
  19. /*
  20. public function addentry($param) {
  21. $sql = "INSERT INTO ".TABLE_PREFIX."entry (title,category_id,body,adddate,moddate) VALUES(?,?,?,now(),now())";
  22. $sth = $this->dbh->prepare($sql);
  23. $sth->execute(array($param['title'],$param['category_id'],$param['body']));
  24. // mysql_query($query,$this->db);
  25. }
  26. public function editentry($param){
  27. $sql = "UPDATE ".TABLE_PREFIX."entry SET title = ?,category_id = ?,body = ?,moddate = now() WHERE id = ?";
  28. $sth = $this->dbh->prepare($sql);
  29. $sth->execute(array($param['title'],$param['category_id'],$param['body'],$param['id']));
  30. //mysql_query($query,$this->db);
  31. }
  32. public function deleteentry($param){
  33. $sql = "DELETE FROM ".TABLE_PREFIX."entry WHERE id = ?";
  34. $sth = $this->dbh->prepare($sql);
  35. $sth->execute(array($param['id']));
  36. // mysql_query($query,$this->db);
  37. }
  38. public function getentry($param) {
  39. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ".TABLE_PREFIX."entry WHERE id = ?";
  40. $sth = $this->dbh->prepare($sql);
  41. $sth->execute(array($param['id']));
  42. return $sth->fetch();
  43. //$result = mysql_query($query,$this->db);
  44. //$entry=mysql_fetch_assoc($result);
  45. //return $entry;
  46. }
  47. */
  48. public function getpreventry($param) {
  49. $sql = "SELECT * FROM ".$this->table." where id < ? ORDER BY id DESC limit 1";
  50. $sth = $this->dbh->prepare($sql);
  51. $sth->execute(array($param['id']));
  52. return $sth->fetch();
  53. //$result = mysql_query($sql,$this->db);
  54. //$entry=mysql_fetch_assoc($result);
  55. //return $entry;
  56. }
  57. public function getnextentry($param) {
  58. $sql = "SELECT * FROM ".$this->table." where id > ? ORDER BY id limit 1";
  59. $sth = $this->dbh->prepare($sql);
  60. $sth->execute(array($param['id']));
  61. return $sth->fetch();
  62. //$result = mysql_query($sql,$this->db);
  63. //$entry=mysql_fetch_assoc($result);
  64. //return $entry;
  65. }
  66. public function getrecententries($param){
  67. $category_id = $param['c'];
  68. $bind_param = array();
  69. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ".$this->table." ";
  70. if ($category_id) {
  71. $sql .= "WHERE category_id = ? ";
  72. array_push($bind_param,$category_id);
  73. }
  74. $sql .= "ORDER BY adddate DESC limit 0,10";
  75. $sth = $this->dbh->prepare($sql);
  76. $sth->execute($bind_param);
  77. return $sth->fetchAll();
  78. /*
  79. $result = mysql_query($sql,$this->db);
  80. $entries = array();
  81. while ($row=mysql_fetch_assoc($result)){
  82. array_push($entries,$row);
  83. }
  84. return $entries;
  85. */
  86. }
  87. public function getentries($param) {
  88. $date = $param['d'];
  89. $category_id = $param['c'];
  90. //$num_per_page = isset($param['n']) ? $param['n'] : '1';
  91. $num_per_page = isset($param['n']) ? $param['n'] : '1';
  92. $page = isset($param['p']) ? $param['p'] : '1';
  93. $start = ($page-1)*$num_per_page;
  94. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ".$this->table." ";
  95. $bind_param = array();
  96. if ($date){
  97. if (strlen($date) == 6){
  98. list($year,$mon) = sscanf($date,"%04d%02d");
  99. $adddate = sprintf("%04d-%02d%%",$year,$mon);
  100. }
  101. if (strlen($date) == 8){
  102. list($year,$mon,$mday) = sscanf($date,"%04d%02d%02d");
  103. $adddate = sprintf("%04d-%02d-%02d%%",$year,$mon,$mday);
  104. }
  105. $sql .= "WHERE adddate like ? ";
  106. array_push($bind_param,$adddate);
  107. }
  108. if ($category_id) {
  109. $sql .= "WHERE category_id = ? ";
  110. array_push($bind_param,$category_id);
  111. }
  112. $sql .= "ORDER BY adddate DESC limit $start,$num_per_page ";
  113. $sth = $this->dbh->prepare($sql);
  114. $sth->execute($bind_param);
  115. return $sth->fetchAll();
  116. /*
  117. $result = mysql_query($query,$this->db);
  118. $entries = array();
  119. while ($row=mysql_fetch_assoc($result)){
  120. array_push($entries,$row);
  121. }
  122. return $entries;
  123. */
  124. }
  125. public function getcount() {
  126. $sql = "SELECT FOUND_ROWS()";
  127. $sth = $this->dbh->prepare($sql);
  128. $sth->execute();
  129. $row = $sth->fetch();
  130. return $row[0];
  131. }
  132. public function getcalendar($year = '',$mon = '') {
  133. if ($year == '' and $mon == '') {
  134. $today = getdate();
  135. $year = $today['year'];
  136. $mon = $today['mon'];
  137. }
  138. $days = $this->getdays($year,$mon);
  139. $pn = $this->getpn($year,$mon);
  140. $d = sprintf("%04d%02d",$year,$mon);
  141. $calendar = $this->generate_calendar($year, $mon, $days, 3, "entry/?d=$d",0,$pn);
  142. return $calendar;
  143. }
  144. public function getdays($year,$mon){
  145. $adddate = sprintf("%04d-%02d%%",$year,$mon);
  146. // その月のデータを取得する
  147. $sql = "SELECT * FROM ".$this->table." WHERE adddate LIKE ?";
  148. //echo $sql;
  149. $sth = $this->dbh->prepare($sql);
  150. $sth->execute(array("$adddate"));
  151. $adddates = $sth->fetchAll();
  152. /*
  153. $result = mysql_query($sql,$this->db);
  154. $adddates = array();
  155. while ($row=mysql_fetch_assoc($result)){
  156. array_push($adddates,$row);
  157. }
  158. */
  159. $days = array();
  160. foreach ($adddates as $adddate) {
  161. // http://keithdevens.com/software/php_calendar#source
  162. list($addyear,$addmon,$addmday,$dmy,$dmy,$dmy) = sscanf($adddate['adddate'],"%04d-%02d-%02d %02d:%02d:%02d");
  163. $d = sprintf("%04d%02d%02d",$addyear,$addmon,$addmday);
  164. $days[$addmday] = array("entry/?d=$d",'linked-day');
  165. }
  166. return $days;
  167. }
  168. public function getpn($year,$mon){
  169. $timestamp = mktime(0, 0, 0, $mon, 1, $year);
  170. $nowmonth = date('Ym', $timestamp);
  171. $nextMonth = strtotime("+1 month",$timestamp);
  172. $nextmonth = date('Ym', $nextMonth);
  173. $prevMonth = strtotime("-1 month",$timestamp);
  174. $prevmonth = date('Ym', $prevMonth);
  175. // prev next :-)
  176. $pn = array('&laquo;'=>"entry/?d=$prevmonth", '&raquo;'=>"entry/?d=$nextmonth");
  177. return $pn;
  178. }
  179. public function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()){
  180. $first_of_month = gmmktime(0,0,0,$month,1,$year);
  181. #remember that mktime will automatically correct if invalid dates are entered
  182. # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
  183. # this provides a built in "rounding" feature to generate_calendar()
  184. $day_names = array(); #generate all the day names according to the current locale
  185. for($n=0,$t=(3+$first_day)*86400; $n<7; $n++,$t+=86400) #January 4, 1970 was a Sunday
  186. $day_names[$n] = ucfirst(gmstrftime('%A',$t)); #%A means full textual day name
  187. list($month, $year, $month_name, $weekday) = explode(',',gmstrftime('%m,%Y,%B,%w',$first_of_month));
  188. $weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
  189. $title = htmlentities(ucfirst($month_name)).'&nbsp;'.$year; #note that some locales don't capitalize month and day names
  190. #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
  191. @list($p, $pl) = each($pn); @list($n, $nl) = each($pn); #previous and next links, if applicable
  192. if($p) $p = '<span class="calendar-prev">'.($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p).'</span>&nbsp;';
  193. if($n) $n = '&nbsp;<span class="calendar-next">'.($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n).'</span>';
  194. $calendar = '<table class="calendar">'."\n".
  195. '<caption class="calendar-month">'.$p.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).$n."</caption>\n<tr>";
  196. if($day_name_length){ #if the day names should be shown ($day_name_length > 0)
  197. #if day_name_length is >3, the full name of the day will be printed
  198. foreach($day_names as $d)
  199. $calendar .= '<th abbr="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>';
  200. $calendar .= "</tr>\n<tr>";
  201. }
  202. if($weekday > 0) $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>'; #initial 'empty' days
  203. for($day=1,$days_in_month=gmdate('t',$first_of_month); $day<=$days_in_month; $day++,$weekday++){
  204. if($weekday == 7){
  205. $weekday = 0; #start a new week
  206. $calendar .= "</tr>\n<tr>";
  207. }
  208. if(isset($days[$day]) and is_array($days[$day])){
  209. @list($link, $classes, $content) = $days[$day];
  210. if(is_null($content)) $content = $day;
  211. $calendar .= '<td'.($classes ? ' class="'.htmlspecialchars($classes).'">' : '>').
  212. ($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
  213. }
  214. else $calendar .= "<td>$day</td>";
  215. }
  216. if($weekday != 7) $calendar .= '<td colspan="'.(7-$weekday).'">&nbsp;</td>'; #remaining "empty" days
  217. return $calendar."</tr>\n</table>\n";
  218. }
  219. }
  220. ?>