PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/objects/story.inc.php

https://github.com/adamfranco/segue-1.x
PHP | 879 lines | 725 code | 76 blank | 78 comment | 49 complexity | e70391db6c36304fcf4eb8f5406bfbd8 MD5 | raw file
  1. <? /* $Id$ */
  2. class story extends segue {
  3. /* var $_allfields = array("page_id","section_id","site_id","title","addedby","addedtimestamp", */
  4. /* "editedby","editedtimestamp","shorttext","longertext", */
  5. /* "activatedate","deactivatedate","discuss", */
  6. /* "locked","category","discussions","texttype","type","url","active"); */
  7. var $_allfields = array("page_id","section_id","site_id","title","addedby","addedtimestamp", "addedbyfull", "editedbyfull",
  8. "editedby","editedtimestamp","shorttext","longertext",
  9. "activatedate","deactivatedate","discuss","discussdisplay","discussauthor","discussemail","discusslabel",
  10. "locked","category","discussions","texttype","type","url","active");
  11. // fields listed in $_datafields are stored in the database.
  12. // the first element is the table join syntax required to pull the data.
  13. // the second element is an array of the database fields we will be selecting
  14. // the third element is the database field by which we will sort
  15. var $_datafields = array(
  16. "id" => array(
  17. "story",
  18. array("story_id"),
  19. "story_id"
  20. ),
  21. "site_id" => array(
  22. "story
  23. INNER JOIN
  24. page
  25. ON FK_page = page_id
  26. INNER JOIN
  27. section
  28. ON FK_section = section_id
  29. site
  30. ON section.FK_site = site.site_id
  31. INNER JOIN
  32. slot
  33. ON site.site_id = slot.FK_site
  34. ",
  35. array("FK_site"),
  36. "story_id"
  37. ),
  38. "section_id" => array(
  39. "story
  40. INNER JOIN
  41. page
  42. ON FK_page = page_id
  43. ",
  44. array("FK_section"),
  45. "story_id"
  46. ),
  47. "page_id" => array(
  48. "story",
  49. array("FK_page"),
  50. "story_id"
  51. ),
  52. "type" => array(
  53. "story",
  54. array("story_display_type"),
  55. "story_id"
  56. ),
  57. "title" => array(
  58. "story",
  59. array("story_title"),
  60. "story_id"
  61. ),
  62. "activatedate" => array(
  63. "story",
  64. array("DATE_FORMAT(story_activate_tstamp, '%Y-%m-%d')"),
  65. "story_id"
  66. ),
  67. "deactivatedate" => array(
  68. "story",
  69. array("DATE_FORMAT(story_deactivate_tstamp, '%Y-%m-%d')"),
  70. "story_id"
  71. ),
  72. "active" => array(
  73. "story",
  74. array("story_active"),
  75. "story_id"
  76. ),
  77. "url" => array(
  78. "story
  79. LEFT JOIN
  80. media
  81. ON FK_media = media_id",
  82. array("media_tag"),
  83. "story_id"
  84. ),
  85. "locked" => array(
  86. "story",
  87. array("story_locked"),
  88. "story_id"
  89. ),
  90. "editedby" => array(
  91. "story
  92. INNER JOIN
  93. user
  94. ON FK_updatedby = user_id",
  95. array("user_uname"),
  96. "story_id"
  97. ),
  98. "editedbyfull" => array(
  99. "story
  100. INNER JOIN
  101. user
  102. ON FK_updatedby = user_id",
  103. array("user_fname"),
  104. "story_id"
  105. ),
  106. "editedtimestamp" => array(
  107. "story",
  108. array("story_updated_tstamp"),
  109. "story_id"
  110. ),
  111. "addedby" => array(
  112. "story
  113. INNER JOIN
  114. user
  115. ON FK_createdby = user_id",
  116. array("user_uname"),
  117. "story_id"
  118. ),
  119. "addedbyfull" => array(
  120. "story
  121. INNER JOIN
  122. user
  123. ON FK_createdby = user_id",
  124. array("user_fname"),
  125. "story_id"
  126. ),
  127. "addedtimestamp" => array(
  128. "story",
  129. array("story_created_tstamp"),
  130. "story_id"
  131. ),
  132. "discuss" => array(
  133. "story",
  134. array("story_discussable"),
  135. "story_id"
  136. ),
  137. "discussdisplay" => array(
  138. "story",
  139. array("story_discussdisplay"),
  140. "story_id"
  141. ),
  142. "discussauthor" => array(
  143. "story",
  144. array("story_discussauthor"),
  145. "story_id"
  146. ),
  147. "discussemail" => array(
  148. "story",
  149. array("story_discussemail"),
  150. "story_id"
  151. ),
  152. "discusslabel" => array(
  153. "story",
  154. array("story_discusslabel"),
  155. "story_id"
  156. ),
  157. "category" => array(
  158. "story",
  159. array("story_category"),
  160. "story_id"
  161. ),
  162. "texttype" => array(
  163. "story",
  164. array("story_text_type"),
  165. "story_id"
  166. ),
  167. "discussions" => array(
  168. "story
  169. INNER JOIN
  170. discussion
  171. ON FK_story = story_id
  172. ",
  173. array("discussion_id"),
  174. "discussion_order"
  175. ),
  176. "shorttext" => array(
  177. "story",
  178. array("story_text_short"),
  179. "story_id"
  180. ),
  181. "longertext" => array(
  182. "story",
  183. array("story_text_long"),
  184. "story_id"
  185. )
  186. );
  187. var $_table = "story";
  188. function story($insite,$insection,$inpage,$id=0,&$pageObj) {
  189. $this->owning_site = $insite;
  190. $this->owning_section = $insection;
  191. $this->owning_page = $inpage;
  192. $this->owningPageObj = &$pageObj;
  193. $this->owningSectionObj = &$this->owningPageObj->owningSectionObj;
  194. $this->owningSiteObj = &$this->owningPageObj->owningSectionObj->owningSiteObj;
  195. $this->fetchedup = 1;
  196. $this->id = $id;
  197. // initialize the data array
  198. $this->data[site_id] = $insite;
  199. $this->data[section_id] = $insection;
  200. $this->data[page_id] = $inpage;
  201. $this->init();
  202. $this->data[type] = "story";
  203. }
  204. function init($formdates=0) {
  205. if (!is_array($this->data)) $this->data = array();
  206. $this->data[title] = "";
  207. $this->data[activatedate] = $this->data[deactivatedate] = "0000-00-00";
  208. $this->data[shorttext] = $this->data[longertext] = "";
  209. /* $this->data[discuss] = 0; */
  210. $this->data[texttype] = "text";
  211. $this->data[category] = "";
  212. $this->data[url] = "http://";
  213. $this->data[locked] = 0;
  214. if ($this->id) $this->fetchFromDB();
  215. if ($formdates) $this->initFormDates();
  216. }
  217. function getFirst($length) {
  218. if ($this->getField("type") == "image" && $this->getField("shorttext") == "")
  219. return "Image";
  220. else {
  221. $text = $this->getField("shorttext");
  222. $text = strip_tags($text);
  223. if (strlen($text) <= $length) return $text;
  224. $text = substr($text,0,$length)."...";
  225. return $text;
  226. }
  227. }
  228. /******************************************************************************
  229. * these functions are old & suck.
  230. ******************************************************************************/
  231. function addDiscussion($id) {
  232. if (!$this->getField("discussions")) $this->data[discussions] = array();
  233. array_push($this->data[discussions],$id);
  234. $this->changed[discussions] = 1;
  235. }
  236. function delDiscussion($id) {
  237. if (!$this->getField("discussions")) return;
  238. $d = array();
  239. foreach ($this->data[discussions] as $i) {
  240. if ($i != $id) $d[] = $i;
  241. }
  242. $this->data[discussions] = $d;
  243. $this->changed[discussiosn] = 1;
  244. }
  245. /******************************************************************************
  246. * end
  247. ******************************************************************************/
  248. function delete($deleteFromParent=0) { // delete from db
  249. if (!$this->id) return false;
  250. if ($deleteFromParent) {
  251. $parentObj =& new page($this->owning_site,$this->owning_section,$this->owning_page,$this->owningPageObj->owningSectionObj);
  252. $parentObj->fetchDown();
  253. /* print "<br />delStory - ".$this->id."<br />"; */
  254. $parentObj->delStory($this->id);
  255. $parentObj->updateDB();
  256. } else {
  257. $query = "DELETE FROM story WHERE story_id='".addslashes($this->id)."'";
  258. db_query($query);
  259. $query = "DELETE FROM permission WHERE FK_scope_id='".addslashes($this->id)."' AND permission_scope_type='story';";
  260. db_query($query);
  261. $query = "DELETE FROM discussion WHERE FK_story='".addslashes($this->id)."'";
  262. db_query($query);
  263. delete_record_tags($this->owning_site,$this->id,"story",$user_id="");
  264. $this->clearPermissions();
  265. $this->updatePermissionsDB();
  266. }
  267. }
  268. function fetchUp($full = 0) {
  269. if (!$this->fetchedup || $full) {
  270. $this->owningSiteObj =& new site($this->owning_site);
  271. $this->owningSiteObj->fetchFromDB();
  272. // $this->owningSiteObj->buildPermissionsArray(1);
  273. $this->owningSectionObj =& new section($this->owning_site,$this->owning_section,$this->owningSiteObj);
  274. $this->owningSectionObj->fetchFromDB();
  275. // $this->owningSectionObj->buildPermissionsArray(1);
  276. $this->owningPageObj =& new page($this->owning_site,$this->owning_section,$this->owning_page,$this->owningSectionObj);
  277. $this->owningPageObj->fetchFromDB();
  278. // $this->owningPageObj->buildPermissionsArray(1);
  279. $this->fetchedup = 1;
  280. }
  281. }
  282. function fetchDown($full=0) {
  283. if (!$this->fetcheddown || $full) {
  284. if (!$this->tobefetched || $full) $this->fetchFromDB(0,$full);
  285. $this->fetcheddown = 1;
  286. }
  287. }
  288. function fetchFromDB($id=0,$force=0) {
  289. if ($id) $this->id = $id;
  290. global $dbuser, $dbpass, $dbdb, $dbhost;
  291. global $cfg;
  292. // take this out when appropriate & replace occurences;
  293. global $uploaddir;
  294. $this->tobefetched=1;
  295. //$this->id = $this->getField("id"); // why need to do this?
  296. if ($force) {
  297. // the code below is inefficient! why fetch each field separately when we can fetch all fields at same time
  298. // thus we can cut the number of queries significantly
  299. /* foreach ($this->_allfields as $f) {
  300. $this->getField($f);
  301. }
  302. */
  303. // connect to db and initialize data array
  304. db_connect($dbhost,$dbuser,$dbpass, $dbdb);
  305. $this->data = array();
  306. // first fetch all fields that are not part of a 1-to-many relationship
  307. $query = "
  308. SELECT
  309. story_display_type AS type,
  310. story_title AS title,
  311. DATE_FORMAT(story_activate_tstamp, '%Y-%m-%d') AS activatedate,
  312. DATE_FORMAT(story_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  313. story_active AS active,
  314. story_locked AS locked,
  315. story_updated_tstamp AS editedtimestamp,
  316. story_created_tstamp AS addedtimestamp,
  317. story_discussable AS discuss,
  318. story_discussdisplay AS discussdisplay,
  319. story_discussauthor AS discussauthor,
  320. story_discussemail AS discussemail,
  321. story_discusslabel AS discusslabel,
  322. story_category AS category,
  323. story_text_type AS texttype,
  324. story_text_short AS shorttext,
  325. story_text_long AS longertext,
  326. media_tag AS url,
  327. user_createdby.user_uname AS addedby,
  328. user_updatedby.user_uname AS editedby,
  329. slot_name as site_id,
  330. FK_section AS section_id,
  331. FK_page as page_id
  332. FROM
  333. story
  334. INNER JOIN
  335. page
  336. ON FK_page = page_id
  337. INNER JOIN
  338. section
  339. ON FK_section = section_id
  340. INNER JOIN
  341. user AS user_createdby
  342. ON story.FK_createdby = user_createdby.user_id
  343. INNER JOIN
  344. user AS user_updatedby
  345. ON story.FK_updatedby = user_updatedby.user_id
  346. INNER JOIN
  347. site
  348. ON section.FK_site = site.site_id
  349. INNER JOIN
  350. slot
  351. ON site.site_id = slot.FK_site
  352. LEFT JOIN
  353. media
  354. ON story.FK_media = media_id
  355. WHERE
  356. story_id = '".addslashes($this->id)."'
  357. ";
  358. $r = db_query($query);
  359. $a = db_fetch_assoc($r);
  360. array_change_key_case($a); // make all keys lower case
  361. // for each field returned by the query
  362. foreach ($a as $field => $value)
  363. // make sure we have defined this field in the _allfields array
  364. if (in_array($field,$this->_allfields)) {
  365. // decode if necessary
  366. if (in_array($field,$this->_encode))
  367. $value = stripslashes(urldecode($value));
  368. $this->data[$field] = $value;
  369. // print "$field] = $value<br />";
  370. $this->fetched[$field] = 1;
  371. }
  372. else
  373. echo "ERROR: field $field not in _allfields!!!<br />";
  374. // now fetch the discussion entries for this story
  375. $query = "
  376. SELECT
  377. discussion_id
  378. FROM
  379. story
  380. INNER JOIN
  381. discussion
  382. ON FK_story = story_id
  383. WHERE
  384. story_id = '".addslashes($this->id)."'
  385. ORDER BY
  386. discussion_order
  387. ";
  388. $r = db_query($query);
  389. $this->data[discussions] = array();
  390. while ($a = db_fetch_assoc($r))
  391. $this->data[discussions][] = $a[discussion_id];
  392. $this->fetched[discussions] = 1;
  393. }
  394. return $this->id;
  395. }
  396. function updateDB($down=0, $force=0, $keepEditHistory=0) {
  397. if ($this->changed) {
  398. /******************************************************************************
  399. * get story text, convert wiki links to internal links
  400. ******************************************************************************/
  401. // $text = $this->getField("shorttext");
  402. // $text = convertWikiMarkupToLinks($this->owning_site, $this->owning_section, $this->owning_page, $this->id, "page", $text);
  403. // // $text = recordInternalLinks ($_SESSION[settings][site],$_SESSION[settings][section],$_SESSION[settings][page], $page_title, $text);
  404. // $text = convertInteralLinksToTags($this->owning_site, $text);
  405. // $text = $this->setField("shorttext", $text);
  406. //
  407. // $text = $this->getField("longertext");
  408. // $text = convertWikiMarkupToLinks($this->owning_site, $this->owning_section, $this->owning_page, $this->id, "page", $text);
  409. // // $text = recordInternalLinks ($_SESSION[settings][site],$_SESSION[settings][section],$_SESSION[settings][page], $page_title, $text);
  410. // $text = convertInteralLinksToTags($this->owning_site, $text);
  411. // $text = $this->setField("longertext", $text);
  412. $this->parseMediaTextForDB("shorttext");
  413. $this->parseMediaTextForDB("longertext");
  414. $a = $this->createSQLArray();
  415. if ($keepEditHistory) {
  416. $a[] = $this->_datafields[editedtimestamp][1][0]."='".$this->getField("editedtimestamp")."'";
  417. } else
  418. $a[] = "FK_updatedby=".$_SESSION[aid];
  419. $query = "
  420. UPDATE
  421. story
  422. SET
  423. ".implode(",",$a)."
  424. WHERE
  425. story_id='".addslashes($this->id)."'
  426. ";
  427. /* print "<pre>Story->UpdateDB: $query<br />"; */
  428. db_query($query);
  429. /* print mysql_error()."<br />"; */
  430. /* print_r($this->data['stories']); */
  431. /* print "</pre>"; */
  432. /******************************************************************************
  433. * update the page updated timestamp
  434. ******************************************************************************/
  435. $query = "
  436. UPDATE
  437. page
  438. SET
  439. page_updated_tstamp = NOW()
  440. WHERE
  441. page_id='".addslashes($this->getField("page_id"))."'
  442. ";
  443. db_query($query);
  444. // the hard step: update the fields in the JOIN tables
  445. /******************************************************************************
  446. * Update version table
  447. ******************************************************************************/
  448. save_version($this->getField("shorttext"), $this->getField("longertext"), $this->id, $this->version_comments);
  449. // Urls are now stored in the media table
  450. if ($this->changed[url] && ($this->getField("type") == 'link' || $this->getField("type") == 'rss')) {
  451. // Urls are now stored in the media table
  452. // get id of media item
  453. $query = "
  454. SELECT
  455. FK_media
  456. FROM
  457. story
  458. WHERE
  459. story_id = '".addslashes($this->id)."'
  460. ";
  461. $a = db_fetch_assoc(db_query($query));
  462. $media_id = $a[FK_media];
  463. $query = "
  464. UPDATE
  465. media
  466. SET
  467. media_tag = '".$this->data[url]."',
  468. FK_updatedby = ".$_SESSION[aid]."
  469. WHERE
  470. media_id = '".addslashes($media_id)."'
  471. ";
  472. db_query($query);
  473. }
  474. }
  475. // update permissions
  476. $this->updatePermissionsDB($force);
  477. // add log entry, now handled elsewhere
  478. /* log_entry("edit_story",$this->owning_site,$this->owning_section,$this->owning_page,"$_SESSION[auser] edited content id ".$this->id." in site ".$this->owning_site); */
  479. return true;
  480. }
  481. function insertDB($down=0, $newsite=null, $newsection=0, $newpage=0, $removeOrigional=0, $keepaddedby=0, $keepDiscussions=0, $storyTags = null) {
  482. $origsite = $this->owning_site;
  483. $origid = $this->id;
  484. if ($newsite) {
  485. $this->owning_site = $newsite;
  486. unset($this->owningSiteObj);
  487. }
  488. if ($newsection) {
  489. $this->owning_section = $newsection;
  490. unset($this->owningSectionObj);
  491. }
  492. if ($newpage) {
  493. $this->owning_page = $newpage;
  494. unset($this->owningPageObj);
  495. }
  496. $this->fetchUp(1);
  497. /*********************************************************
  498. * Re-Key the ordering of the rest of the stories in the
  499. * section to make sure that there are no holes
  500. *********************************************************/
  501. foreach($this->owningPageObj->getField("stories") as $order => $storyId) {
  502. $query =
  503. "UPDATE
  504. story
  505. SET
  506. story_order = '".addslashes($order)."'
  507. WHERE
  508. story_id = '".$storyId."'";
  509. // printpre($query);
  510. db_query($query);
  511. }
  512. // if moving to a new site, copy the media
  513. if ($origsite != $this->owning_site && $down) {
  514. $images = array();
  515. if ($this->getField("type") == "image" || $this->getField("type") == "rss" || $this->getField("type") == "file") {
  516. $media_id = $this->getField("longertext");
  517. $this->setField("longertext",copy_media($media_id,$newsite));
  518. } else if ($this->getField("type") == "story") {
  519. // These do some moving of files based on a ####id##### syntax
  520. // for storing inline images.
  521. // Adam 2005-06-27: I don't believe that these were ever used, but
  522. // I'll leave them here so as not to break any old data that uses them.
  523. $ids = segue::getMediaIDs("shorttext");
  524. segue::replaceMediaIDs($ids,"shorttext",$newsite);
  525. $ids = segue::getMediaIDs("longertext");
  526. segue::replaceMediaIDs($ids,"longertext",$newsite);
  527. // Search for and copy images that use the "[[mediapath]]/filename.ext"
  528. // syntax.
  529. preg_match_all("/\[\[mediapath\]\]\/([^'\"]+)/",
  530. $this->getField("shorttext").$this->getField("longertext"),
  531. $matches);
  532. $fnames = array_unique($matches[1]);
  533. foreach ($fnames as $fname) {
  534. copy_media_with_fname($fname, $origsite, $newsite);
  535. }
  536. }
  537. }
  538. $a = $this->createSQLArray(1);
  539. if (!$keepaddedby) {
  540. $a[] = "FK_createdby='".addslashes($_SESSION[aid])."'";
  541. $a[] = $this->_datafields[addedtimestamp][1][0]."=NOW()";
  542. $a[] = "FK_updatedby='".addslashes($_SESSION[aid])."'";
  543. } else {
  544. $a[] = "FK_createdby=".db_get_value("user","user_id","user_uname='".addslashes($this->getField("addedby"))."'");
  545. $a[] = $this->_datafields[addedtimestamp][1][0]."='".addslashes($this->getField("addedtimestamp"))."'";
  546. $a[] = "FK_updatedby=".db_get_value("user","user_id","user_uname='".addslashes($this->getField("editedby"))."'");
  547. $a[] = $this->_datafields[editedtimestamp][1][0]."='".addslashes($this->getField("editedtimestamp"))."'";
  548. }
  549. // insert media (url)
  550. if ($this->data[url] && ($this->data['type'] == 'link' || $this->data['type'] == 'rss')) {
  551. // first see, if media item already exists in media table
  552. $query = "
  553. SELECT
  554. media_id
  555. FROM
  556. media
  557. WHERE
  558. FK_site = '".addslashes($this->owningSiteObj->id)."' AND
  559. FK_createdby = '".addslashes($_SESSION[aid])."' AND
  560. media_tag = '".addslashes($this->data[url])."' AND
  561. media_location = 'remote'
  562. ";
  563. $r = db_query($query);
  564. // if not in media table insert it
  565. if (!db_num_rows($r)) {
  566. $query = "
  567. INSERT INTO
  568. media
  569. SET
  570. FK_site = '".addslashes($this->owningSiteObj->id)."',
  571. FK_createdby = '".addslashes($_SESSION[aid])."',
  572. media_tag = '".addslashes($this->data[url])."',
  573. media_location = 'remote',
  574. FK_updatedby = '".addslashes($_SESSION[aid])."'
  575. ";
  576. db_query($query);
  577. $a[] = "FK_media=".lastid();
  578. }
  579. // if in media table, assign the media id
  580. else {
  581. $arr = db_fetch_assoc($r);
  582. $a[] = "FK_media='".addslashes($arr[media_id])."'";
  583. }
  584. }
  585. $query = "INSERT INTO story SET ".implode(",",$a);
  586. db_query($query);
  587. $this->id = lastid();
  588. /******************************************************************************
  589. * get story text, convert wiki links to internal links
  590. ******************************************************************************/
  591. // $text = $this->getField("shorttext");
  592. // $text = convertWikiMarkupToLinks($this->owning_site, $this->owning_section, $this->owning_page, $this->id, "page", $text);
  593. // $text = recordInternalLinks ($_SESSION[settings][site],$_SESSION[settings][section],$_SESSION[settings][page], $page_title, $text);
  594. // $shorttext = convertInteralLinksToTags($this->owning_site, $text);
  595. // $text = $this->getField("longertext");
  596. // $text = convertWikiMarkupToLinks($this->owning_site, $this->owning_section, $this->owning_page, $this->id, "page", $text);
  597. // $text = recordInternalLinks ($_SESSION[settings][site],$_SESSION[settings][section],$_SESSION[settings][page], $page_title, $text);
  598. // $longertext = convertInteralLinksToTags($this->owning_site, $text);
  599. // update table with new short and long text
  600. // $query = "UPDATE
  601. // story
  602. // SET
  603. // story_text_short ='".addslashes($shorttext)."',
  604. // story_text_long ='".addslashes($longertext)."'
  605. // WHERE
  606. // story_id ='".addslashes($this->id)."'
  607. // ";
  608. //
  609. // db_query($query);
  610. //
  611. // See if there is a site hash (meaning that we are being copied).
  612. // If so, try to match our id with the hash entry for 'NEXT'.
  613. if ($GLOBALS['__site_hash']['stories']
  614. && $oldId = array_search('NEXT', $GLOBALS['__site_hash']['stories']))
  615. {
  616. $GLOBALS['__site_hash']['stories'][$oldId] = $this->id;
  617. }
  618. $this->fetchUp();
  619. /* $this->owningPageObj->addStory($this->id); */
  620. if ($removeOrigional) {
  621. $this->owningPageObj->delStory($origid,0);
  622. $this->owningPageObj->updateDB();
  623. }
  624. /******************************************************************************
  625. * update the page updated timestamp
  626. ******************************************************************************/
  627. $query = "
  628. UPDATE
  629. page
  630. SET
  631. page_updated_tstamp = NOW()
  632. WHERE
  633. page_id='".addslashes($this->getField("page_id"))."'
  634. ";
  635. db_query($query);
  636. /******************************************************************************
  637. * Update version table
  638. ******************************************************************************/
  639. save_version($this->getField("shorttext"), $this->getField("longertext"), $this->id, $this->version_comments);
  640. // add new permissions entry.. force update
  641. $this->updatePermissionsDB(1);
  642. if ($keepDiscussions && $this->fetcheddown && $this->data[discussions]) {
  643. $idMapping = array();
  644. $discussionData = array();
  645. // The discussions objects are way to fucked up to use to copy the
  646. // posts so we are going to have to do this 'maunally'.
  647. // Fetch all of the discussling data
  648. foreach($this->data[discussions] as $discussionId) {
  649. // Get all the posts and dump their properties into an array.
  650. $query = "
  651. SELECT
  652. *
  653. FROM
  654. discussion
  655. WHERE
  656. discussion_id='".addslashes($discussionId)."'";
  657. $r = db_query($query);
  658. $discussionData[$discussionId] = db_fetch_assoc($r);
  659. }
  660. // Insert new posts, pointing to the new story
  661. foreach (array_keys($discussionData) as $oldId) {
  662. // Insert the post
  663. $query =
  664. "INSERT INTO
  665. discussion
  666. SET";
  667. // Set the FK_story to our new story ID
  668. $discussionData[$oldId]['FK_story'] = $this->id;
  669. // Add the rest of the fields.
  670. $i=0;
  671. foreach ($discussionData[$oldId] as $field => $val) {
  672. if ($field != 'discussion_id' && $val) {
  673. $query .= "\n\t".(($i==0)?"":", ").$field."='".addslashes($val)."'";
  674. $i++;
  675. }
  676. }
  677. $r = db_query($query);
  678. // store the id mapping
  679. $idMapping[$oldId] = lastid();
  680. if ($GLOBALS['__site_hash']['discussions']){
  681. $GLOBALS['__site_hash']['discussions'][$oldId] = lastid();
  682. }
  683. }
  684. // go through and update all of the FK_parents to point to the new Ids.
  685. // Also, rebuild the discussions array in case we try to access it after
  686. // the copy.
  687. $this->data[discussions] = array();
  688. foreach (array_keys($discussionData) as $oldId) {
  689. $newId = $idMapping[$oldId];
  690. $this->data[discussions][] = $newId;
  691. // If we were a reply, update our parent key
  692. if ($discussionData[$oldId]['FK_parent']) {
  693. $query = "
  694. UPDATE
  695. discussion
  696. SET
  697. FK_parent = '".addslashes($idMapping[$discussionData[$oldId]['FK_parent']])."',
  698. discussion_tstamp = '".addslashes($discussionData[$oldId]['discussion_tstamp'])."'
  699. WHERE
  700. discussion_id = '".addslashes($newId)."'
  701. ";
  702. // printpre($query);
  703. $res = db_query($query);
  704. }
  705. }
  706. }
  707. // Update any story tags
  708. if (is_array($storyTags)) {
  709. save_record_tags($storyTags, null, $this->id, $_SESSION[aid], "story");
  710. }
  711. return true;
  712. }
  713. function createSQLArray($all=0) {
  714. $d = $this->data;
  715. $a = array();
  716. /* if (!isset($this->owningSiteObj)) $this->owningSiteObj =& new site($this->owning_site); */
  717. /* if ($all) $a[] = $this->_datafields[site_id][1][0]."='".$this->owningSiteObj->getField("id")."'"; */
  718. /* if (!isset($this->owningSectionObj)) $this->owningSectionObj = new section($this->owning_site,$this->owning_section); */
  719. /* if ($all) $a[] = $this->_datafields[section_id][1][0]."='".$this->owningSectionObj->getField("id")."'"; */
  720. $this->fetchUp();
  721. if ($all)
  722. $a[] = $this->_datafields[page_id][1][0]."='".addslashes($this->owningPageObj->getField("id"))."'";
  723. // if ($this->id && ($all || $this->changed[pages])) { //I belive we may always need to fix the order.
  724. if ($this->id) {
  725. $orderkeys = array_keys($this->owningPageObj->getField("stories"),$this->id);
  726. // print "<br />".$this->id."<br />".$orderkeys[0]."<br />";
  727. $a[] = "story_order='".addslashes($orderkeys[0])."'";
  728. } else {
  729. $a[] = "story_order='".addslashes(count($this->owningPageObj->getField("stories")))."'";
  730. }
  731. if ($all || $this->changed[title]) $a[] = $this->_datafields[title][1][0]."='".addslashes($d[title])."'";
  732. if ($all || $this->changed[activatedate]) $a[] = "story_activate_tstamp ='".addslashes(ereg_replace("-","",$d[activatedate]))."'"; // remove dashes to make a tstamp
  733. if ($all || $this->changed[deactivatedate]) $a[] = "story_deactivate_tstamp ='".addslashes(ereg_replace("-","",$d[deactivatedate]))."'"; // remove dashes to make a tstamp
  734. if ($all || $this->changed[active]) $a[] = $this->_datafields[active][1][0]."='".addslashes((($d[active])?1:0))."'";
  735. if ($all || $this->changed[type]) $a[] = $this->_datafields[type][1][0]."='".addslashes($d[type])."'";
  736. if ($all || $this->changed[locked]) $a[] = $this->_datafields[locked][1][0]."='".addslashes((($d[locked])?1:0))."'";
  737. // if ($all || $this->changed[stories]) $a[] = "stories='".encode_array($d[stories])."'";
  738. // if (($all && $this->data[url]) || $this->changed[url]) $a[] = $this->_datafields[url][1][0]."='$d[url]'";
  739. if ($all || $this->changed[discuss]) $a[] = $this->_datafields[discuss][1][0]."='".addslashes((($d[discuss])?1:0))."'";
  740. if ($all || $this->changed[discussemail]) $a[] = $this->_datafields[discussemail][1][0]."='".addslashes((($d[discussemail])?1:0))."'";
  741. if ($all || $this->changed[discussdisplay]) $a[] = $this->_datafields[discussdisplay][1][0]."='".addslashes(($d[discussdisplay]))."'";
  742. if ($all || $this->changed[discussauthor]) $a[] = $this->_datafields[discussauthor][1][0]."='".addslashes(($d[discussauthor]))."'";
  743. if ($all || $this->changed[discusslabel]) $a[] = $this->_datafields[discusslabel][1][0]."='".addslashes($d[discusslabel])."'";
  744. if ($all || $this->changed[texttype]) $a[] = $this->_datafields[texttype][1][0]."='".addslashes($d[texttype])."'";
  745. if ($all || $this->changed[category]) $a[] = $this->_datafields[category][1][0]."='".addslashes($d[category])."'";
  746. if ($all || $this->changed[shorttext]) $a[] = $this->_datafields[shorttext][1][0]."='".addslashes(urlencode($d[shorttext]))."'";
  747. if ($all || $this->changed[longertext]) $a[] = $this->_datafields[longertext][1][0]."='".addslashes(urlencode($d[longertext]))."'";
  748. // if ($all || $this->changed[discussions]) $a[] = "discussions='".encode_array($d[discussions])."'";
  749. return $a;
  750. }
  751. }