content.module

  1. 6.x modules/content/content.module
  2. 4.x modules/content/content.module
  3. 5.x modules/content/content.module

File

modules/content/content.module
View source
  1. <?php
  2. // Define constants
  3. define('CONTENT_PRIVATE_FILE', 0); // must be logged in to view
  4. define('CONTENT_PUBLIC_FILE', 1); // publicly (anonymous) can view with URL
  5. /**
  6. * This module lets administrators create content, when then appear can appear in a block.
  7. */
  8. function content_menu() {
  9. $items = array();
  10. $items["admin-tools/content"] = array(
  11. "title" => "Content",
  12. "description" => "Manage content in " . variable_get("system_name", "FlightPath") . ". Ex: Announcements.",
  13. "page_callback" => "content_display_content_admin_list",
  14. "access_arguments" => array("admin_content"),
  15. "page_settings" => array(
  16. "menu_icon" => fp_get_module_path('content') . "/icons/application_edit.png",
  17. "page_show_title" => TRUE,
  18. "menu_links" => array(
  19. 0 => array(
  20. "text" => t("Admin Tools"),
  21. "path" => "admin-tools",
  22. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  23. ),
  24. ),
  25. ),
  26. 'type' => MENU_TYPE_DEFAULT_TAB,
  27. 'tab_family' => 'content_man_tabs',
  28. "weight" => 0,
  29. );
  30. $items["admin-tools/content/files"] = array(
  31. "title" => "Public Files",
  32. "page_callback" => 'fp_render_form',
  33. 'page_arguments' => array('content_public_files_form'),
  34. "access_arguments" => array("admin_public_files"),
  35. "page_settings" => array(
  36. "page_show_title" => TRUE,
  37. "menu_links" => array(
  38. 0 => array(
  39. "text" => t("Admin Tools"),
  40. "path" => "admin-tools",
  41. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  42. ),
  43. ),
  44. ),
  45. 'type' => MENU_TYPE_TAB,
  46. 'tab_family' => 'content_man_tabs',
  47. "weight" => 10,
  48. );
  49. /*
  50. $items["content/delete"] = array(
  51. "page_callback" => "content_handle_delete_content",
  52. "access_arguments" => array("admin_content"),
  53. "type" => MENU_TYPE_CALLBACK,
  54. );
  55. */
  56. // content/add/TYPE
  57. $items["content/add/%"] = array(
  58. "page_callback" => "fp_render_form",
  59. "page_arguments" => array("content_edit_content_form", "", 2, "new"),
  60. "access_callback" => "content_user_access",
  61. "access_arguments" => array("add", 2),
  62. "type" => MENU_TYPE_NORMAL_ITEM,
  63. "page_settings" => array(
  64. "menu_links" => array(
  65. 0 => array(
  66. "text" => t("Admin Tools"),
  67. "path" => "admin-tools",
  68. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  69. ),
  70. 1 => array(
  71. "text" => "Content",
  72. "path" => "admin-tools/content",
  73. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  74. ),
  75. ),
  76. ),
  77. );
  78. // Add content/add/TYPE/ with multiple wildcards for future module developers. Ex:
  79. // in hook_form_alter, using the arg() function to find out what these values are.
  80. $items["content/add/%/%"] = $items["content/add/%"];
  81. $items["content/add/%/%/%"] = $items["content/add/%"];
  82. $items["content/add/%/%/%/%"] = $items["content/add/%"];
  83. $items["content/add/%/%/%/%/%"] = $items["content/add/%"];
  84. $items["content/add/%/%/%/%/%/%"] = $items["content/add/%"];
  85. $items["content/%"] = array(
  86. "title" => "View",
  87. "page_callback" => "content_view_content",
  88. "page_arguments" => array(1),
  89. "access_callback" => "content_user_access",
  90. "access_arguments" => array("view", 1),
  91. "page_settings" => array(
  92. "page_show_title" => TRUE,
  93. "menu_links" => array(
  94. 0 => array(
  95. "text" => t("Admin Tools"),
  96. "path" => "admin-tools",
  97. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  98. ),
  99. 1 => array(
  100. "text" => "Content",
  101. "path" => "admin-tools/content",
  102. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  103. ),
  104. ),
  105. ),
  106. "type" => MENU_TYPE_TAB,
  107. "tab_family" => "content",
  108. "weight" => 10,
  109. );
  110. $items["content/%/edit"] = array(
  111. "title" => "Edit",
  112. "page_callback" => "fp_render_form",
  113. "page_arguments" => array("content_edit_content_form", "", "", 1),
  114. "access_callback" => "content_user_access",
  115. "access_arguments" => array("edit", 1),
  116. "page_settings" => array(
  117. "page_hide_report_error" => TRUE,
  118. "menu_links" => array(
  119. 0 => array(
  120. "text" => t("Admin Tools"),
  121. "path" => "admin-tools",
  122. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  123. ),
  124. 1 => array(
  125. "text" => "Content",
  126. "path" => "admin-tools/content",
  127. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  128. ),
  129. ),
  130. ),
  131. "type" => MENU_TYPE_TAB,
  132. "tab_family" => "content",
  133. "weight" => 20,
  134. );
  135. $items["content/%/devel"] = array(
  136. "title" => "Devel",
  137. "page_callback" => "content_display_devel",
  138. "page_arguments" => array(1),
  139. "access_arguments" => array("view_fpm_debug"),
  140. "page_settings" => array(
  141. "page_hide_report_error" => TRUE,
  142. "menu_links" => array(
  143. 0 => array(
  144. "text" => t("Admin Tools"),
  145. "path" => "admin-tools",
  146. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  147. ),
  148. 1 => array(
  149. "text" => "Content",
  150. "path" => "admin-tools/content",
  151. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  152. ),
  153. ),
  154. ),
  155. "type" => MENU_TYPE_TAB,
  156. "tab_family" => "content",
  157. "weight" => 30,
  158. );
  159. $items['content/%/remove'] = array(
  160. "page_callback" => "fp_render_form",
  161. "page_arguments" => array("content_unpublish_content_form", "", 1),
  162. "access_callback" => "content_user_access",
  163. "access_arguments" => array("edit", 1), // since we are editing the published field, we simply check for "edit"
  164. );
  165. $items["content-files/handle-download/%"] = array(
  166. "page_callback" => "content_files_handle_download",
  167. "page_arguments" => array(2),
  168. "access_callback" => "content_files_user_may_download_file",
  169. "access_arguments" => array(2),
  170. "type" => MENU_TYPE_CALLBACK,
  171. );
  172. /**
  173. * Generic screen which, if we have saved content from within an iframe dialog,
  174. * we want to display a "success" message, then just refresh the screen.
  175. */
  176. $items["content-dialog-handle-after-save"] = array(
  177. "page_callback" => "content_dialog_handle_after_save",
  178. "access_callback" => TRUE,
  179. "page_settings" => array (
  180. "display_currently_advising" => FALSE,
  181. "screen_mode" => "not_advising",
  182. "page_is_popup" => TRUE,
  183. ),
  184. "type" => MENU_TYPE_CALLBACK,
  185. );
  186. return $items;
  187. }
  188. /**
  189. * Returns TRUE or FALSE if the user has access to download this particular student's file.
  190. */
  191. function content_files_user_may_download_file($fid) {
  192. global $user;
  193. $file = content_get_uploaded_file($fid);
  194. if ($user->id == 1) return TRUE; // this is the admin user.
  195. // Get cid of original piece of content this file belonged to.
  196. $cid = intval($file['cid']);
  197. if ($cid === 0) return TRUE; // no CID attached, so we can assume they can view it.
  198. // Otherwise, see if the user is allowed to "view" this piece of content. If so, that means they
  199. // can view any files attached as well.
  200. return content_user_access('view', $cid);
  201. }
  202. /**
  203. * This actually finds and downloads the file for the user, decrypting if necessary.
  204. */
  205. function content_files_handle_download($fid) {
  206. $file = content_get_uploaded_file($fid);
  207. if (!$file) {
  208. display_not_found();
  209. die;
  210. }
  211. // Otherwise, now we proceed.
  212. $file_contents = @file_get_contents($file["full_filename"]);
  213. if ($file_contents && intval($file["is_encrypted"]) === 1 && function_exists("encryption_decrypt")) {
  214. $file_contents = encryption_decrypt($file_contents);
  215. }
  216. // Okay, now let's spit it out to the browser for download.
  217. header('Content-type: ' . $file["filetype"]);
  218. header('Content-Disposition: attachment; filename="' . $file["original_filename"] . '"');
  219. print $file_contents;
  220. die;
  221. }
  222. /**
  223. * This screen lets the user upload/manage/delete "public files" stored at custom/files/content_uploads/public_uploads/
  224. */
  225. function content_public_files_form() {
  226. fp_add_css(fp_get_module_path('content') . '/css/content.css');
  227. $form = array();
  228. fp_set_title('');
  229. // Did we receive a request to delete a file?
  230. if (isset($_GET['del']) && is_numeric($_GET['del'])) {
  231. $del_fid = intval($_GET['del']);
  232. $del_file = content_get_uploaded_file($del_fid);
  233. // Make sure the file is a "public" file.
  234. if (intval($del_file['attributes']) === CONTENT_PUBLIC_FILE) {
  235. // Double-check the user has access to delete
  236. if (user_has_permission('admin_public_files')) {
  237. // Okay, unlink the file.
  238. $temp = unlink(fp_get_files_path() . '/content_uploads/public_uploads/' . $del_file['filename']);
  239. if ($temp == FALSE) {
  240. fp_add_message(t("Unable to delete file. Possible server permissions issue, or file not found."), 'error');
  241. }
  242. else {
  243. // We did delete from file system, so delete from content_files table as well.
  244. db_query("DELETE FROM content_files WHERE fid = ?", array($del_fid));
  245. fp_add_message(t("File deleted successfully."));
  246. }
  247. }
  248. } // if public file
  249. // Use fp_goto() to avoid issue with user hitting "refresh"
  250. fp_goto("admin-tools/content/files");
  251. } // trying to delete file
  252. $form["#attributes"] = array("enctype" => 'multipart/form-data'); // allow the form itself to submit files.
  253. $form['mark_top'] = array(
  254. 'value' => '<p>' . t("This form allows you to upload files which are publicly accessible (without needing to log in) through the web.
  255. For example, you may wish to upload image files, css, etc, for use in other settings.
  256. <br><br>
  257. <strong>Note:</strong> These files will not be encrypted, and will be publicly available. Do not upload sensitive
  258. information, like student records or any personally-identifying details.") . "</p><hr>",
  259. );
  260. $form['upload_file'] = array(
  261. 'type' => 'file',
  262. 'label' => t("Upload file:"),
  263. 'description' => t("Allowed file extensions: %ext", array('%ext' => strtolower(variable_get("public_files_allowed_extensions", "css, txt, pdf, doc, docx, xls, xlsx, ppt, pptx, rtf, odt, jpg, jpeg, png, gif, zip, 7z")))),
  264. );
  265. $overwrite_val = FALSE;
  266. if (isset($_SESSION['public_file_form_overwrite_val'])) {
  267. $overwrite_val = (bool) $_SESSION['public_file_form_overwrite_val'];
  268. }
  269. $form['overwrite'] = array(
  270. 'type' => 'checkbox',
  271. 'label' => t('Keep original filename and overwrite existing file by the same name?'),
  272. 'value' => $overwrite_val,
  273. 'prefix' => t("<strong>Important:</strong> By default, files are given randomized names when uploaded, so as not to overwrite an existing file with the same name.
  274. <br>If you would prefer to overwrite files with the same name, check the box below:"),
  275. );
  276. $form['upload_btn'] = array(
  277. 'type' => 'submit',
  278. 'value' => t("Upload"),
  279. );
  280. $form['mark_below_upload'] = array(
  281. 'value' => "<hr>",
  282. );
  283. ///////////////////////////////////////////////////////////
  284. $html = "";
  285. $table_headers = array();
  286. $table_headers[] = array("label" => "Actions");
  287. $table_headers[] = array("label" => "FID", "field" => "fid");
  288. $table_headers[] = array("label" => "Filename", "field" => "original_filename");
  289. $table_headers[] = array("label" => "Links");
  290. $table_headers[] = array("label" => "Uploaded", "field" => "posted");
  291. // Set our initial sort, if none is already set.
  292. theme_table_header_sortable_set_initial_sort("posted", 'DESC');
  293. $html .= "<table class='public-files-table' cellspacing='0' cellpadding='4'>";
  294. // Draw our our table headers, with links....
  295. $html .= theme_table_header_sortable($table_headers);
  296. // Get our order by clause based on selected table header, if any.
  297. $order_by = theme_table_header_sortable_order_by($table_headers);
  298. $res = pager_query("SELECT * FROM content_files WHERE `attributes` = ?
  299. $order_by", array(CONTENT_PUBLIC_FILE), 10, 0);
  300. while ($cur = db_fetch_object($res)) {
  301. $pol = (@$pol == "even") ? "odd" : "even";
  302. $posted = format_date(convert_time($cur->posted), "short");
  303. $abs = base_url() . "/custom/files/content_uploads/public_uploads/$cur->filename";
  304. $rel = base_path() . "/custom/files/content_uploads/public_uploads/$cur->filename";
  305. $absolute_link = "<a href='$abs' title='Absolute link' class='abs-link' target='_blank'><i class='fa fa-globe'></i></a>";
  306. $relative_link = "<a href='$rel' title='Relative link' class='rel-link' target='_blank'><i class='fa fa-link'></i></a>";
  307. $del_link = fp_get_js_confirm_link("Are you sure you want to delete this file?\\n\\nThis action cannot be undone.", "window.location=\"" . fp_url("admin-tools/content/files", "del=$cur->fid") . "\";", "<i class='fa fa-remove' title='" . t("Delete?") . "'></i>", "delete-link");
  308. $html .= "<tr class='row-$pol'>
  309. <td valign='top'>$del_link</td>
  310. <td valign='top'>$cur->fid</td>
  311. <td valign='top'>$cur->original_filename</td>
  312. <td valign='top'>$relative_link &nbsp; $absolute_link</td>
  313. <td valign='top'>$posted</td>
  314. </tr>
  315. ";
  316. } // while
  317. $html .= "</table>";
  318. $html .= theme_pager();
  319. $form['mark_files_table'] = array(
  320. 'value' => $html,
  321. );
  322. return $form;
  323. } // content_public_files_form
  324. function content_public_files_form_validate($form, $form_state) {
  325. if (isset($form_state['values']['upload_btn']) && $form_state['values']['upload_btn'] != "") {
  326. // The user is trying to upload a file.
  327. // Make sure there is something there.
  328. if (!isset($_FILES['upload_file']) || trim($_FILES['upload_file']['name'][0]) == '') {
  329. form_error('upload_file', t('Please select a file from your computer to upload.'));
  330. return;
  331. }
  332. // Make sure it's the correct extension.
  333. $original_filename = $_FILES['upload_file']['name'][0];
  334. // Figure out the extension of the original filename.
  335. $temp = explode(".", $original_filename);
  336. $ext = $temp[count($temp) - 1];
  337. // Make sure that this extension is allowed.
  338. $allowed_extensions = csv_to_array(strtolower(variable_get("public_files_allowed_extensions", "css, txt, pdf, doc, docx, xls, xlsx, ppt, pptx, rtf, odt, jpg, jpeg, png, gif, zip, 7z")));
  339. if (!in_array(strtolower($ext), $allowed_extensions)) {
  340. // Meaning, this extension is not allowed!
  341. form_error('upload_file', t("Sorry, the file's type/extension (%ext) is not allowed. Please rename or select another file, then try again.", array("%ext" => $ext)));
  342. return;
  343. }
  344. // If we made it here, then there's nothing wrong and we can continue to the _submit function.
  345. } // upload file
  346. } // ... validate
  347. function content_public_files_form_submit($form, $form_state) {
  348. if (isset($form_state['values']['upload_btn']) && $form_state['values']['upload_btn'] != "") {
  349. // The user is trying to upload a file.
  350. $file = $_FILES["upload_file"];
  351. $temp = fp_re_array_files($file);
  352. $file = $temp[0];
  353. $_SESSION['public_file_form_overwrite_val'] = $form_state['values']['overwrite'];
  354. content_add_new_uploaded_file($file, 0, !$form_state['values']['overwrite'], CONTENT_PUBLIC_FILE, TRUE);
  355. fp_add_message("File uploaded successfully.");
  356. }
  357. } // ... submit
  358. function content_unpublish_content_form($cid) {
  359. $content = content_load($cid);
  360. fp_add_css(fp_get_module_path("content") . "/css/content.css");
  361. // Display a confirmation page
  362. $types = content_get_types();
  363. $type = $content->type;
  364. // We want to redirect correctly based on this type's options....
  365. // Are there any special settings for the title?
  366. if (isset($types[$type]['settings']) && isset($types[$type]['settings']['title'])) {
  367. if (is_array($types[$type]['settings']['#redirect'])) {
  368. $form['#redirect'] = $types[$type]['settings']['#redirect'];
  369. }
  370. }
  371. $form['cid'] = array(
  372. 'type' => 'hidden',
  373. 'value' => $cid,
  374. );
  375. $form['mark_top'] = array(
  376. 'type' => 'markup',
  377. 'value' => "<h2>" . t("Are you sure you wish to remove <em>$content->title</em>?") . "</h2>
  378. <p>" . t("This action will cause the content to not appear in reports, and not be easily accessible. It can only be restored by an administrator.") . "</p>
  379. <hr>",
  380. );
  381. $form['submit_confirm'] = array(
  382. 'type' => 'submit',
  383. 'value' => 'Confirm & Remove',
  384. 'spinner' => TRUE,
  385. 'attributes' => array('class' => 'content-submit-btn'),
  386. );
  387. $form['submit_cancel'] = array(
  388. 'type' => 'submit',
  389. 'value' => 'Cancel',
  390. 'attributes' => array('class' => 'content-delete-btn'),
  391. );
  392. return $form;
  393. }
  394. function content_unpublish_content_form_submit(&$form, &$form_state) {
  395. $values = $form_state['values'];
  396. if (trim($values['submit_cancel']) != '') {
  397. // We want to CANCEL this operation!
  398. // find out if we should redirect anywhere in particular for this cancelation
  399. if (isset($form['#redirect'])) {
  400. fp_add_message(t("The operation was cancelled."));
  401. $form['#redirect']['query'] .= "&cancel=yes";
  402. }
  403. else {
  404. // No redirect was specified, so let's just return.
  405. fp_add_message(t("The operation was cancelled."));
  406. }
  407. return;
  408. }
  409. $cid = $form_state['values']['cid'];
  410. $content = content_load($cid);
  411. $content->published = 0;
  412. content_save($content);
  413. watchdog('content', "Content with cid $cid was removed/unpublished.");
  414. fp_add_message("Content has been removed.");
  415. }
  416. /**
  417. * Simply returns the fpm() results for the supplied content.
  418. */
  419. function content_display_devel($cid) {
  420. $rtn = "";
  421. $content = content_load($cid);
  422. $rtn .= "&nbsp; &nbsp;";
  423. fpm($content);
  424. return $rtn;
  425. }
  426. /**
  427. * Returns the timestamp of last access, or ZERO if never accessed
  428. */
  429. function content_get_last_access($cid, $account = null) {
  430. global $user;
  431. if ($account == NULL) $account = $user;
  432. $res = db_result(db_query("SELECT last_access FROM content_last_access WHERE cid = ? AND user_id = ?", array($cid, $account->id)));
  433. return intval($res);
  434. }
  435. /**
  436. * Sets the content_last_access timestamp for this user and content. Should be called
  437. * whenever a "content" node is viewed by the user.
  438. */
  439. function content_set_last_access($cid, $account = NULL) {
  440. global $user;
  441. if ($account == NULL) $account = $user;
  442. db_query("DELETE FROM content_last_access WHERE cid = ? AND user_id = ?", array($cid, $account->id));
  443. db_query("INSERT INTO content_last_access(cid, user_id, last_access)
  444. VALUES (?, ?, ?)", array($cid, $account->id, time()));
  445. // We need to re-calculate our alerts count just in case now.
  446. fp_recalculate_alert_count_by_type($account);
  447. }
  448. /**
  449. * Custom user access function to determine if the user can add, edit, etc, the content
  450. *
  451. * $op can equal: "add" (adding a new piece of content) $var will be the content_type. Ex: "engagement"
  452. * "view" (attempting to view the content as a full page)
  453. * "edit" (trying to edit an existing piece of content
  454. * "delete" (trying to delete the content)
  455. *
  456. *
  457. * @param unknown_type $op
  458. * @param unknown_type $var
  459. */
  460. function content_user_access($op, $var) {
  461. global $user;
  462. if ($user->id == 1) return TRUE; // the admin user.
  463. $cid = intval($var);
  464. $content = NULL;
  465. if ($op != "add") {
  466. $content = content_load($cid);
  467. }
  468. // Simply trying to view the content. Do we have permission to view published content of this type?
  469. if ($op == 'view' && user_has_permission("view_" . $content->type . "_content")) {
  470. if (intval($content->published) === 1) {
  471. // If the user is a student, we must take special considerations into account, like if
  472. // the visibility is set to faculty/staff only, and if the content is not ABOUT the student.
  473. if ($user->is_student == TRUE) {
  474. // Is this a faculty only file, and the user is a student?
  475. if (isset($content->field__visibility) && $content->field__visibility['value'] == "faculty") {
  476. return FALSE; // nope, can't view it.
  477. }
  478. // Is the user a student, and is the content ABOUT this student? If not, then they cannot view it. In other words,
  479. // a student can't see another student's engagements, etc.
  480. if (isset($content->field__student_id) && $content->field__student_id['value'] != $user->cwid) {
  481. return FALSE; // nope, can't view it.
  482. }
  483. }
  484. return TRUE;
  485. }
  486. }
  487. // Trying to add new content of the type specified in $var. Do we have permission?
  488. if ($op == 'add' && user_has_permission("add_" . $var . "_content")) {
  489. return TRUE;
  490. }
  491. // Trying to edit or delete this content.
  492. if ($op == "edit" || $op == "delete") {
  493. // Does the user have permission to edit/delete ANY of this type?
  494. if (user_has_permission($op . "_any_" . $content->type . "_content")) {
  495. return TRUE;
  496. }
  497. // Otherwise, does the content belong the the user, and do they have permission to edit/delete their OWN content?
  498. if ($content->user_id == $user->id && user_has_permission($op . "_own_" . $content->type . "_content")) {
  499. return TRUE;
  500. }
  501. } // op == edit or delete
  502. // Failed all of the previous tests, so return false.
  503. return FALSE;
  504. }
  505. /**
  506. * This is an implementation of hook_menu_handle_replacement_pattern.
  507. * It will search for and replace replacement patterns which we are aware of it in $str.
  508. */
  509. function content_menu_handle_replacement_pattern($str) {
  510. $cid = @$_REQUEST["cid"];
  511. if ($cid) {
  512. $content = content_load($cid);
  513. if (is_array($content)) {
  514. if (strpos($str, "%CONTENT_CID%") !== 0) {
  515. // It contains this replacement pattern!
  516. $str = str_replace("%CONTENT_CID%", $cid, $str);
  517. }
  518. if (strpos($str, "%CONTENT_TYPE%") !== 0) {
  519. // It contains this replacement pattern!
  520. $str = str_replace("%CONTENT_TYPE%", $content["type"], $str);
  521. }
  522. }
  523. }
  524. return $str;
  525. }
  526. /**
  527. * This is the URL we redirect to after saving a piece of content in a dialog,
  528. * so all we really want to do is close the dialog and reload the parent page.
  529. *
  530. * Remember, we are inside an iframe.
  531. */
  532. function content_dialog_handle_after_save() {
  533. $rtn = "";
  534. $rtn = "";
  535. $timeout = 1000;
  536. if (isset($_REQUEST['del']) && $_REQUEST['del'] != '') {
  537. $rtn .= "<h2 class='dialog-handle-after-save'>" . t("The content has been deleted successfully.<br><br>Closing window...") . "</h2>";
  538. }
  539. else if (isset($_REQUEST['cancel']) && $_REQUEST['cancel'] != '') {
  540. $rtn .= "<h2 class='dialog-handle-after-save'>" . t("Cancellation successful.<br><br>Closing window...") . "</h2>";
  541. }
  542. else {
  543. $rtn .= "<h2 class='dialog-handle-after-save'>" . t("Your submission has been saved correctly!<br><br>Closing window...") . "</h2>";
  544. }
  545. $rtn .= "<br><br><p>" . t("If this windows doesn't close within 3 seconds,") . " <a href='javascript:window.parent.location.reload();'>" . t("click here") . "</a>.</p>";
  546. // Add this javascript code to when the page loads.
  547. ///* DEV: comment out while testing
  548. $rtn .= "
  549. <script type='text/javascript'>
  550. $(document).ready(function() {
  551. setTimeout(function() {
  552. window.parent.location.reload();
  553. }, $timeout);
  554. });
  555. </script>
  556. ";
  557. //*/
  558. return $rtn;
  559. }
  560. /**
  561. * Display the content specified in the GET's cid.
  562. *
  563. */
  564. function content_view_content($cid) {
  565. $render = array();
  566. $render["#id"] = "content_" . $cid; // We will be doing this as a render array, so other modules can alter.
  567. $render['#cid'] = intval($cid);
  568. fp_add_css(fp_get_module_path('content') . '/css/content.css');
  569. if ($_GET['content_tabs'] == 'false') { // the string "false", not FALSE
  570. fp_add_body_class('content-tabs-false');
  571. }
  572. $content = content_load($cid); // assumed to be path/content/CID So, arg(1) == the cid
  573. if (!$content) {
  574. // Couldn't find it! Show page not found.
  575. display_not_found();
  576. return;
  577. }
  578. $render['#content_object'] = $content;
  579. $type = $content->type;
  580. $types = content_get_types();
  581. $author = fp_load_user($content->user_id);
  582. $render['#content-type'] = $type;
  583. $render['#class'] = "content-type-" . $type;
  584. $weight = 0;
  585. if (!strstr($content->title, "[hide]")) {
  586. fp_set_title($content->title);
  587. }
  588. fp_add_body_class("content-type-" . $content->type);
  589. //$rtn .= "<div class='content-view content-view-{$content->type}'>";
  590. $render['content_title'] = array(
  591. 'type' => 'markup',
  592. 'value' => "<div class='content-title'>" . $content->title . "</div>",
  593. 'weight' => $weight,
  594. );
  595. $render['content_submitted'] = array(
  596. 'type' => 'markup',
  597. 'value' => "<div class='content-submitted'>" . t("Submitted by") . " " . $author->name . " " . t("on") . " " . format_date(convert_time($content->updated), 'short') . "</div>",
  598. 'weight' => $weight += 10,
  599. );
  600. if ($content->delete_flag == 1) {
  601. $render['content_deleted'] = array(
  602. 'type' => 'markup',
  603. 'value' => "<div class='content-deleted'>" . t("NOTE: This content has been marked as deleted, and is scheduled for permanent removal.") . "</div>",
  604. 'weight' => $weight += 10,
  605. );
  606. }
  607. // Display all the various fields
  608. if (is_array($types[$type]['fields'])) {
  609. foreach($types[$type]["fields"] as $field_name => $field_details) {
  610. $display_value = $content->{"field__$field_name"}['display_value'];
  611. if ($field_details['type'] != 'hidden' && $field_details['type'] != 'cfieldset') {
  612. $render['field__' . $field_name] = array(
  613. 'type' => 'markup',
  614. 'label' => $field_details['label'],
  615. 'value' => "<div class='field-value'>$display_value</div>",
  616. 'weight' => $weight += 10,
  617. );
  618. }
  619. if ($field_details['type'] == 'cfieldset') {
  620. // TODO: make this appear within a fieldset?
  621. foreach ($field_details['elements'] as $c => $v) {
  622. foreach ($field_details['elements'][$c] as $efield_name => $edetails) {
  623. $display_value = $content->{"field__$efield_name"}['display_value'];
  624. if ($edetails['type'] != 'hidden' && $edetails['type'] != 'cfieldset') {
  625. $render['field__' . $efield_name] = array(
  626. 'type' => 'markup',
  627. 'label' => $edetails['label'],
  628. 'value' => "<div class='field-value'>$display_value</div>",
  629. 'weight' => $weight += 10,
  630. );
  631. }
  632. }
  633. }
  634. } // its a fieldset
  635. }
  636. }
  637. content_set_last_access($cid);
  638. if (isset($_GET['content_crumbs'])) {
  639. if ($_GET['content_crumbs'] == 'alerts') {
  640. $crumbs = array();
  641. $crumbs[] = array(
  642. 'text' => 'Alerts',
  643. 'path' => 'alerts',
  644. );
  645. fp_set_breadcrumbs($crumbs);
  646. }
  647. }
  648. watchdog("content", "view cid:$cid, type:$type", array());
  649. return fp_render_content($render);
  650. }
  651. /**
  652. * Meant to be run by admin, this generates table creation sql for a given type.
  653. */
  654. function _content_generate_create_table_sql($type) {
  655. $rtn = "";
  656. $keys = "";
  657. $types = content_get_types();
  658. fpm($types);
  659. $rtn .= "CREATE TABLE `content__$type` (
  660. `cid` int unsigned NOT NULL,
  661. `vid` int NOT NULL, \n";
  662. foreach ($types[$type]['fields'] as $name => $element) {
  663. if ($element['type'] == '' || strstr($element['type'], 'markup')) continue;
  664. fpm($name);
  665. $rtn .= " `field__$name` varchar(255) DEFAULT NULL, \n";
  666. $keys .= " KEY `field__$name` (`field__$name`), \n";
  667. } //foreach
  668. $keys = "PRIMARY KEY (`vid`),
  669. KEY `cid` (`cid`),
  670. $keys";
  671. $rtn .= rtrim($keys, ", \n") . "\n);";
  672. fpm(ppm($rtn, TRUE));
  673. } // _content_generate_create_table_sql
  674. /**
  675. * This form lets the user edit some piece of content
  676. *
  677. */
  678. function content_edit_content_form($type = "", $cid = "") {
  679. global $user;
  680. $form = array();
  681. $m = 0;
  682. if (@$_GET['content_tabs'] === 'false') { // the string "false", not FALSE
  683. fp_add_body_class('content-tabs-false');
  684. }
  685. fp_add_css(fp_get_module_path("content") . "/css/content.css");
  686. fp_add_js(fp_get_module_path("content") . "/js/content.js");
  687. if ($type == "") {
  688. @$type = strip_tags($_REQUEST["type"]); // strip_tags to make it safer.
  689. }
  690. if ($cid == "") {
  691. @$cid = strip_tags($_REQUEST["cid"]);
  692. }
  693. $types = content_get_types();
  694. if ($cid != "new") {
  695. $content = content_load($cid);
  696. if (!$content) {
  697. display_not_found();
  698. return;
  699. }
  700. fp_set_title(t("Editing") . " " . $content->title . "");
  701. // Re-set $type, just in case the user put the wrong type in the
  702. // URL
  703. $type = $content->type;
  704. }
  705. else {
  706. // New piece of content
  707. $content = new stdClass();
  708. $content->published = 1;
  709. $content->title = "";
  710. $content->delete_flag = 0;
  711. fp_set_title(t("Add new") . " " . $types[$type]["title"]);
  712. }
  713. fp_add_body_class('content-edit-type--' . $type);
  714. // For simplicity, and because it causes no harm except a small amount of extra bandwidth, we will
  715. // state that all "content" forms are multipart (so they can handle file uploads) even if there
  716. // are no files being uploaded.
  717. $form["#attributes"] = array("enctype" => 'multipart/form-data');
  718. $form["type"] = array(
  719. "type" => "hidden",
  720. "value" => $type,
  721. );
  722. $form["cid"] = array(
  723. "type" => "hidden",
  724. "value" => $cid,
  725. );
  726. $form["title"] = array(
  727. "type" => "textfield",
  728. "label" => t("Title:"),
  729. "value" => $content->title,
  730. "required" => TRUE,
  731. );
  732. // Are there any special settings for the title?
  733. if (isset($types[$type]['settings']) && isset($types[$type]['settings']['title'])) {
  734. if (is_array($types[$type]['settings']['title'])) {
  735. // Use array_merge to just override with whatever we have in our settings for the title.
  736. $form['title'] = array_merge($form['title'], $types[$type]['settings']['title']);
  737. if (isset($form['title']['auto']) && $form['title']['auto'] == TRUE) {
  738. // We will set the form to an automatic title, and hide.
  739. $form['title']['value'] = $type . ' content saved on ' . format_date(convert_time(time()));
  740. $form['title']['type'] = 'hidden';
  741. }
  742. }
  743. }
  744. if (isset($types[$type]['settings']['#redirect'])) {
  745. $form['#redirect'] = $types[$type]['settings']['#redirect'];
  746. }
  747. if (isset($types[$type]['settings']['#validate_handlers'])) {
  748. $form['#validate_handlers'] = $types[$type]['settings']['#validate_handlers'];
  749. }
  750. if (isset($types[$type]['settings']['#submit_handlers'])) {
  751. $form['#submit_handlers'] = $types[$type]['settings']['#submit_handlers'];
  752. }
  753. // If a #redirect has not been set, then we'll go back to this content's "view"...
  754. if (!isset($form['#redirect'])) {
  755. $form['#redirect'] = array('path' => 'content/' . $cid);
  756. }
  757. // Any js or css to add for this form, defined in the settings?
  758. if (isset($types[$type]['settings']['js'])) {
  759. fp_add_js($types[$type]['settings']['js']);
  760. }
  761. if (isset($types[$type]['settings']['css'])) {
  762. fp_add_css($types[$type]['settings']['css']);
  763. }
  764. if ($content->delete_flag == 1) {
  765. $form['mark_content_deleted'] = array(
  766. 'type' => 'markup',
  767. 'value' => "<div class='content-deleted'>" . t("NOTE: This content has been marked as deleted, and is scheduled for permanent removal.") . "</div>",
  768. 'weight' => -999,
  769. );
  770. }
  771. // Are there extra fields? If so, add their values in as well.
  772. if (is_array($types[$type]['fields'])) {
  773. foreach($types[$type]["fields"] as $field_name => $field_details) {
  774. $form[$field_name] = $field_details;
  775. if ($field_details['type'] != 'cfieldset') {
  776. $value = NULL;
  777. if (isset($content->{"field__$field_name"}['value'])) {
  778. $value = $content->{"field__$field_name"}['value'];
  779. }
  780. if ($cid == 'new') {
  781. // value won't be anything. Use value attr from the form as the initial value
  782. $value = @$form[$field_name]['value'];
  783. }
  784. $form[$field_name]['value'] = $value;
  785. // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly. (it was stored as UTC in database)
  786. if ($form[$field_name]['type'] == 'datetime-local') {
  787. if (trim($value) != '') {
  788. $form[$field_name]["value"] = date('Y-m-d\TH:i', convert_time(strtotime($value)));
  789. }
  790. }
  791. // Similar to datetime-local, if this is a "time" field, then it has been stored as UTC in the database, and now
  792. // needs to be converted to local timezone.
  793. if ($form[$field_name]['type'] == 'time') {
  794. if (trim($value) != '') {
  795. $form[$field_name]["value"] = date('H:i', convert_time(strtotime($value)));
  796. }
  797. }
  798. // If this is a file, we want to have a way to REMOVE the file, so we can upload a different one, or simply to
  799. // have no file there at all.
  800. if ($form[$field_name]['type'] == 'file') {
  801. $limit = intval(@$form[$field_name]['limit']);
  802. $allowed = trim(@$form[$field_name]['allowed']);
  803. if (!$allowed) {
  804. $allowed = "txt, pdf, doc, docx, xls, xlsx, ppt, pptx, rtf, odt, jpg, jpeg, png, gif, zip, 7z"; // default safe files
  805. }
  806. $form['file_allowed_ext_' . $field_name] = array(
  807. 'type' => 'hidden',
  808. 'value' => $allowed,
  809. );
  810. $file_count = 0;
  811. if ($limit == 0) $limit = 1;
  812. $form['file_limit_' . $field_name] = array(
  813. 'type' => 'hidden',
  814. 'value' => $limit - 1,
  815. );
  816. // Do we already have a list of files we've attached?
  817. $html = "";
  818. $ofids = $value;
  819. $fids = explode(",", $value);
  820. $bool_is_empty = TRUE;
  821. foreach ($fids as $fid) {
  822. if ($fid == '' || intval($fid) == 0) continue;
  823. $fid = intval($fid);
  824. $file = content_get_uploaded_file($fid);
  825. if ($file) {
  826. $bool_is_empty = FALSE;
  827. $file_count++;
  828. // create a markup field, including a delete option, with the filename listed.
  829. $url = $file['url'];
  830. $delete_link = "<a href='javascript:contentDeleteFile(\"$field_name\",\"$fid\");'>Delete</a>";
  831. $html .= "<div class='file-field-line-$fid'><a href='$url' target='_blank'>" . $file['original_filename'] . "</a> - $delete_link</div>";
  832. } // we already had a file.
  833. } // foreach
  834. if (!$bool_is_empty) {
  835. $html = "<label>" . t("(Existing)") . " " . $form[$field_name]['label'] . "</label>" . $html;
  836. }
  837. $form["markup_$field_name"] = array(
  838. 'type' => 'markup',
  839. 'value' => $html,
  840. 'weight' => @intval($form[$field_name]['weight']) - 1,
  841. );
  842. // Add a hidden field to note that we have already uploaded this file.
  843. $form["previous_upload_$field_name"] = array(
  844. 'type' => 'hidden',
  845. 'value' => $ofids,
  846. 'weight' => @$form[$field_name]['weight'],
  847. );
  848. $form["file_count_" . $field_name] = array(
  849. 'type' => 'hidden',
  850. 'value' => $file_count,
  851. );
  852. if ($file_count >= $limit) {
  853. // hide the original field using CSS.
  854. $form[$field_name]['attributes'] = array("class" => 'hidden');
  855. }
  856. if ($allowed) {
  857. if (!isset($form[$field_name]['description'])) $form[$field_name]['description'] = '';
  858. $form[$field_name]['description'] .= "<div class='content-allowed-ext'><strong>" . t("Allowed file extensions:") . "</strong> " . $allowed . "</div>";
  859. }
  860. if ($limit > 1 && $file_count <= $limit) {
  861. $add_link = "<a href='javascript:contentAddMoreFile(\"$field_name\");' class='add-more-link add-more-link-$field_name'><i class='fa fa-plus'></i> " . t("Add More") . "</a>";
  862. if (!isset($form[$field_name]['suffix'])) $form[$field_name]['suffix'] = '';
  863. $form[$field_name]['suffix'] .= $add_link;
  864. }
  865. } // type == file
  866. } // is not cfieldset
  867. else {
  868. // If this is a fieldset, then we need to assign values to its ELEMENTS. If not empty, then OPEN the cfieldset!
  869. foreach ($form[$field_name]['elements'] as $c => $efields) {
  870. foreach ($efields as $efield_name => $efield_details) {
  871. $value = $content->{"field__$efield_name"}['value'];
  872. if ($cid == 'new') {
  873. // value won't be anything. Use value attr from the form as the initial value
  874. $value = @$efield_details['value'];
  875. }
  876. $efield_details['value'] = $value;
  877. // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly.
  878. if ($efield_details['type'] == 'datetime-local') {
  879. if (trim($value) != '') {
  880. $value = date('Y-m-d\TH:i', convert_time(strtotime($value)));
  881. }
  882. }
  883. // Similar to datetime-local, if this is a "time" field, then it has been stored as UTC in the database, and now
  884. // needs to be converted to local timezone.
  885. if ($efield_details['type'] == 'time') {
  886. if (trim($value) != '') {
  887. $value = date('H:i', convert_time(strtotime($value)));
  888. }
  889. }
  890. $form[$field_name]['elements'][$c][$efield_name]["value"] = $value;
  891. if (trim($value)) {
  892. $form[$field_name]['start_closed'] = FALSE;
  893. }
  894. }
  895. }
  896. } // else is fieldset
  897. }
  898. }
  899. // We never show the published option, since we will always give the user a way to "remove" and "restore" content.
  900. $form['published'] = array(
  901. 'type' => 'hidden',
  902. 'value' => intval($content->published),
  903. );
  904. // Draw the controls (buttons)
  905. $form["submit_submit"] = array(
  906. "type" => "submit",
  907. "value" => t("Submit"),
  908. "spinner" => TRUE,
  909. 'weight' => 9920,
  910. 'attributes' => array('class' => 'content-submit-btn'),
  911. );
  912. if ($cid != 'new') {
  913. // Only display the delete button if we have permission
  914. if (content_user_access('delete', $content->cid)) {
  915. $form["submit_delete"] = array(
  916. "type" => "submit",
  917. "value" => t("Delete"),
  918. "confirm" => t('Are you sure you wish to delete this?\nThis action cannot be undone.'),
  919. 'weight' => 9930,
  920. //'spinner' => TRUE,
  921. 'attributes' => array('class' => 'content-delete-btn'),
  922. );
  923. }
  924. }
  925. watchdog('content', "edit_content_form cid:$cid, type:$type", array(), WATCHDOG_DEBUG);
  926. return $form;
  927. }
  928. function content_get_uploaded_file($fid) {
  929. $res = db_query("SELECT * FROM content_files WHERE fid = ?", array($fid));
  930. $cur = db_fetch_array($res);
  931. if ($cur) {
  932. $temp = explode(".", $cur['filename']);
  933. $ext = $temp[count($temp) - 1]; // get the extension.
  934. $cur['full_filename'] = fp_get_files_path() . '/content_uploads/' . $cur['filename'];
  935. // also get the URL for downloading this file.
  936. // If this file was encrypted, then the url should change to the url to decrypt this particular file (probably by passing it the fid)
  937. //if (intval($cur['is_encrypted']) === 1) {
  938. // *** Always handle downloads through FlightPath, for security reasons. ***
  939. $cur['url'] = fp_url('content-files/handle-download/' . $cur['fid']);
  940. //}
  941. //else {
  942. // $cur['url'] = base_path() . "/custom/files/content_uploads/" . $cur['filename'];
  943. //}
  944. $cur['ext'] = $ext;
  945. }
  946. return $cur;
  947. }
  948. /**
  949. * Find by mime type OR file extension.
  950. */
  951. function content_get_fontawesome_icon_for_mimetype($mime_type, $ext = "") {
  952. // List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml
  953. $icon_classes = array(
  954. // Media
  955. 'image' => 'fa-file-image-o',
  956. 'audio' => 'fa-file-audio-o',
  957. 'video' => 'fa-file-video-o',
  958. // Documents
  959. 'application/pdf' => 'fa-file-pdf-o',
  960. 'application/msword' => 'fa-file-word-o',
  961. 'application/vnd.ms-word' => 'fa-file-word-o',
  962. 'application/vnd.oasis.opendocument.text' => 'fa-file-word-o',
  963. 'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word-o',
  964. 'application/vnd.ms-excel' => 'fa-file-excel-o',
  965. 'application/vnd.openxmlformats-officedocument.spreadsheetml' => 'fa-file-excel-o',
  966. 'application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel-o',
  967. 'application/vnd.ms-powerpoint' => 'fa-file-powerpoint-o',
  968. 'application/vnd.openxmlformats-officedocument.presentationml' => 'fa-file-powerpoint-o',
  969. 'application/vnd.oasis.opendocument.presentation' => 'fa-file-powerpoint-o',
  970. 'text/plain' => 'fa-file-text-o',
  971. 'text/html' => 'fa-file-code-o',
  972. 'application/json' => 'fa-file-code-o',
  973. // Archives
  974. 'application/gzip' => 'fa-file-archive-o',
  975. 'application/zip' => 'fa-file-archive-o',
  976. );
  977. $rtn = "";
  978. foreach ($icon_classes as $text => $icon) {
  979. if (strpos($mime_type, $text) === 0) {
  980. $rtn = $icon;
  981. }
  982. }
  983. if ($rtn) return $rtn;
  984. // else, we search by file extension now....
  985. $ext_array = array(
  986. 'png' => 'fa-file-image-o',
  987. 'jpg' => 'fa-file-image-o',
  988. 'jpeg' => 'fa-file-image-o',
  989. 'gif' => 'fa-file-image-o',
  990. "mp3" => "fa-file-audio-o",
  991. "mpg" => "fa-file-video-o",
  992. "mpeg" => "fa-file-video-o",
  993. "mp4" => "fa-file-video-o",
  994. 'pdf' => 'fa-file-pdf-o',
  995. 'doc' => 'fa-file-word-o',
  996. 'pages' => 'fa-file-word-o',
  997. 'docx' => 'fa-file-word-o',
  998. 'rtf' => 'fa-file-word-o',
  999. 'odt' => 'fa-file-word-o',
  1000. 'xls' => 'fa-file-excel-o',
  1001. 'xlsx' => 'fa-file-excel-o',
  1002. 'numbers' => 'fa-file-excel-o',
  1003. 'csv' => 'fa-file-excel-o',
  1004. 'ppt' => 'fa-file-powerpoint-o',
  1005. 'pptx' => 'fa-file-powerpoint-o',
  1006. 'odp' => 'fa-file-powerpoint-o',
  1007. 'txt' => 'fa-file-text-o',
  1008. 'html' => 'fa-file-code-o',
  1009. 'htm' => 'fa-file-code-o',
  1010. 'json' => 'fa-file-code-o',
  1011. // Archives
  1012. 'gzip' => 'fa-file-archive-o',
  1013. 'zip' => 'fa-file-archive-o',
  1014. );
  1015. foreach ($ext_array as $text => $icon) {
  1016. if (strpos($ext, $text) === 0) {
  1017. $rtn = $icon;
  1018. }
  1019. }
  1020. if ($rtn) return $rtn;
  1021. return 'fa-file-o';
  1022. }
  1023. function content_edit_content_form_validate(&$form, &$form_state) {
  1024. // If we are submitting any files, make sure they have allowed extensions.
  1025. $values = $form_state['values'];
  1026. $type = $values["type"];
  1027. $types = content_get_types();
  1028. foreach ($types[$type]['fields'] as $fieldname => $details) {
  1029. if ($details['type'] != 'cfieldset') {
  1030. // If this field type is "file" then we are going to do something special with it
  1031. // don't need to do this if we are just editing an existing content node and hitting "save"
  1032. if ($details['type'] == 'file') {
  1033. $uploaded_files = fp_re_array_files($_FILES[$fieldname]); // makes them easier to work with.
  1034. $allowed = '';
  1035. if (isset($details['allowed'])) {
  1036. $allowed = trim($details['allowed']);
  1037. }
  1038. if (!$allowed) {
  1039. $allowed = "txt, pdf, doc, docx, xls, xlsx, ppt, pptx, rtf, odt, jpg, jpeg, png, gif, zip, 7z";
  1040. }
  1041. $allowed = str_replace(".", "", strtolower($allowed)); // get rid of dots and make lowercase.
  1042. $allowed = str_replace(" ", "", $allowed);
  1043. $allowed_csv = csv_to_array($allowed);
  1044. $fid = "";
  1045. foreach ($uploaded_files as $file) {
  1046. if (trim($file['name']) == "") continue; // blank, so skip it.
  1047. // Get the extension for the file.
  1048. $temp = explode(".", $file['name']);
  1049. $ext = strtolower(trim($temp[count($temp) - 1]));
  1050. if (!in_array($ext, $allowed_csv)) {
  1051. form_error($fieldname, t("Sorry, but the file %file is not allowed to be uploaded (file extension \"%ext\" not allowed). Please see the list of allowed file types/extensions.", array("%file" => $file['name'], "%ext" => $ext)));
  1052. return;
  1053. }
  1054. } // foreach uploaded files
  1055. } // if a file
  1056. } // if not a cfieldset
  1057. } // foreach
  1058. } // ... validate
  1059. /**
  1060. * Submit handler for the edit content form.
  1061. *
  1062. * @param unknown_type $form
  1063. * @param unknown_type $form_state
  1064. */
  1065. function content_edit_content_form_submit(&$form, &$form_state) {
  1066. global $user;
  1067. $values = $form_state["values"];
  1068. $cid = $values['cid'];
  1069. $type = $values["type"];
  1070. // are we trying to delete?
  1071. if (isset($values['submit_delete']) && trim($values['submit_delete']) != '') {
  1072. // Yes, we are deleting!
  1073. $content = content_load($cid);
  1074. if (!content_user_access('delete', $content->cid)) {
  1075. fp_add_message(t("Sorry, you do not have permission to delete this content."));
  1076. fp_goto("<front>");
  1077. return;
  1078. }
  1079. $content->delete_flag = 1;
  1080. content_save($content);
  1081. watchdog("content", "delete cid:$cid, type:$type", array());
  1082. // find out if we should redirect anywhere in particular for this deletion.
  1083. if (isset($form['#redirect'])) {
  1084. fp_add_message(t("The content was deleted successfully."));
  1085. $form['#redirect']['query'] .= "&del=" . $content->cid;
  1086. }
  1087. else {
  1088. // No redirect was specified, so let's send us to the home page.
  1089. fp_add_message(t("The content was deleted successfully."));
  1090. $form['#redirect']['path'] = "<front>";
  1091. }
  1092. return;
  1093. }
  1094. $now = time();
  1095. $types = content_get_types();
  1096. // Does the user have permission to edit/add this content?
  1097. $access_action = "add";
  1098. $var = $type;
  1099. if ($cid !== "new") {
  1100. $access_action = "edit";
  1101. $var = $cid;
  1102. }
  1103. if (!content_user_access($access_action, $var)) {
  1104. fp_add_message(t("Sorry, you do not have permission to @action that content type.", array("@action" => $access_action)));
  1105. fp_goto("admin-tools/content");
  1106. return;
  1107. }
  1108. // Assemble into a content object, then call content_save();
  1109. if ($values['cid'] == 'new') {
  1110. // This is a NEW content obj!
  1111. $content = new stdClass();
  1112. $content->type = $type;
  1113. $content->cid = "new";
  1114. $content->published = 1;
  1115. $content->delete_flag = 0;
  1116. content_save($content); // Do an initial save, so we get its cid back.
  1117. }
  1118. else {
  1119. // This is an EXISTING content. Load it.
  1120. $content = content_load($values['cid']);
  1121. }
  1122. $cid = $content->cid;
  1123. $content->title = $values['title']; // required
  1124. // set published status. 0 for not, 1 for published
  1125. $content->published = (bool) $values['published'];
  1126. // Add in our new field values.
  1127. foreach ($types[$type]['fields'] as $fieldname => $details) {
  1128. if ($details['type'] != 'cfieldset') {
  1129. if ($details['type'] == 'datetime-local') {
  1130. // Since this is datetime-local, the value is actually already in the user's timezone.
  1131. // We want to CHANGE THIS TO UTC, so it saves in the database as UTC.
  1132. $val_ts = @strtotime($values[$fieldname]);
  1133. $utc_ts = convert_time($val_ts, fp_get_user_timezone(), 'UTC');
  1134. $values[$fieldname] = date('Y-m-d H:i:s', $utc_ts);
  1135. }
  1136. // Do the same for "time" field type. We need to convert to UTC from the user's timezone.
  1137. if ($details['type'] == 'time') {
  1138. // Since this is time, the value is actually already in the user's timezone.
  1139. // We want to CHANGE THIS TO UTC, so it saves in the database as UTC.
  1140. $val_ts = @strtotime("2000-01-01 " . $values[$fieldname]); // give a bogus date just so our times make since.
  1141. $utc_ts = convert_time($val_ts, fp_get_user_timezone(), 'UTC');
  1142. $values[$fieldname] = date('H:i', $utc_ts);
  1143. }
  1144. // If this field type is "file" then we are going to do something special with it
  1145. // don't need to do this if we are just editing an existing content node and hitting "save"
  1146. if ($details['type'] == 'file') {
  1147. $uploaded_files = fp_re_array_files($_FILES[$fieldname]); // makes them easier to work with.
  1148. $fid = "";
  1149. foreach ($uploaded_files as $file) {
  1150. if ($file && $file['name'] != '') {
  1151. // Now, save to the content_files table.
  1152. $fid .= content_add_new_uploaded_file($file, $cid) . ",";
  1153. }
  1154. } // foreach
  1155. // Do we have any previously uploaded files we need to include?
  1156. if (@$form_state['values']['previous_upload_' . $fieldname] != "") {
  1157. $fid .= "," . $form_state['values']['previous_upload_' . $fieldname] . ",";
  1158. }
  1159. $fid = ltrim($fid, ",");
  1160. for ($t = 0; $t < 10; $t++) {
  1161. $fid = str_replace(",,", ",", $fid); // get rid of any duplicate commas
  1162. }
  1163. $fid = rtrim($fid, ","); // trim any extra commas
  1164. $values[$fieldname] = $fid;
  1165. } // if details = file
  1166. $content->{'field__' . $fieldname}['value'] = @$values[$fieldname];
  1167. }
  1168. else {
  1169. foreach ($details['elements'] as $c => $v) {
  1170. foreach ($details['elements'][$c] as $fieldname => $edetails) {
  1171. if ($details['type'] == 'datetime-local') {
  1172. // Since this is datetime-local, the value is actually already in the user's timezone.
  1173. // We want to CHANGE THIS TO UTC, so it saves in the database as UTC.
  1174. $val_ts = @strtotime($values[$fieldname]);
  1175. $utc_ts = convert_time($val_ts, fp_get_user_timezone(), 'UTC');
  1176. $values[$fieldname] = date('Y-m-d H:i:s', $utc_ts);
  1177. }
  1178. // Do the same for "time" field type. We need to convert to UTC from the user's timezone.
  1179. if ($details['type'] == 'time') {
  1180. // Since this is time, the value is actually already in the user's timezone.
  1181. // We want to CHANGE THIS TO UTC, so it saves in the database as UTC.
  1182. $val_ts = @strtotime("2000-01-01 " . $values[$fieldname]); // give a bogus date just so our times make since.
  1183. $utc_ts = convert_time($val_ts, fp_get_user_timezone(), 'UTC');
  1184. $values[$fieldname] = date('H:i', $utc_ts);
  1185. }
  1186. $content->{'field__' . $fieldname}['value'] = @$values[$fieldname];
  1187. }
  1188. }
  1189. }
  1190. }
  1191. content_save($content);
  1192. $form_state['content_object'] = $content;
  1193. watchdog("content", "edited/saved cid:$cid, type:$type", array());
  1194. if (isset($form['#redirect'])) {
  1195. if ($form['#redirect']['path'] == 'content/new') {
  1196. $form['#redirect']['path'] = 'content/' . $content->cid;
  1197. }
  1198. }
  1199. fp_add_message(t('Saved successfully.'));
  1200. } // ... submit
  1201. /**
  1202. * bool_replace_existing means should we replace an existing file (based on filename) with this new one? Instead of creating an INSERT, that is.
  1203. */
  1204. function content_add_new_uploaded_file($file, $cid = 0, $bool_override_original_filename = TRUE, $attributes = CONTENT_PRIVATE_FILE, $bool_replace_exsting_filename = FALSE) {
  1205. // Store the file information in the content_files table, and then THIS field contains the fid reference number.
  1206. $original_filename = @$file["name"];
  1207. $type = @$file["type"];
  1208. $tmp_name = @$file["tmp_name"];
  1209. if (!strstr($tmp_name, "/")) {
  1210. // Doesn't contain the temp location. Pre-prend to the tmp_name.
  1211. $tmp_name = sys_get_temp_dir() . "/" . $tmp_name;
  1212. }
  1213. // Figure out the extension of the original filename.
  1214. $temp = explode(".", $original_filename);
  1215. $ext = $temp[count($temp) - 1];
  1216. $filename = $original_filename;
  1217. if ($bool_override_original_filename) {
  1218. $filename = 'file__' . time() . '' . mt_rand(9,9999) . '_' . $original_filename;
  1219. }
  1220. // If the filename is too long, shorten it. Linux won't allow more than 255 bytes (usually corresponds to chars, depending on file system
  1221. // it might be as low as ),
  1222. // Windows its 260 chars. Let's be safe and stop at 100 chars + ext.
  1223. if (strlen($filename) > 100) {
  1224. $filename = substr($filename, 0, 100) . ".$ext";
  1225. }
  1226. $extra = "";
  1227. if ($attributes === CONTENT_PUBLIC_FILE) {
  1228. // If this is a "public" file, then put in the correct public files dir.
  1229. $extra .= "public_uploads/";
  1230. }
  1231. $full_filename = fp_get_files_path() . '/content_uploads/' . $extra . $filename;
  1232. $is_encrypted = 0;
  1233. // If we should be encrypting files, so do here, and set the is_encrypted value correctly.
  1234. if (module_enabled('encryption') && variable_get("encryption_files_encryption", "yes") === 'yes' && encryption_get_key() && $attributes != CONTENT_PUBLIC_FILE) {
  1235. // (note: public files are never encrypted)
  1236. // We need to do that by loading the file into memory, then getting the encrypted version, then writing it
  1237. // out to the destination.
  1238. $file_contents = file_get_contents($tmp_name);
  1239. $enc_file_contents = encryption_encrypt($file_contents);
  1240. $full_filename .= ".enc";
  1241. $filename .= ".enc";
  1242. if (!file_put_contents($full_filename, $enc_file_contents)) {
  1243. fp_add_message(t("Could not upload file. Possibly because of permission issues on the destination directory,
  1244. the disk is full, or some other reason."), "error");
  1245. return;
  1246. }
  1247. $is_encrypted = 1;
  1248. } // encryption module is enabled and we should be encrypting.
  1249. else {
  1250. // No encryption, just copy the normal way.
  1251. $is_encrypted = 0;
  1252. if (!copy($tmp_name, $full_filename)) {
  1253. fp_add_message(t("Could not upload file. Possibly because of permission issues on the destination directory,
  1254. the disk is full, or some other reason."), "error");
  1255. }
  1256. }
  1257. if ($bool_replace_exsting_filename) {
  1258. $fid = db_result(db_query("SELECT fid FROM content_files
  1259. WHERE filename = ?
  1260. AND cid = ?
  1261. AND `attributes` = ?", array($filename, $cid, $attributes)));
  1262. if ($fid) {
  1263. db_query("UPDATE content_files SET posted = ? WHERE fid = ?", array(time(), $fid));
  1264. return $fid;
  1265. }
  1266. else {
  1267. // File wasn't in there. Just do nothing here and proceed to a normal insert.
  1268. }
  1269. }
  1270. // Add to the content_files table.
  1271. db_query("INSERT INTO content_files (cid, original_filename, filename, mimetype, is_encrypted, posted, `attributes`)
  1272. VALUES (?, ?, ?, ?, ?, ?, ?)", array($cid, $original_filename, $filename, $type, $is_encrypted, time(), $attributes));
  1273. return db_insert_id();
  1274. }
  1275. /**
  1276. * Save the content to the database.
  1277. *
  1278. * @param unknown_type $cid
  1279. */
  1280. function content_save(&$content) {
  1281. // Remove from our globals cache
  1282. if ($content->cid != 'new') {
  1283. unset($GLOBALS['content_cache'][$content->cid]);
  1284. }
  1285. $modules = modules_implement_hook("content_save");
  1286. foreach ($modules as $module) {
  1287. call_user_func_array($module . "_content_save", array(&$content));
  1288. }
  1289. // Save to globals cache
  1290. $GLOBALS['content_cache'][$content->cid] = $content;
  1291. }
  1292. /**
  1293. * Implements hook_content_save. We will save the content object to the database.
  1294. */
  1295. function content_content_save(&$content) {
  1296. global $user;
  1297. $types = content_get_types();
  1298. $type = $content->type;
  1299. if (!isset($content->title) || !$content->title || $content->title === NULL || $content->title === FALSE) {
  1300. $content->title = t("Content") . " " . time(); // give a default title, since title cannot be null.
  1301. }
  1302. $log = @trim($content->log);
  1303. // If this is a NEW content, then insert once into content table to get cid. Then, insert into content_versions to get vid. Then update content table with vid.
  1304. if ($content->cid == 'new') {
  1305. db_query('INSERT INTO content (user_id, `type`, title, posted, updated, published, delete_flag, `log`)
  1306. VALUES (?, ?, ?, ?, ?, ?, ?, ?)', array($user->id, $type, $content->title, time(), time(), $content->published, 0, $log));
  1307. $content->cid = db_insert_id();
  1308. // insert into content_versions to get the vid.
  1309. db_query("INSERT INTO content_versions (cid, `user_id`) VALUES (?,?)", array($content->cid, $user->id));
  1310. $content->vid = db_insert_id();
  1311. }
  1312. else {
  1313. // If this is an EXISTING content, then insert once into content_versions to get new vid. Update title and vid in content table.
  1314. // insert into content_versions to get the vid.
  1315. db_query("INSERT INTO content_versions (`cid`, `user_id`) VALUES (?,?)", array($content->cid, $user->id));
  1316. $content->vid = db_insert_id();
  1317. }
  1318. if (!$content->delete_flag) $content->delete_flag = 0; // force content->delete_flag to equal a value.
  1319. // Now we should have a fully created $content object. Let's update the title and other values in the content table.
  1320. db_query("UPDATE content SET vid = ?, title = ?, updated = ?, published = ?, delete_flag = ?, `log` = ? WHERE
  1321. cid = ?", array($content->vid, $content->title, time(), $content->published, $content->delete_flag, $log, $content->cid));
  1322. // TODO: If deleting, should we delete associated files too? Maybe we do this on a cron after X number of months?
  1323. // Now we are going to insert all of the field values. We assume there is a table ALREADY CREATED with our field names
  1324. // as field__the_name.
  1325. // Insert new row into content__$type table, including all the extra fields and such. It should
  1326. // look like cid | vid | field__name | field__name | and so on.
  1327. //
  1328. // Add in our new field values.
  1329. $params = array();
  1330. $all_fields = array();
  1331. $param_names = array();
  1332. $all_fields[] = 'cid';
  1333. $all_fields[] = 'vid';
  1334. $param_names[] = ':cid';
  1335. $param_names[] = ':vid';
  1336. $params[':cid'] = $content->cid;
  1337. $params[':vid'] = $content->vid;
  1338. foreach ($types[$type]['fields'] as $fieldname => $details) {
  1339. if ($details['type'] != 'cfieldset' && $details['type'] != '' && !strstr($details['type'], 'markup')) { // skip markup and fieldsets
  1340. $db_field_name = "field__$fieldname";
  1341. $all_fields[] = $db_field_name;
  1342. $param_names[] = ":$db_field_name";
  1343. $val = NULL;
  1344. if (isset($content->{$db_field_name}) && isset($content->{$db_field_name}['value'])) {
  1345. $val = $content->{$db_field_name}['value'];
  1346. }
  1347. if ($details['type'] == 'checkboxes' && is_array($val)) {
  1348. // Since we came from "checkboxes", and val is an array, we will save the following way:
  1349. // ~~key~~key~~key~~. This lets us query like so: LIKE '%~~key~~%' to find elements with that key. Not the most
  1350. // elegant way to do it, but it should work for now.
  1351. $new_val = "~~"; // always begin with ~~
  1352. foreach ($val as $k => $v) {
  1353. $new_val .= $k . "~~"; // always end with ~~.
  1354. }
  1355. $val = $new_val;
  1356. }
  1357. else if (is_array($val)) {
  1358. // Not sure what this is, but we will serialize it.
  1359. $val = serialize($val);
  1360. }
  1361. $params[":field__$fieldname"] = $val;
  1362. }
  1363. else if ($details['type'] == 'cfieldset'){
  1364. // This is a fieldset, so now we need to descend into its elements to find the fields we need to save.
  1365. foreach($details['elements'] as $c => $v) {
  1366. foreach ($details['elements'][$c] as $fieldname => $edetails) {
  1367. if (strstr($edetails['type'], 'markup') || $edetails['type'] == '') continue; // skip markup
  1368. $db_field_name = "field__$fieldname";
  1369. $all_fields[] = $db_field_name;
  1370. $param_names[] = ":$db_field_name";
  1371. $val = NULL;
  1372. if (isset($content->{$db_field_name}) && isset($content->{$db_field_name}['value'])) {
  1373. $val = $content->{$db_field_name}['value'];
  1374. }
  1375. if ($edetails['type'] == 'checkboxes' && is_array($val)) {
  1376. // Since we came from "checkboxes", and val is an array, we will save the following way:
  1377. // ~~key~~key~~key~~. This lets us query like so: LIKE '%~~key~~%' to find elements with that key. Not the most
  1378. // elegant way to do it, but it should work for now.
  1379. $new_val = "~~"; // always begin with ~~
  1380. foreach ($val as $k => $v) {
  1381. $new_val .= $k . "~~"; // always end with ~~.
  1382. }
  1383. $val = $new_val;
  1384. }
  1385. else if (is_array($val)) {
  1386. // Not sure what this is, but we will serialize it.
  1387. $val = serialize($val);
  1388. }
  1389. $params[":field__$fieldname"] = $val;
  1390. }
  1391. }
  1392. } //else/if
  1393. } // foreach
  1394. $sql = "INSERT INTO content__$type
  1395. (" . join(", " , $all_fields) . ")
  1396. VALUES (" . join(", " , $param_names) . ")";
  1397. db_query($sql, $params);
  1398. // Re-calc our alert counts.
  1399. fp_recalculate_alert_count_by_type();
  1400. watchdog('content', "content_save cid:$content->cid");
  1401. } // content_content_save
  1402. /**
  1403. * Load the content from the database and return an array, by calling hook_content_load.
  1404. *
  1405. * @param unknown_type $cid
  1406. */
  1407. function content_load($cid, $reset = FALSE) {
  1408. if (!$cid) return null;
  1409. if ($reset) {
  1410. unset($GLOBALS['content_cache'][$cid]);
  1411. }
  1412. if (isset($GLOBALS['content_cache'][$cid])) {
  1413. return $GLOBALS['content_cache'][$cid];
  1414. }
  1415. $content = new stdClass();
  1416. $content->cid = $cid;
  1417. $modules = modules_implement_hook("content_load");
  1418. foreach ($modules as $module) {
  1419. call_user_func_array($module . "_content_load", array(&$content));
  1420. }
  1421. // Add to our globals cache
  1422. $GLOBALS['content_cache'][$cid] = $content;
  1423. return $content;
  1424. }
  1425. /**
  1426. * Hook form alter, to set some defaults.
  1427. */
  1428. function content_form_alter(&$form, $form_id) {
  1429. if ($form_id == 'content_edit_content_form') {
  1430. // This is a NEW form.
  1431. if ($form['cid']['value'] === 'new') {
  1432. // Always set the published = TRUE, and hide.
  1433. $form['published']['value'] = TRUE;
  1434. $form['published']['attributes'] = array('class' => 'hidden');
  1435. // Set visibility to 'faculty' by default, if it exists.
  1436. if (isset($form['visibility'])) {
  1437. $form['visibility']['value'] = 'faculty';
  1438. }
  1439. } // if "new"
  1440. } // if form_id
  1441. } // hook_form_alter
  1442. /**
  1443. * This function will look for content (in the content tables) for a field called field__faculty_id, and return an array
  1444. * of fully loaded content objects.
  1445. */
  1446. function content_get_content_for_faculty_id($content_type, $faculty_id, $order_by = "title ASC", $bool_published_only = TRUE) {
  1447. $rtn = array();
  1448. $published_line = " AND n.published = 1 ";
  1449. if (!$bool_published_only) $published_line = "";
  1450. // Display all of the event types for this faculty member.
  1451. $res = db_query("SELECT DISTINCT(a.cid) FROM content__$content_type a, content n
  1452. WHERE field__faculty_id = ?
  1453. AND a.vid = n.vid
  1454. AND a.cid = n.cid
  1455. AND n.delete_flag = 0
  1456. $published_line
  1457. ORDER BY $order_by, a.vid DESC", array($faculty_id));
  1458. while ($cur = db_fetch_object($res)) {
  1459. $cid = $cur->cid;
  1460. $content = content_load($cid);
  1461. $rtn[$cid] = $content;
  1462. }
  1463. return $rtn;
  1464. }
  1465. /**
  1466. * Implementation of content's hook_content_load
  1467. *
  1468. * We simply wish to load from our db table. The cid is in
  1469. * $content->cid.
  1470. *
  1471. * @param unknown_type $content
  1472. */
  1473. function content_content_load(&$content) {
  1474. $res = db_query("SELECT * FROM content WHERE cid = ?
  1475. ORDER BY vid DESC
  1476. LIMIT 1", array( $content->cid));
  1477. $content = db_fetch_object($res);
  1478. if (!$content) return FALSE;
  1479. // Populate our field values in the content object
  1480. $res = db_query("SELECT * FROM content__$content->type WHERE vid = ?", array($content->vid));
  1481. $fieldvals = db_fetch_array($res);
  1482. $types = content_get_types();
  1483. // This condition can occur if we have content from a module which is no longer enables.
  1484. if (!isset($types[$content->type])) {
  1485. return;
  1486. }
  1487. foreach ($types[$content->type]['fields'] as $fieldname => $details) {
  1488. if ($details['type'] != 'cfieldset') {
  1489. $val = @$fieldvals['field__' . $fieldname];
  1490. if (is_serialized_string($val)) {
  1491. $val = unserialize($val);
  1492. }
  1493. $content->{'field__' . $fieldname} = array('value' => $val, 'field' => $details);
  1494. $display_value = $val;
  1495. // Figure out or clean up the "display_value" for this field.
  1496. if (isset($details['filter']) && $details['filter'] != 'plain') {
  1497. $display_value = filter_markup(trim($display_value), $details['filter']);
  1498. }
  1499. else {
  1500. // Always assumed to be "plain" if no filter is set.
  1501. $display_value = filter_markup($display_value, "plain");
  1502. // Need to add in line breaks if this was plain.
  1503. $display_value = nl2br(trim($display_value));
  1504. }
  1505. // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly.
  1506. if ($details['type'] == 'datetime-local') {
  1507. if (trim($display_value) != '') {
  1508. //$value = date('Y-m-d\TH:i', strtotime($value));
  1509. $format = 'standard';
  1510. if (isset($details['format_date'])) {
  1511. $format = $details['format_date'];
  1512. }
  1513. $display_value = format_date(convert_time(strtotime($display_value)), $format);
  1514. }
  1515. }
  1516. // if this is a "time" field, then similar to datetime-local, it needs to be adjusted. That's because
  1517. // in the database we saved it as UTC. We ned to now convert into the user's timezone.
  1518. if ($details['type'] == 'time') {
  1519. if (trim($display_value) != '') {
  1520. $format = 'standard';
  1521. if (isset($details['format_date'])) {
  1522. $format = $details['format_date'];
  1523. }
  1524. $display_value = format_date(convert_time(strtotime("2001-01-01 $display_value")), 'custom', 'g:ia');
  1525. }
  1526. }
  1527. // if this is a select list, we want to display the option label this value went with.
  1528. if ($details['type'] == 'select' || $details['type'] == 'radios') {
  1529. $temp = @$details['options'][$val];
  1530. if ($temp != "") $display_value = $temp;
  1531. }
  1532. // If this is a single checkbox, then the possible values are 1 or blank. Let's make the display_value a little more interesting
  1533. if ($details['type'] == 'checkbox') {
  1534. $val = intval($val);
  1535. if ($val === 1) {
  1536. $display_value = t("Yes / Checked");
  1537. }
  1538. else {
  1539. $display_value = t("No / Unchecked");
  1540. }
  1541. }
  1542. // If this was a set of checkboxes ($val is an array), we want to display the label the values went to.
  1543. if ($details['type'] == 'checkboxes') {
  1544. $display_value = "<div class='checkbox-values checkbox-values-field__$fieldname'>";
  1545. // Convert val into an array. It will look like: ~~key~~key~~key~~ etc.
  1546. @$temp = explode("~~", $val);
  1547. if (!is_array($temp)) $temp = array();
  1548. $val = array();
  1549. foreach ($temp as $k) {
  1550. if (trim($k) == "") continue;
  1551. $val[$k] = $k;
  1552. }
  1553. if (is_array($val)) {
  1554. foreach ($val as $k => $v) {
  1555. $temp = @$details['options'][$v];
  1556. if ($temp != "") $display_value .= "<div class='checkbox-val'>$temp</div>";
  1557. }
  1558. }
  1559. $display_value .= "</div>";
  1560. $content->{'field__' . $fieldname}['value'] = $val;
  1561. }
  1562. if ($details['type'] == 'file') {
  1563. if ($val != "") {
  1564. $display_value = "";
  1565. $fids = explode(",", $val);
  1566. foreach ($fids as $fid) {
  1567. if ($fid == '') continue;
  1568. $fid = intval($fid);
  1569. $file = content_get_uploaded_file($fid);
  1570. if ($file) {
  1571. $url = $file['url'];
  1572. $display_value .= "<div class='field-file-attachment-wrapper'><a href='$url'>" . $file['original_filename'] . "</a></div>";
  1573. }
  1574. }
  1575. }
  1576. }
  1577. $content->{'field__' . $fieldname}['display_value'] = $display_value;
  1578. } // type != cfieldset
  1579. else {
  1580. // This is a fieldset, so we need to go into all its elements.
  1581. foreach ($details['elements'] as $c => $v) {
  1582. foreach ($details['elements'][$c] as $fieldname => $edetails) {
  1583. $val = @$fieldvals['field__' . $fieldname];
  1584. if (is_serialized_string($val)) {
  1585. $val = unserialize($val);
  1586. }
  1587. $content->{'field__' . $fieldname} = array('value' => $val, 'field' => $details);
  1588. $display_value = $val;
  1589. if (isset($edetails['filter']) && $edetails['filter'] != 'plain') {
  1590. $display_value = filter_markup(trim($display_value), $edetails['filter']);
  1591. }
  1592. else {
  1593. // Always assumed to be "plain" if no filter is set.
  1594. $display_value = filter_markup($display_value, "plain");
  1595. // Need to add in line breaks if this was plain.
  1596. $display_value = nl2br(trim($display_value));
  1597. }
  1598. // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly.
  1599. if ($edetails['type'] == 'datetime-local') {
  1600. if (trim($display_value) != '') {
  1601. //$value = date('Y-m-d\TH:i', strtotime($value));
  1602. $format = 'standard';
  1603. if (isset($details['format_date'])) {
  1604. $format = $details['format_date'];
  1605. }
  1606. $display_value = format_date(convert_time(strtotime($display_value)), $format);
  1607. }
  1608. }
  1609. // if this is a "time" field, then similar to datetime-local, it needs to be adjusted. That's because
  1610. // in the database we saved it as UTC. We ned to now convert into the user's timezone.
  1611. if ($edetails['type'] == 'time') {
  1612. if (trim($display_value) != '') {
  1613. $format = 'standard';
  1614. if (isset($details['format_date'])) {
  1615. $format = $details['format_date'];
  1616. }
  1617. $display_value = format_date(convert_time(strtotime("2001-01-01 $display_value")), 'custom', 'g:ia');
  1618. }
  1619. }
  1620. // if this is a select list, we want to display the option label this value went with.
  1621. if ($edetails['type'] == 'select') {
  1622. $temp = @$edetails['options'][$val];
  1623. if ($temp != "") $display_value = $temp;
  1624. }
  1625. if ($edetails['type'] == 'file') {
  1626. $fid = intval($val);
  1627. $file = content_get_uploaded_file($fid);
  1628. if ($file) {
  1629. $url = $file['url'];
  1630. $display_value = "<a href='$url'>" . $file['original_filename'] . "</a>";
  1631. }
  1632. }
  1633. // If this was a set of checkboxes ($val is an array), we want to display the label the values went to.
  1634. if ($edetails['type'] == 'checkboxes') {
  1635. $display_value = "<div class='checkbox-values checkbox-values-field__$fieldname'>";
  1636. // Convert val into an array. It will look like: ~~key~~key~~key~~ etc.
  1637. $temp = explode("~~", $val);
  1638. $val = array();
  1639. foreach ($temp as $k) {
  1640. if (trim($k) == "") continue;
  1641. $val[$k] = $k;
  1642. }
  1643. if (is_array($val)) {
  1644. foreach ($val as $k => $v) {
  1645. $temp = @$details['options'][$v];
  1646. if ($temp != "") $display_value .= "<div class='checkbox-val'>$temp</div>";
  1647. }
  1648. }
  1649. $display_value .= "</div>";
  1650. $content->{'field__' . $fieldname}['value'] = $val;
  1651. }
  1652. $content->{'field__' . $fieldname}['display_value'] = $display_value;
  1653. }
  1654. }
  1655. } // else type == cfielset
  1656. } // foreach
  1657. }
  1658. /**
  1659. * hook_cron
  1660. */
  1661. function content_cron() {
  1662. // We want to delete any content (and files!) which have been scheduled for deletion.
  1663. // Check to see when the last time we did this. If less than X number of days, we don't do it.
  1664. $last_run = intval(variable_get("content_last_run_delete_flag_removal", 0));
  1665. $check_against = strtotime("NOW - 7 DAYS"); // don't run any more often than once every 7 DAYS.
  1666. $delete_threshold = strtotime("NOW - 7 DAYS"); // delete content older than 7 days.
  1667. $c = 0;
  1668. if ($check_against > $last_run) {
  1669. $res = db_query("SELECT * FROM content
  1670. WHERE delete_flag = 1
  1671. AND `updated` < ?", array($delete_threshold));
  1672. while ($cur = db_fetch_object($res)) {
  1673. $cid = $cur->cid;
  1674. $type = $cur->type;
  1675. // Sanitize $type as best we can. Should be fine coming from database, but just in case.
  1676. $type = str_replace("`", "", $type);
  1677. $type = str_replace("'", "", $type);
  1678. $type = str_replace('"', "", $type);
  1679. $type = str_replace(' ', "", $type);
  1680. $type = str_replace('(', "", $type);
  1681. // First, are there any files associated with this cid we need to delete?
  1682. $res2 = db_query("SELECT * FROM content_files WHERE cid = ?", array($cid));
  1683. while ($cur2 = db_fetch_object($res2)) {
  1684. $fid = $cur2->fid;
  1685. $full_filename = fp_get_files_path() . '/content_uploads/' . $cur2->filename;
  1686. if (!unlink($full_filename)) {
  1687. watchdog("content", "Could not delete $full_filename for content $cid", array(), WATCHDOG_ERROR);
  1688. fpm("Could not delete $full_filename for content $cid");
  1689. }
  1690. db_query("DELETE FROM content_files WHERE fid = ?", array($fid));
  1691. }
  1692. // Now, delete the content from the table it belongs to...
  1693. db_query("DELETE FROM `content__$type` WHERE cid = ?", array($cid));
  1694. // Delete from content_versions and content_last_access, engagements_tracking
  1695. db_query("DELETE FROM content_versions WHERE cid = ?", array($cid));
  1696. db_query("DELETE FROM content_last_access WHERE cid = ?", array($cid));
  1697. db_query("DELETE FROM engagements_tracking WHERE cid = ?", array($cid));
  1698. // Lastly, delete from content table itself.
  1699. db_query("DELETE FROM content WHERE cid = ?", array($cid));
  1700. watchdog("content", "Deleted from db deleted content $cid", array(), WATCHDOG_DEBUG);
  1701. $c++;
  1702. } // while cur
  1703. watchdog("content", "Delete from db complete. $c items removed.", array(), WATCHDOG_DEBUG);
  1704. variable_set("content_last_run_delete_flag_removal", time());
  1705. } // check against > last_run, so we should do it.
  1706. } // hook_cron
  1707. /**
  1708. * Display a list of content for the administrator
  1709. */
  1710. function content_display_content_admin_list() {
  1711. global $user;
  1712. $rtn = "";
  1713. //fp_add_css(fp_get_module_path("content") . "/css/content.css");
  1714. fp_set_title('');
  1715. $rtn .= "<br><b>" .t("Create new content:") . "</b>
  1716. <table border='1' width='100%' cellpadding='4'>";
  1717. // Go through all available content types and provide a link.
  1718. $types = content_get_types();
  1719. // We expect types to be returned like this:
  1720. // ["page"]["title"] = "Basic Page";
  1721. // ["page"]["description"] = "This is a standard page.";
  1722. foreach ($types as $type => $details) {
  1723. if (user_has_permission("add_$type" . "_content")) {
  1724. $rtn .= "<tr>
  1725. <td>" . l($details["title"], "content/add/$type") . "</td>
  1726. <td>" . $details["description"] . "</td></tr>";
  1727. }
  1728. }
  1729. $rtn .= "</table>
  1730. <hr>";
  1731. // Show a table of current content in the system.
  1732. $rtn .= "<b>" . t("Existing content:") . "</b>";
  1733. @$filter = $_GET["filter"];
  1734. $rtn .= "<form action='" . fp_url("admin-tools/content") . "' method='GET'>
  1735. " . t("Display type:") . " &nbsp; <select name='filter'>
  1736. <option value=''>" . t(" -- Any --") . "</option>";
  1737. foreach ($types as $type => $details) {
  1738. $sel = ($type == $filter) ? "selected" : "";
  1739. $rtn .= "<option value='$type' $sel>" . $details["title"] . "</option>";
  1740. }
  1741. $rtn .= "</select><input type='submit' value='-&gt;'> </form>";
  1742. $table_headers = array();
  1743. $table_headers[] = array("label" => "Actions");
  1744. $table_headers[] = array("label" => "Title", "field" => "n.title");
  1745. $table_headers[] = array("label" => "Type", "field" => "n.type");
  1746. $table_headers[] = array("label" => "Published", "field" => "n.published");
  1747. $table_headers[] = array("label" => "Author");
  1748. $table_headers[] = array("label" => "Updated", "field" => "n.updated");
  1749. // Set our initial sort, if none is already set.
  1750. theme_table_header_sortable_set_initial_sort('n.updated', 'DESC');
  1751. $rtn .= "<table width='100%' border='1'>";
  1752. // Draw our our table headers, with links....
  1753. $rtn .= theme_table_header_sortable($table_headers);
  1754. $params = array();
  1755. $filter_line = "";
  1756. if ($filter != "") {
  1757. $filter_line = "AND type = :ftype";
  1758. $params[":ftype"] = $filter;
  1759. }
  1760. $content_types = content_get_types();
  1761. // Only look for types which have been defined (nothing for modules we have since disabled)
  1762. $types_line = "AND `type` IN ('" . join("','", array_keys($content_types)) . "')";
  1763. // Get our order by clause based on selected table header, if any.
  1764. $order_by = theme_table_header_sortable_order_by($table_headers);
  1765. $res = pager_query("SELECT DISTINCT(cid) FROM content n
  1766. WHERE delete_flag = 0
  1767. $types_line
  1768. $filter_line
  1769. $order_by", $params, 20, 0, "SELECT COUNT(DISTINCT(cid)) FROM content n
  1770. WHERE delete_flag = 0
  1771. $types_line
  1772. $filter_line
  1773. $order_by", $params);
  1774. while ($cur = db_fetch_array($res)) {
  1775. $content = content_load($cur['cid']);
  1776. $author = fp_load_user($content->user_id);
  1777. $author_name = "";
  1778. if ($author && isset($author->name)) {
  1779. $author_name = $author->name;
  1780. }
  1781. $updated = format_date(convert_time($content->updated));
  1782. $view_link = $edit_link = "";
  1783. $view_link = l("<i class='fa fa-eye'></i>", "content/$content->cid");
  1784. if ((user_has_permission("edit_any_{$content->type}" . "_content")) || (user_has_permission("edit_own_{$content->type}" . "_content") && intval($content->user_id) === intval($user->id) && intval($user->id) > 0)) {
  1785. $edit_link = l("<i class='fa fa-pencil'></i>", "content/{$cur["cid"]}/edit");
  1786. }
  1787. $rtn .= "<tr>
  1788. <td>$view_link &nbsp; $edit_link</td>
  1789. <td valign='top'>" . $content->title . "</td>";
  1790. $rtn .= "
  1791. <td valign='top'>" . $content->type . "</td>
  1792. <td valign='top'>" . $content->published . "</td>
  1793. <td valign='top'>" . $author_name . "</td>
  1794. <td valign='top'>" . $updated . "</td>
  1795. </tr>";
  1796. }
  1797. $rtn .= "</table>";
  1798. // Display the pager that was generated by the pager_query above!
  1799. $rtn .= theme_pager();
  1800. return $rtn;
  1801. }
  1802. /**
  1803. * Return an array with all the possible content types known to FlightPath
  1804. *
  1805. * Modules may declare their content types using the hook_content_register_content_type hook.
  1806. * You should clear the cache after creating a new content type.
  1807. *
  1808. */
  1809. function content_get_types() {
  1810. $rtn = array();
  1811. // Save effort by saving to a globals cache.
  1812. if (isset($GLOBALS['content_types_cache'])) {
  1813. return $GLOBALS['content_types_cache'];
  1814. }
  1815. $modules = modules_implement_hook("content_register_content_type");
  1816. foreach($modules as $module) {
  1817. $types = call_user_func($module . '_content_register_content_type');
  1818. $rtn += $types;
  1819. }
  1820. $GLOBALS['content_types_cache'] = $rtn;
  1821. return $rtn;
  1822. }
  1823. /**
  1824. * Implementation of this module's hook_content_register_content_type.
  1825. *
  1826. * I mainly just want to register the "page" content type, for a basic
  1827. * web page set up.
  1828. *
  1829. */
  1830. function content_content_register_content_type() {
  1831. $arr['page'] = array(
  1832. 'title' => 'Page',
  1833. 'description' => 'This is a basic, publicly visible web page.',
  1834. );
  1835. $fields['body'] = array(
  1836. 'type' => 'textarea_editor',
  1837. 'label' => '',
  1838. 'filter' => 'basic',
  1839. );
  1840. $arr['page']['fields'] = $fields;
  1841. return $arr;
  1842. }
  1843. /**
  1844. * Return the HTML rendering the content we have in the database.
  1845. */
  1846. function content_render_content($bool_show_delete = FALSE) {
  1847. $rtn = "";
  1848. $res = db_query("SELECT * FROM content WHERE delete_flag = 0
  1849. ORDER BY posted DESC");
  1850. while ($cur = db_fetch_array($res)) {
  1851. $rtn .= "<div class='content'>
  1852. <div class='content-text'>" . filter_markup($cur["content"], "full") . "</div>
  1853. <div class='content-posted'>" . t("Posted") . " " . date("D, M jS Y - g:ia", $cur["posted"]) . "</div>";
  1854. if ($bool_show_delete) {
  1855. $delete_link = "";
  1856. $delete_link = fp_get_js_confirm_link(t("Are you sure you wish to delete this content?"), 'window.location="' . fp_url("content/delete", "aid=" . $cur["aid"]) . '"', t("Delete?"));
  1857. $rtn .= "<div class='content-delete'>$delete_link</div>";
  1858. }
  1859. $rtn .= "</div>";
  1860. }
  1861. return $rtn;
  1862. }
  1863. /**
  1864. * Implementation of hook_perm
  1865. *
  1866. * We want to create a permission for every content type.
  1867. *
  1868. */
  1869. function content_perm() {
  1870. $rtn = array();
  1871. $rtn["admin_content"] = array(
  1872. "title" => t("Administer Content"),
  1873. "description" => t("The user needs this permission to access the content page at all, in
  1874. addition to specific permissions listed below for editing
  1875. different content types."),
  1876. );
  1877. $rtn["admin_public_files"] = array(
  1878. "title" => t("Administer Public Files"),
  1879. "description" => t("The user is allowed to upload, modify, or delete 'public files' which are stored in a special location
  1880. on the web server. Only give this permission to admin or developer users."),
  1881. );
  1882. $types = content_get_types();
  1883. foreach ($types as $type => $details) {
  1884. $rtn ["add_$type" . "_content"] = array(
  1885. "title" => t("Add new") . " " . $details["title"] . " " . t("content"),
  1886. );
  1887. $rtn ["view_$type" . "_content"] = array(
  1888. "title" => "&nbsp; &nbsp; " . t("View published") . " " . $details["title"] . " " . t("content"),
  1889. );
  1890. $rtn ["edit_any_$type" . "_content"] = array(
  1891. "title" => "&nbsp; &nbsp; " . t("Edit any") . " " . $details["title"] . " " . t("content"),
  1892. );
  1893. $rtn ["edit_own_$type" . "_content"] = array(
  1894. "title" => "&nbsp; &nbsp; " . t("Edit own") . " " . $details["title"] . " " . t("content"),
  1895. );
  1896. $rtn ["delete_any_$type" . "_content"] = array(
  1897. "title" => "&nbsp; &nbsp; " . t("Delete any") . " " . $details["title"] . " " . t("content"),
  1898. );
  1899. $rtn ["delete_own_$type" . "_content"] = array(
  1900. "title" => "&nbsp; &nbsp; " . t("Delete own") . " " . $details["title"] . " " . t("content"),
  1901. );
  1902. }
  1903. return $rtn;
  1904. }
  1905. /**
  1906. * hook_blocks. Returns an array of available blocks offered by this module in this format:
  1907. * array(
  1908. * delta => "This is the title of the block.",
  1909. * ),
  1910. * );
  1911. *
  1912. * Delta can be just about anything machine-readable. Alphanumeric and underscores only.
  1913. * Ex: 0, 1, fun_2, etc.
  1914. *
  1915. */
  1916. function content_blocks() {
  1917. return array(
  1918. "primary" => t("Primary content block"),
  1919. );
  1920. }
  1921. /**
  1922. * Called when it is time to render the block in question.
  1923. * Expected to return an array which looks like this:
  1924. * array(
  1925. * "title" => "Some title goes here.",
  1926. * "body" => "this is the primary body of the block",
  1927. * );
  1928. */
  1929. function content_render_block($delta) {
  1930. $rtn = array();
  1931. if ($delta == "primary") {
  1932. fp_add_css(fp_get_module_path("content") . "/css/content.css");
  1933. $rtn["title"] = t("content");
  1934. $rtn["body"] = content_render_content();
  1935. }
  1936. return $rtn;
  1937. }

Functions

Namesort descending Description
content_add_new_uploaded_file bool_replace_existing means should we replace an existing file (based on filename) with this new one? Instead of creating an INSERT, that is.
content_blocks hook_blocks. Returns an array of available blocks offered by this module in this format: array( delta => "This is the title of the block.", ), );
content_content_load Implementation of content's hook_content_load
content_content_register_content_type Implementation of this module's hook_content_register_content_type.
content_content_save Implements hook_content_save. We will save the content object to the database.
content_cron hook_cron
content_dialog_handle_after_save This is the URL we redirect to after saving a piece of content in a dialog, so all we really want to do is close the dialog and reload the parent page.
content_display_content_admin_list Display a list of content for the administrator
content_display_devel Simply returns the fpm() results for the supplied content.
content_edit_content_form This form lets the user edit some piece of content
content_edit_content_form_submit Submit handler for the edit content form.
content_edit_content_form_validate
content_files_handle_download This actually finds and downloads the file for the user, decrypting if necessary.
content_files_user_may_download_file Returns TRUE or FALSE if the user has access to download this particular student's file.
content_form_alter Hook form alter, to set some defaults.
content_get_content_for_faculty_id This function will look for content (in the content tables) for a field called field__faculty_id, and return an array of fully loaded content objects.
content_get_fontawesome_icon_for_mimetype Find by mime type OR file extension.
content_get_last_access Returns the timestamp of last access, or ZERO if never accessed
content_get_types Return an array with all the possible content types known to FlightPath
content_get_uploaded_file
content_load Load the content from the database and return an array, by calling hook_content_load.
content_menu
content_menu_handle_replacement_pattern This is an implementation of hook_menu_handle_replacement_pattern. It will search for and replace replacement patterns which we are aware of it in $str.
content_perm Implementation of hook_perm
content_public_files_form This screen lets the user upload/manage/delete "public files" stored at custom/files/content_uploads/public_uploads/
content_public_files_form_submit
content_public_files_form_validate
content_render_block Called when it is time to render the block in question. Expected to return an array which looks like this: array( "title" => "Some title goes here.", "body" => "this is the primary body of the block", );
content_render_content Return the HTML rendering the content we have in the database.
content_save Save the content to the database.
content_set_last_access Sets the content_last_access timestamp for this user and content. Should be called whenever a "content" node is viewed by the user.
content_unpublish_content_form
content_unpublish_content_form_submit
content_user_access Custom user access function to determine if the user can add, edit, etc, the content
content_view_content Display the content specified in the GET's cid.
_content_generate_create_table_sql Meant to be run by admin, this generates table creation sql for a given type.

Constants