PageRenderTime 20ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/CArticle/CArticle.php

https://github.com/mosbth/medes
PHP | 443 lines | 210 code | 79 blank | 154 comment | 32 complexity | 30c767cf28a502d1a8e100ef007ed67a MD5 | raw file
  1. <?php
  2. /**
  3. * Store articles, pieces of information/content in the database.
  4. *
  5. * @package MedesCore
  6. */
  7. class CArticle implements IUsesSQL, IModule {
  8. /**#@+
  9. * @access private
  10. */
  11. /**
  12. * Reference to the database.
  13. * @var CDatabaseController
  14. */
  15. private $db;
  16. /**#@-*/
  17. /**#@+
  18. * @access public
  19. */
  20. /**
  21. * Remember last resultset.
  22. * @var array
  23. */
  24. public $res;
  25. /**
  26. * Holder of current (one) article.
  27. * @var array
  28. */
  29. public $current;
  30. /**
  31. * Columns of table article.
  32. * @var array
  33. */
  34. public $articleCols = array(
  35. // basics
  36. "id", // int primary key auto_increment
  37. "key", // text unique key,
  38. "type", // text, use to identify articles of various content types such as article, blog, news, page, etc.
  39. "title", // text, the title of the article
  40. "url", // text, the canonical url to this article
  41. "content", // text, the actual content of the article
  42. "filter", // text, function to use to filter the content, is it text, html or php or other?
  43. "template", // text, filename och template file to use when rendering the content
  44. // drafts
  45. "draftTitle", // text, a draft title
  46. "draftContent", // text, a draft content
  47. // timestamps
  48. "owner", // text, who owns this article, should be int and foreign key to user table later on.
  49. // timestamps
  50. "published", // datetime, timestamp for publishing the article
  51. "created", // datetime, timestamp for creating the article
  52. "modified", // datetime, timestamp when article was last modified
  53. "deleted", // datetime, timestamp when article was deleted
  54. /* by userid, owner, writer */
  55. /* tags */
  56. /* category */
  57. /* taxeonomy */
  58. /* meta information
  59. "author"=>array("type"=>"text"),
  60. "copyright"=>array("type"=>"text"),
  61. "description"=>array("type"=>"text"),
  62. "keywords"=>array("type"=>"text"),
  63. */
  64. );
  65. /**#@-*/
  66. /**
  67. * Constructor.
  68. */
  69. public function __construct() {
  70. global $pp;
  71. $this->db = $pp->db;
  72. $this->ClearCurrent();
  73. }
  74. /**
  75. * Destructor.
  76. */
  77. public function __destruct() {;}
  78. /**
  79. * Magic method to alarm when setting member that does not exists.
  80. */
  81. public function __set($name, $value) {
  82. echo get_class() . ": Setting undefined member: {$name} => {$value}";
  83. }
  84. /**
  85. * Magic method to alarm when getting member that does not exists.
  86. * @return mixed
  87. */
  88. public function __get($name) {
  89. throw new Exception(get_class() . ": Getting undefined member: {$name}");
  90. }
  91. /**
  92. * Implementing interface IModule. Initiating when module is installed.
  93. */
  94. public function InstallModule() {
  95. $this->db->ExecuteQuery(self::SQL('create table article'));
  96. }
  97. /**
  98. * Implementing interface IModule. Cleaning up when module is deinstalled.
  99. */
  100. public function DeinstallModule() {
  101. }
  102. /**
  103. * Implementing interface IModule. Called when updating to newer versions.
  104. */
  105. public function UpdateModule() {
  106. }
  107. /**
  108. * Implementing interface IUsesSQL. Encapsulate all SQL used by this class.
  109. *
  110. * @param string $id the string that is the key of a SQL-entry in the array
  111. */
  112. public static function SQL($id=null) {
  113. $query = array(
  114. 'create table article' => 'create table if not exists article(id integer primary key autoincrement, key text unique, type text, title text, url text, content text, filter text, template text, draftTitle text, draftContent text, owner text, published datetime, created datetime, modified datetime, deleted datetime)',
  115. 'insert new article' => 'insert into article(%s,owner,published,created,modified,deleted) values(%s,?,null,datetime("now"),null,null)',
  116. 'update article' => 'update article set modified=datetime("now") %s where id=?',
  117. 'update article as published' => 'update article set published=datetime("now") where id=?',
  118. 'update article as unpublished' => 'update article set published=null where id=?',
  119. 'update article as deleted' => 'update article set deleted=datetime("now") where id=?',
  120. 'update article as restored' => 'update article set deleted=null where id=?',
  121. 'update article unset draft' => 'update article set draftTitle=null, draftContent=null where id=?',
  122. 'update change key' => 'update article set key=? where key=?',
  123. 'select article by id' => 'select * from article where id=?',
  124. 'select article by key' => 'select * from article where key=?',
  125. 'select article id by key' => 'select id from article where key=?',
  126. 'select * by type' => 'select * from article where type=?',
  127. 'select * by type no deleted' => 'select * from article where type=? and deleted is null',
  128. );
  129. if(!isset($query[$id])) {
  130. throw new Exception(t('#class error: Out of range. Query = @id', array('#class'=>get_class(), '@id'=>$id)));
  131. }
  132. return $query[$id];
  133. }
  134. /**
  135. * Load article from db. Use $this->current to find out which article to load.
  136. * @param int The id to load, overrides $current.
  137. * @returns boolean wether succeeded with loading or not.
  138. */
  139. public function Load($id = null) {
  140. // id provided, use it
  141. if(!empty($id)) {
  142. $this->current['id'] = $id;
  143. }
  144. // Article has id, use it
  145. if(isset($this->current['id'])) {
  146. $this->res = $this->db->ExecuteSelectQueryAndFetchAll(self::SQL('select article by id'), array($this->current['id']));
  147. }
  148. // Article has key, use it
  149. else if(isset($this->current['key'])) {
  150. $this->res = $this->db->ExecuteSelectQueryAndFetchAll(self::SQL('select article by key'), array($this->current['key']));
  151. }
  152. else {
  153. throw new Exception(t('Load() article without id or key.'));
  154. }
  155. // Max one item is returned, set this as current
  156. if(empty($this->res[0]) && isset($this->current['key'])) {
  157. $key = $this->current['key'];
  158. $this->ClearCurrent();
  159. $this->current['key'] = $key;
  160. return false;
  161. } else if(empty($this->res[0])) {
  162. $this->ClearCurrent();
  163. return false;
  164. } else {
  165. $this->current = $this->res[0];
  166. return true;
  167. }
  168. }
  169. /**
  170. * Load article from db, by using key
  171. */
  172. public function LoadByKey($key=null) {
  173. if(!empty($key)) {
  174. $this->SetKey($key);
  175. }
  176. $this->SetId(null);
  177. return $this->Load();
  178. }
  179. /**
  180. * Insert new article to db.
  181. */
  182. public function Insert() {
  183. $a = $this->current;
  184. unset($a['id'], $a['published'], $a['created'], $a['modified'], $a['deleted']);
  185. foreach($a as $key=>$val) {
  186. if(!isset($a[$key])) {
  187. unset($a[$key]);
  188. }
  189. }
  190. $uc = CUserController::GetInstance();
  191. $a['owner'] = $uc->IsAuthenticated() ? $uc->GetUserAccount() : "root";
  192. $q = sprintf(self::SQL('insert new article'), implode(",", array_keys($a)), implode(",", array_fill(1,sizeof($a), "?")));
  193. $this->db->ExecuteQuery($q, array_values($a));
  194. $this->SetId($this->db->LastInsertId());
  195. }
  196. /**
  197. * Update existing article in db.
  198. */
  199. public function Update() {
  200. if(!$this->GetId()) {
  201. throw new Exception(t('Update() without id set.'));
  202. }
  203. $a = $this->current;
  204. unset($a['id'], $a['published'], $a['created'], $a['modified'], $a['deleted']);
  205. $assign = "";
  206. foreach($a as $key=>$val) {
  207. if(!isset($a[$key])) {
  208. unset($a[$key]);
  209. } else {
  210. $assign .= ",{$key}=?";
  211. }
  212. }
  213. $a['id'] = $this->GetId();
  214. $q = sprintf(self::SQL('update article'), $assign);
  215. $this->db->ExecuteQuery($q, array_values($a));
  216. }
  217. /**
  218. * Save article to db.
  219. */
  220. public function Save() {
  221. // Article has id, do update
  222. if($this->GetId()) {
  223. $this->Update();
  224. }
  225. // Article has key, do update if exists or insert it
  226. if($this->GetKey()) {
  227. $res = $this->db->ExecuteSelectQueryAndFetchAll(self::SQL('select article id by key'), array($this->GetKey()));
  228. if(empty($res)) {
  229. $this->Insert();
  230. } else {
  231. $this->SetId($res[0]['id']);
  232. $this->Update();
  233. }
  234. }
  235. // Insert new article
  236. else {
  237. $this->Insert();
  238. }
  239. }
  240. /**
  241. * Delete object from database.
  242. * @param boolean $really Put object in wastebasket (false) or really delete row from table (true)
  243. */
  244. public function Delete($really=false) {
  245. if(!$this->GetId()) throw new Exception(t('No id set.'));
  246. $this->db->ExecuteQuery(self::SQL('update article as deleted'), array($this->GetId()));
  247. if($really) die('Delete($really=true) not implemented');
  248. }
  249. /**
  250. * Restore a deleted object.
  251. */
  252. public function Restore() {
  253. if(!$this->GetId()) throw new Exception(t('No id set.'));
  254. $this->db->ExecuteQuery(self::SQL('update article as restored'), array($this->GetId()));
  255. }
  256. /**
  257. * List all articles of specified type.
  258. * @param string $type the type of articles to show.
  259. * @param boolean $deleted default false, set to true to include deleted pages
  260. * @returns array with information on articles.
  261. */
  262. public function ListByType($type, $deleted=false) {
  263. if($deleted) {
  264. return $this->db->ExecuteSelectQueryAndFetchAll(self::SQL('select * by type'), array($type));
  265. } else {
  266. return $this->db->ExecuteSelectQueryAndFetchAll(self::SQL('select * by type no deleted'), array($type));
  267. }
  268. }
  269. /**
  270. * Rename article key.
  271. */
  272. public function RenameKey($key, $newKey) {
  273. $this->db->ExecuteQuery(self::SQL('update change key'), array($newKey, $key));
  274. if($this->db->RowCount() == 1) {
  275. return true;
  276. } else {
  277. return "Failed.";
  278. //return false;
  279. }
  280. }
  281. /**
  282. * Publish article.
  283. */
  284. public function Publish() {
  285. if(!$this->GetId()) throw new Exception(t('No id set.'));
  286. $this->db->ExecuteQuery(self::SQL('update article as published'), array($this->GetId()));
  287. }
  288. /**
  289. * Unpublish article.
  290. */
  291. public function Unpublish() {
  292. if(!$this->GetId()) throw new Exception(t('No id set.'));
  293. $this->db->ExecuteQuery(self::SQL('update article as unpublished'), array($this->GetId()));
  294. }
  295. /**
  296. * Remove draft article.
  297. */
  298. public function UnsetDraft() {
  299. if(!$this->GetId()) throw new Exception(t('No id set.'));
  300. $this->db->ExecuteQuery(self::SQL('update article unset draft'), array($this->GetId()));
  301. }
  302. /**
  303. * Clear current article.
  304. */
  305. public function ClearCurrent() {
  306. $this->current = array_fill_keys($this->articleCols, null);
  307. }
  308. /**
  309. * Setters and getters
  310. */
  311. public function SetId($value) { $this->current['id'] = $value; }
  312. public function GetId() { return $this->current['id']; }
  313. public function SetKey($value) { $this->current['key'] = $value; }
  314. public function GetKey() { return $this->current['key']; }
  315. public function SetTitle($value) { $this->current['title'] = $value; }
  316. public function GetTitle() { return $this->current['title']; }
  317. public function SetContent($value) { $this->current['content'] = $value; }
  318. public function GetContent() { return $this->current['content']; }
  319. public function SetFilter($value) { $this->current['filter'] = $value; }
  320. public function GetFilter() { return $this->current['filter']; }
  321. public function SetTemplate($value) { $this->current['template'] = $value; }
  322. public function GetTemplate() { return $this->current['template']; }
  323. public function SetCanonicalUrl($value) { $this->current['url'] = $value; }
  324. public function GetCanonicalUrl() { return $this->current['url']; }
  325. public function SetDraftTitle($value) { $this->current['draftTitle'] = $value; }
  326. public function GetDraftTitle() { return $this->current['draftTitle']; }
  327. public function SetDraftContent($value) { $this->current['draftContent'] = $value; }
  328. public function GetDraftContent() { return $this->current['draftContent']; }
  329. public function SetType($value) { $this->current['type'] = $value; }
  330. public function GetType() { return $this->current['type']; }
  331. public function GetOwner() { return $this->current['owner']; }
  332. public function GetPublished() { return $this->current['published']; }
  333. public function GetCreated() { return $this->current['created']; }
  334. public function GetModified() { return $this->current['modified']; }
  335. public function GetDeleted() { return $this->current['deleted']; }
  336. /*
  337. // ------------------------------------------------------------------------------------
  338. //
  339. // Get all articles.
  340. //
  341. public function GetArticles($attributes=array('*'), $order=array(), $range=array('limit'=>10), $where=array()){
  342. return $this->adb->GetArticles($attributes, $order, $range, $where);
  343. }
  344. // ------------------------------------------------------------------------------------
  345. //
  346. // Save a new article.
  347. //
  348. public function SaveNew($attributes){
  349. return $this->adb->SaveNew($attributes);
  350. }
  351. // ------------------------------------------------------------------------------------
  352. //
  353. // Delete all articles by owner.
  354. //
  355. public function DeleteAllByOwner($aOwner) {
  356. $this->adb->DeleteAllByOwner($aOwner);
  357. }
  358. */
  359. }