PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/objects/section.inc.php

https://github.com/adamfranco/segue-1.x
PHP | 621 lines | 443 code | 91 blank | 87 comment | 101 complexity | c5203b384623b36bc7e7a4de3fa3d34c MD5 | raw file
  1. <? /* $Id$ */
  2. class section extends segue {
  3. var $pages;
  4. var $_allfields = array("site_id","title","editedtimestamp","addedby","editedby","addedtimestamp",
  5. "activatedate","deactivatedate","active","pages","pageorder","type",
  6. "url","locked","hide_sidebar");
  7. // fields listed in $_datafields are stored in the database.
  8. // the first element is the table join syntax required to pull the data.
  9. // the second element is an array of the database fields we will be selecting
  10. // the third element is the database field by which we will sort
  11. var $_datafields = array(
  12. "id" => array(
  13. "section",
  14. array("section_id"),
  15. "section_id"
  16. ),
  17. "site_id" => array(
  18. "section
  19. INNER JOIN
  20. site
  21. ON section.FK_site = site.site_id
  22. INNER JOIN
  23. slot
  24. ON site.site_id = slot.FK_site",
  25. array("slot_name"),
  26. "section_id"
  27. ),
  28. "type" => array(
  29. "section",
  30. array("section_display_type"),
  31. "section_id"
  32. ),
  33. "title" => array(
  34. "section",
  35. array("section_title"),
  36. "section_id"
  37. ),
  38. "activatedate" => array(
  39. "section",
  40. array("DATE_FORMAT(section_activate_tstamp, '%Y-%m-%d')"),
  41. "section_id"
  42. ),
  43. "deactivatedate" => array(
  44. "section",
  45. array("DATE_FORMAT(section_deactivate_tstamp, '%Y-%m-%d')"),
  46. "section_id"
  47. ),
  48. "active" => array(
  49. "section",
  50. array("section_active"),
  51. "section_id"
  52. ),
  53. "pageorder" => array(
  54. "section",
  55. array("section_page_order"),
  56. "section_id"
  57. ),
  58. "url" => array(
  59. "section
  60. LEFT JOIN
  61. media
  62. ON FK_media = media_id",
  63. array("media_tag"),
  64. "section_id"
  65. ),
  66. "locked" => array(
  67. "section",
  68. array("section_locked"),
  69. "section_id"
  70. ),
  71. "editedby" => array(
  72. "section
  73. INNER JOIN
  74. user
  75. ON FK_updatedby = user_id",
  76. array("user_uname"),
  77. "section_id"
  78. ),
  79. "editedtimestamp" => array(
  80. "section",
  81. array("section_updated_tstamp"),
  82. "section_id"
  83. ),
  84. "addedby" => array(
  85. "section
  86. INNER JOIN
  87. user
  88. ON FK_createdby = user_id",
  89. array("user_uname"),
  90. "section_id"
  91. ),
  92. "addedtimestamp" => array(
  93. "section",
  94. array("section_created_tstamp"),
  95. "section_id"
  96. ),
  97. "hide_sidebar" => array(
  98. "section",
  99. array("section_hide_sidebar"),
  100. "section_id"
  101. ),
  102. "pages" => array(
  103. "section
  104. INNER JOIN
  105. page
  106. ON section_id = FK_section",
  107. array("page_id"),
  108. "page_order"
  109. )
  110. );
  111. var $_table = "section";
  112. function section($insite,$id=0,&$siteObj) {
  113. $this->owning_site = $insite;
  114. $this->owningSiteObj = &$siteObj;
  115. $this->fetchedup = 1;
  116. $this->id = $id;
  117. // initialize the data array
  118. $this->data[site_id] = $insite;
  119. $this->init();
  120. $this->data[type] = "section";
  121. $this->data[id] = $id;
  122. }
  123. function delete($deleteFromParent=0) { // delete from db
  124. if (!$this->id) return false;
  125. if ($deleteFromParent) {
  126. $parentObj =& new site ($this->owning_site);
  127. $parentObj->fetchDown();
  128. $parentObj->delSection($this->id);
  129. $parentObj->updateDB();
  130. } else {
  131. // remove pages
  132. $this->fetchDown();
  133. if ($this->pages) {
  134. foreach ($this->pages as $p=>$o) {
  135. $o->delete();
  136. }
  137. }
  138. $query = "DELETE FROM section WHERE section_id='".addslashes($this->id)."'";
  139. db_query($query);
  140. $query = "DELETE FROM permission WHERE FK_scope_id='".addslashes($this->id)."' AND permission_scope_type='section';";
  141. db_query($query);
  142. $this->clearPermissions();
  143. $this->updatePermissionsDB();
  144. }
  145. }
  146. function init($formdates=0) {
  147. $this->pages = array();
  148. if (!is_array($this->data)) $this->data = array();
  149. $this->data[title] = "";
  150. $this->data[activatedate] = $this->data[deactivatedate] = "0000-00-00";
  151. $this->data[active] = 1;
  152. $this->data[hide_sidebar] = 0;
  153. $this->data[url] = "http://";
  154. $this->data[locked] = 0;
  155. if ($this->id) $this->fetchFromDB();
  156. if ($formdates) $this->initFormDates();
  157. }
  158. function fetchUp($full=0) {
  159. if (!$this->fetchedup || $full) {
  160. /* print "<br />Fetching Up<br />"; */
  161. $this->owningSiteObj =& new site($this->owning_site);
  162. $this->owningSiteObj->fetchFromDB(1);
  163. // $this->owningSiteObj->buildPermissionsArray(1);
  164. $this->fetchedup = 1;
  165. }
  166. }
  167. function addPage($id) {
  168. if (!is_array($this->getField("pages"))) $this->data[pages] = array();
  169. array_push($this->data["pages"],$id);
  170. $this->changed[pages] = 1;
  171. }
  172. function delPage($id,$delete=1) {
  173. $d = array();
  174. foreach ($this->getField("pages") as $p)
  175. if ($p != $id) $d[]=$p;
  176. $this->data[pages] = $d;
  177. $this->changed[pages]=1;
  178. if ($delete) {
  179. $page =& new page($this->owning_site,$this->id,$id,$this);
  180. $page->delete();
  181. }
  182. }
  183. function fetchDown($full=0) {
  184. if (!$this->fetcheddown || $full) {
  185. /* print "-->section fetchdown ".$this->id."<br />"; */
  186. if (!$this->tobefetched || $full) $this->fetchFromDB(0,$full);
  187. foreach ($this->getField("pages") as $p) {
  188. $this->pages[$p] =& new page($this->owning_site,$this->id,$p,$this);
  189. $this->pages[$p]->fetchDown($full);
  190. }
  191. $this->fetcheddown = 1;
  192. }
  193. }
  194. function fetchFromDB($id=0,$force=0) {
  195. if ($id) $this->id = $id;
  196. $this->tobefetched=1;
  197. global $dbuser, $dbpass, $dbdb, $dbhost;
  198. global $cfg;
  199. // take this out when appropriate & replace occurences;
  200. global $uploaddir;
  201. $this->tobefetched=1;
  202. //$this->id = $this->getField("id"); // why need to do this?
  203. if ($force) {
  204. // the code below is inefficient! why fetch each field separately when we can fetch all fields at same time
  205. // thus we can cut the number of queries significantly
  206. /* foreach ($this->_allfields as $f) {
  207. $this->getField($f);
  208. }
  209. */
  210. // connect to db and initialize data array
  211. db_connect($dbhost,$dbuser,$dbpass, $dbdb);
  212. $this->data = array();
  213. // first fetch all fields that are not part of a 1-to-many relationship
  214. $query = "
  215. SELECT
  216. section_display_type AS type, section_title AS title, DATE_FORMAT(section_activate_tstamp, '%Y-%m-%d') AS activatedate, DATE_FORMAT(section_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  217. section_active AS active, section_locked AS locked, section_updated_tstamp AS editedtimestamp,
  218. section_created_tstamp AS addedtimestamp, section_hide_sidebar AS hide_sidebar, section_page_order AS pageorder,
  219. user_createdby.user_uname AS addedby, user_updatedby.user_uname AS editedby, slot_name as site_id,
  220. media_tag AS url
  221. FROM
  222. section
  223. INNER JOIN
  224. user AS user_createdby
  225. ON section.FK_createdby = user_createdby.user_id
  226. INNER JOIN
  227. user AS user_updatedby
  228. ON section.FK_updatedby = user_updatedby.user_id
  229. INNER JOIN
  230. site
  231. ON section.FK_site = site.site_id
  232. INNER JOIN
  233. slot
  234. ON site.site_id = slot.FK_site
  235. LEFT JOIN
  236. media
  237. ON FK_media = media_id
  238. WHERE section_id = '".addslashes($this->id)."'
  239. ";
  240. $r = db_query($query);
  241. $a = db_fetch_assoc($r);
  242. array_change_key_case($a); // make all keys lower case
  243. // for each field returned by the query
  244. foreach ($a as $field => $value)
  245. // make sure we have defined this field in the _allfields array
  246. if (in_array($field,$this->_allfields)) {
  247. // decode if necessary
  248. if (in_array($field,$this->_encode))
  249. $value = stripslashes(urldecode($value));
  250. $this->data[$field] = $value;
  251. $this->fetched[$field] = 1;
  252. }
  253. else
  254. echo "ERROR: field $field not in _allfields!!!<br />";
  255. // now fetch the sections (they are part of a 1-to-many relationship and therefore
  256. // we cannot fetch them along with the other fields)
  257. $query = "
  258. SELECT
  259. page_id
  260. FROM
  261. section
  262. INNER JOIN
  263. page
  264. ON section_id = FK_section
  265. WHERE
  266. section_id = '".addslashes($this->id)."'
  267. ORDER BY
  268. page_order
  269. ";
  270. $r = db_query($query);
  271. $this->data[pages] = array();
  272. while ($a = db_fetch_assoc($r))
  273. $this->data[pages][] = $a[page_id];
  274. $this->fetched[pages] = 1;
  275. }
  276. return $this->id;
  277. }
  278. function updateDB($down=0, $force=0, $keepEditHistory = FALSE) {
  279. if (count($this->changed)) {
  280. $a = $this->createSQLArray();
  281. if ($keepEditHistory) {
  282. $a[] = $this->_datafields[editedtimestamp][1][0]."='".addslashes($this->getField("editedtimestamp"))."'";
  283. } else
  284. $a[] = "FK_updatedby='".addslashes($_SESSION[aid])."'";
  285. $query = "UPDATE section SET ".implode(",",$a)." WHERE section_id='".addslashes($this->id)."'";
  286. /* print "<pre>Section->UpdateDB: $query<br />"; */
  287. db_query($query);
  288. /* print mysql_error()."<br />"; */
  289. /* print_r($this->data['pages']); */
  290. /* print "</pre>"; */
  291. // the hard step: update the fields in the JOIN tables
  292. if ($this->changed[url]) {
  293. // Urls are now stored in the media table
  294. // get id of media item
  295. $query = "
  296. SELECT
  297. FK_media
  298. FROM
  299. section
  300. WHERE
  301. section_id = '".addslashes($this->id)."'
  302. ";
  303. $a = db_fetch_assoc(db_query($query));
  304. $media_id = $a[FK_media];
  305. if ($media_id) {
  306. $query = "
  307. UPDATE
  308. media
  309. SET
  310. media_tag = '".addslashes($this->data[url])."',
  311. FK_updatedby = '".addslashes($_SESSION[aid])."'
  312. WHERE
  313. media_id = '".addslashes($media_id)."'
  314. ";
  315. } else {
  316. $query = "
  317. INSERT INTO
  318. media
  319. SET
  320. media_tag = '".addslashes($this->data[url])."',
  321. FK_updatedby = '".addslashes($_SESSION[aid])."'
  322. ";
  323. }
  324. db_query($query);
  325. }
  326. /* // now update all the page ids in the children, if the latter have changed */
  327. /* if ($this->changed[pages]) { */
  328. /* // first, a precautionary step: reset the parent of every section that used to have this site object as the parent */
  329. /* // we do this, because we might have removed a certain section from the array of sections of a site object */
  330. /* $query = "UPDATE page SET FK_section=0 WHERE FK_section=".$this->id; */
  331. /* db_query($query); */
  332. /* */
  333. /* // now, update all pages */
  334. /* foreach ($this->data['pages'] as $k => $v) { */
  335. /* $query = "UPDATE page SET FK_section=".$this->id.", page_order=$k WHERE page_id=".$v; */
  336. /* db_query($query); */
  337. /* } */
  338. /* */
  339. /* } */
  340. }
  341. // update permissions
  342. $this->updatePermissionsDB($force);
  343. // add log entry
  344. /* log_entry("edit_section",$this->owning_site,$this->id,"","$_SESSION[auser] edited section id ".$this->id." in site ".$this->owning_site); */
  345. // update down
  346. if ($down) {
  347. if ($this->fetcheddown && $this->pages) {
  348. foreach (array_keys($this->pages) as $k=>$i) $this->pages[$i]->updateDB($down, $force, $keepEditHistory);
  349. }
  350. }
  351. return true;
  352. }
  353. function insertDB($down=0, $newsite=null, $removeOrigional=0, $keepaddedby=0, $keepDiscussions=0) {
  354. $origsite = $this->owning_site;
  355. $origid = $this->id;
  356. if ($newsite) {
  357. $this->owning_site = $newsite;
  358. unset($this->owningSiteObj);
  359. }
  360. $this->fetchUp(1);
  361. /*********************************************************
  362. * Re-Key the ordering of the rest of the sections in the
  363. * site to make sure that there are no holes in the order
  364. *********************************************************/
  365. foreach($this->owningSiteObj->getField("sections") as $order => $sectionId) {
  366. $query =
  367. "UPDATE
  368. section
  369. SET
  370. section_order = '".addslashes($order)."'
  371. WHERE
  372. section_id = '".$sectionId."'";
  373. // printpre($query);
  374. db_query($query);
  375. }
  376. $a = $this->createSQLArray(1);
  377. if (!$keepaddedby) {
  378. $a[] = "FK_createdby='".addslashes($_SESSION[aid])."'";
  379. $a[] = $this->_datafields[addedtimestamp][1][0]."=NOW()";
  380. $a[] = "FK_updatedby='".addslashes($_SESSION[aid])."'";
  381. } else {
  382. $a[] = "FK_createdby=".db_get_value("user","user_id","user_uname='".addslashes($this->getField("addedby"))."'");
  383. $a[] = $this->_datafields[addedtimestamp][1][0]."='".addslashes($this->getField("addedtimestamp"))."'";
  384. $a[] = "FK_updatedby=".db_get_value("user","user_id","user_uname='".addslashes($this->getField("editedby"))."'");
  385. $a[] = $this->_datafields[editedtimestamp][1][0]."='".addslashes($this->getField("editedtimestamp"))."'";
  386. }
  387. // insert media (url)
  388. if ($this->data[url]) {
  389. // first see, if media item already exists in media table
  390. $query = "
  391. SELECT
  392. media_id
  393. FROM
  394. media
  395. WHERE
  396. FK_site = '".addslashes($this->owningSiteObj->id)."' AND
  397. FK_createdby = '".addslashes($_SESSION[aid])."' AND
  398. media_tag = '".addslashes($this->data[url])."' AND
  399. media_location = 'remote'
  400. ";
  401. $r = db_query($query);
  402. // if not in media table insert it
  403. if (!db_num_rows($r)) {
  404. $query = "
  405. INSERT
  406. INTO media
  407. SET
  408. FK_site = '".addslashes($this->owningSiteObj->id)."',
  409. FK_createdby = '".addslashes($_SESSION[aid])."',
  410. media_tag = '".addslashes($this->data[url])."',
  411. media_location = 'remote',
  412. FK_updatedby = '".addslashes($_SESSION[aid])."'
  413. ";
  414. db_query($query);
  415. $a[] = "FK_media=".lastid();
  416. }
  417. // if in media table, assign the media id
  418. else {
  419. $arr = db_fetch_assoc($r);
  420. $a[] = "FK_media='".addslashes($arr[media_id])."'";
  421. }
  422. }
  423. $query = "INSERT INTO section SET ".implode(",",$a);
  424. db_query($query);
  425. $this->id = lastid();
  426. // See if there is a site hash (meaning that we are being copied).
  427. // If so, try to match our id with the hash entry for 'NEXT'.
  428. if ($GLOBALS['__site_hash']['sections']
  429. && $oldId = array_search('NEXT', $GLOBALS['__site_hash']['sections']))
  430. {
  431. $GLOBALS['__site_hash']['sections'][$oldId] = $this->id;
  432. }
  433. // $this->fetchUp(1);
  434. /* print "<br />remove origionl: $removeOrigional<br />"; */
  435. if ($removeOrigional) $this->owningSiteObj->delSection($origid,0);
  436. /* print "<pre>this->owningsiteobject: "; print_r($this->owningSiteObj); print "</pre>"; */
  437. $this->owningSiteObj->updateDB();
  438. // add new permissions entry.. force update
  439. // $this->updatePermissionsDB(1); // We shouldn't need this because new sections will just
  440. //inherit the permissions of their parent sites
  441. // add log entry
  442. /* log_entry("add_section",$this->owning_site,$this->id,"","$_SESSION[auser] added section id ".$this->id." to site ".$this->owning_site); */
  443. // insert down
  444. if ($down && $this->fetcheddown && $this->pages) {
  445. foreach (array_keys($this->pages) as $k=>$i) {
  446. // Mark our Id as the next one to set
  447. if (is_array($GLOBALS['__site_hash']['pages']))
  448. $GLOBALS['__site_hash']['pages'][$i] = 'NEXT';
  449. $this->pages[$i]->id = 0; // createSQLArray uses this to tell if we are inserting or updating
  450. $this->pages[$i]->insertDB(1, $this->owning_site, $this->id, 1, $keepaddedby, $keepDiscussions);
  451. }
  452. }
  453. return true;
  454. }
  455. function createSQLArray($all=0) {
  456. $d = $this->data;
  457. $a = array();
  458. $this->fetchUp();
  459. if ($all) $a[] = "FK_site='".$this->owningSiteObj->id."'";
  460. /* print "<pre>\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"; */
  461. /* print "owning_site=".$this->owning_site."\nOwningSiteObj: "; */
  462. /* print_r ($this->owningSiteObj); */
  463. /* print "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\n</pre>"; */
  464. /* print "<br />Sections = <pre>"; */
  465. /* print_r($this->owningSiteObj->getField("sections")); */
  466. /* print "</pre>"; */
  467. // if ($this->id && ($all || $this->changed[sections])) { //I belive we may always need to fix the order.
  468. if ($this->id) {
  469. $orderkeys = array_keys($this->owningSiteObj->getField("sections"),$this->id);
  470. $a[] = "section_order=".$orderkeys[0];
  471. } else {
  472. /* print "<br />No id, inserting at end of other sections. Count=".count($this->owningSiteObj->getField("sections")); */
  473. $a[] = "section_order=".count($this->owningSiteObj->getField("sections"));
  474. }
  475. if ($all || $this->changed[title]) $a[] = $this->_datafields[title][1][0]."='".addslashes($d[title])."'";
  476. if ($all || $this->changed[activatedate]) $a[] = "section_activate_tstamp ='".addslashes(ereg_replace("-","",$d[activatedate]))."'"; // remove dashes to make a tstamp
  477. if ($all || $this->changed[deactivatedate]) $a[] = "section_deactivate_tstamp ='".addslashes(ereg_replace("-","",$d[deactivatedate]))."'"; // remove dashes to make a tstamp
  478. if ($all || $this->changed[active]) $a[] = $this->_datafields[active][1][0]."='".addslashes((($d[active])?1:0))."'";
  479. if ($all || $this->changed[type]) $a[] = $this->_datafields[type][1][0]."='".addslashes($d[type])."'";
  480. // if ($all || $this->changed[pages]) $a[] = "pages='".encode_array($this->getField("pages"))."'";
  481. // if ($all || $this->changed[url]) $a[] = $this->_datafields[url][1][0]."='$d[url]'";
  482. if ($all || $this->changed[locked]) $a[] = $this->_datafields[locked][1][0]."='".addslashes((($d[locked])?1:0))."'";
  483. if ($all || $this->changed[hide_sidebar]) $a[] = $this->_datafields[hide_sidebar][1][0]."='".addslashes((($d[hide_sidebar])?1:0))."'";
  484. if ($all || $this->changed[pageorder]) $a[] = $this->_datafields[pageorder][1][0]."='".addslashes($d[pageorder])."'";
  485. return $a;
  486. }
  487. function handlePageOrder() {
  488. // reorders the pages array passed to it depending on the order specified.
  489. // Orders: editeddesc, editedasc, titleasc
  490. $newpages = array();
  491. $order = $this->getField("pageorder");
  492. if ($order == '' || $order=='custom') return;
  493. $this->fetchDown();
  494. // printpre ($order);
  495. foreach ($this->pages as $p=>$o) {
  496. $added = ereg_replace("[: -]","",$o->getField("addedtimestamp"));
  497. $edited = ereg_replace("[: -]","",$o->getField("editedtimestamp"));
  498. /* $added = str_replace("-","",$added); */
  499. /* $added = str_replace(" ","",$added); */
  500. // printpre ($added."-".$o->getField("title"));
  501. if ($order == "addeddesc" || $order == "addedasc") {
  502. $newpages[$p] = $added;
  503. } else if ($order == "editeddesc" || $order == "editedasc") {
  504. $newpages[$p] = $edited;
  505. } else if ($order == "author") {
  506. $newpages[$p] = $o->getField("addedby");
  507. } else if ($order == "editor") {
  508. $newpages[$p] = $o->getField("editedby");
  509. } else if ($order == "category") {
  510. $newpages[$p] = $o->getField("category");
  511. } else if ($order == "titledesc" || $order == "titleasc") {
  512. $newpages[$p] = strtolower($o->getField("title"));
  513. }
  514. }
  515. if ($order == "addeddesc" || $order == "editeddesc") {
  516. arsort($newpages,SORT_NUMERIC);
  517. } else if ($order == "addedasc" || $order == "editedasc") {
  518. asort($newpages,SORT_NUMERIC);
  519. } else if ($order == "titledesc") {
  520. arsort($newpages);
  521. } else {
  522. asort($newpages);
  523. }
  524. foreach ($newpages as $id=>$n) {
  525. $newpages[$id] = $this->pages[$id];
  526. }
  527. $this->pages = $newpages;
  528. $this->setField("pages",array_keys($newpages));
  529. }
  530. }