blocks.module

  1. 4.x modules/blocks/blocks.module
  2. 5.x modules/blocks/blocks.module

File

modules/blocks/blocks.module
View source
  1. <?php
  2. /**
  3. * This is the blocks module, which lets the user manage the location and appearance of blocks
  4. * in FlightPath.
  5. */
  6. function blocks_perm() {
  7. return array(
  8. "administer_blocks" => array(
  9. "title" => t("Administer blocks"),
  10. "description" => t("Allow the user to administer the location of blocks."),
  11. ),
  12. );
  13. }
  14. function blocks_menu() {
  15. $items = array();
  16. $items["admin/config/blocks"] = array(
  17. "title" => "Blocks",
  18. "description" => "Configure blocks of content and where they show up in FlightPath",
  19. "page_callback" => "fp_render_form",
  20. "page_arguments" => array("blocks_manage_blocks_form"),
  21. "access_arguments" => array("admin_blocks"),
  22. "page_settings" => array(
  23. "page_has_search" => FALSE,
  24. "page_banner_is_link" => TRUE,
  25. "page_hide_report_error" => TRUE,
  26. "menu_links" => array(
  27. 0 => array(
  28. "text" => "Back to main menu",
  29. "path" => "admin-tools/admin",
  30. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  31. ),
  32. ),
  33. ),
  34. "type" => MENU_TYPE_TAB,
  35. "tab_family" => "blocks_manage",
  36. );
  37. return $items;
  38. }
  39. /**
  40. * This form lets the user manage the various blocks in the system.
  41. */
  42. function blocks_manage_blocks_form() {
  43. $form = array();
  44. fp_add_css(fp_get_module_path("blocks") . "/css/blocks.css");
  45. $selected_section = $_REQUEST["section"];
  46. $selected_module = $_REQUEST["module"];
  47. // We should have sub-tabs for all the possible sections. To accomplish this,
  48. // we need to get a list of defined sections and regions.
  49. $regions = blocks_get_sections_and_regions();
  50. $tab_array = array();
  51. $c = 0;
  52. foreach ($regions as $module => $region_data) {
  53. foreach ($region_data as $section => $data) {
  54. if ($selected_section == "" && $c == 0) {
  55. // No selected section AND this is the first-- select it!
  56. $selected_section = $section;
  57. $selected_module = $module;
  58. }
  59. $is_active = ($selected_section == $section && $selected_module == $module) ? TRUE : FALSE;
  60. $tab_array[] = array(
  61. "title" => $data["title"],
  62. "active" => $is_active,
  63. "on_click" => 'window.location="' . fp_url("admin/config/blocks", "section=$section&module=$module") . '";',
  64. );
  65. $c++;
  66. }
  67. }
  68. fp_set_page_sub_tabs($tab_array);
  69. $form["module"] = array(
  70. "type" => "hidden",
  71. "value" => $selected_module,
  72. );
  73. $form["section"] = array(
  74. "type" => "hidden",
  75. "value" => $selected_section,
  76. );
  77. // Tell the user how they can use the content module, if it is enabled.
  78. if (function_exists("content_menu")) {
  79. $form["mark" . $m++] = array(
  80. "value" => t("To add your own custom blocks of generic content,
  81. use the 'content' module. ") . "<br>" . l(t("Add or edit content blocks"), "admin/config/content"),
  82. );
  83. }
  84. else {
  85. $form["mark" . $m++] = array(
  86. "value" => t("To add your own custom blocks of generic content,
  87. use the 'content' module. Have your administrator
  88. enable it to begin using it.")
  89. );
  90. }
  91. // Build the available region options.
  92. $region_options = array();
  93. $region_options[""] = t("- Disabled -");
  94. foreach ($regions[$selected_module][$selected_section]["regions"] as $region => $rtitle) {
  95. $region_options[$region] = $rtitle["title"];
  96. }
  97. $weight_options = array();
  98. for($t = -100; $t <= 100; $t++) {
  99. $weight_options[$t] = $t;
  100. }
  101. // Okay, now we need a list of available blocks in the system. We will list each one along with
  102. // a pulldown list (of available regions on this section) and a weight pulldown.
  103. $available_blocks = blocks_get_available_blocks();
  104. $form["mark" . $m++] = array(
  105. "value" => "<table class='blocks-table'>",
  106. );
  107. foreach($available_blocks as $module => $blocks) {
  108. $form["mark" . $m++] = array(
  109. "value" => "<tr>
  110. <td colspan='10' class='block-module-name'>$module</td>
  111. </tr>
  112. <tr>
  113. <th>" . t("Block") . "</th>
  114. <th>" . t("Region") . "</th>
  115. <th>" . t("Weight") . "</th>
  116. </tr>",
  117. );
  118. foreach ($blocks as $delta => $title) {
  119. $default_region = "";
  120. $default_weight = "";
  121. // is this module and delta currently assigned a particular weight or region in this section?
  122. $res = db_query("SELECT * FROM blocks WHERE section = '?' AND module = '?' AND delta = '?'
  123. ", $selected_section, $module, $delta);
  124. $cur = db_fetch_array($res);
  125. $default_region = $cur["region"];
  126. $default_weight = $cur["weight"];
  127. $form["block_region~~{$module}~~{$delta}"] = array(
  128. "type" => "select",
  129. "options" => $region_options,
  130. "prefix" => "<tr>
  131. <td>$title</td>
  132. <td>",
  133. "suffix" => "</td>",
  134. "value" => $default_region,
  135. "no_please_select" => TRUE,
  136. );
  137. $form["block_weight~~{$module}~~{$delta}"] = array(
  138. "type" => "select",
  139. "options" => $weight_options,
  140. "prefix" => "<td>",
  141. "suffix" => "</td>
  142. </tr>",
  143. "value" => $default_weight,
  144. "no_please_select" => TRUE,
  145. );
  146. }
  147. }
  148. $form["mark" . $m++] = array(
  149. "value" => "</table>",
  150. );
  151. $form["submit"] = array(
  152. "type" => "submit",
  153. "value" => t("Save block positions"),
  154. "prefix" => "<hr>",
  155. );
  156. return $form;
  157. }
  158. /**
  159. * Implementatin of content's hook_content_register_content_type
  160. *
  161. * @return unknown
  162. */
  163. function blocks_content_register_content_type() {
  164. $arr = array(
  165. "content_block" => array(
  166. "title" => t("Content Block"),
  167. "description" => t("This is a piece of content which can be used as a Block on the Blocks page."),
  168. "settings" => array(),
  169. ),
  170. );
  171. // In the settings region, I want some extra fields which will be used
  172. // when we display.
  173. $arr["content_block"]["settings"]["mobile_display_mode"] = array(
  174. "type" => "select",
  175. "label" => t("Mobile display mode"),
  176. "options" => array("show" => t("Display"), "hide" => t("Do not show"), "collapse" => t("Collapse into fieldset")),
  177. "description" => t("When viewing on a mobile device, how should this block display?
  178. If you are unsure what to select, leave it set to 'Display'."),
  179. "no_please_select" => TRUE,
  180. );
  181. return $arr;
  182. }
  183. /**
  184. * Implementation of hook_blocks
  185. *
  186. * We want to provide every "content_block" piece of content as a block.
  187. *
  188. * @return unknown
  189. */
  190. function blocks_blocks() {
  191. $rtn = array();
  192. $res = db_query("SELECT * FROM content WHERE type = 'content_block'
  193. ORDER BY title");
  194. while ($cur = db_fetch_array($res)) {
  195. $rtn["block_" . $cur["cid"]] = $cur["title"];
  196. }
  197. return $rtn;
  198. }
  199. /**
  200. * Implementation of hook_render_block. We are going
  201. * to render out our content_block content type.
  202. *
  203. * @param unknown_type $delta
  204. */
  205. function blocks_render_block($delta) {
  206. $temp = explode("_", $delta);
  207. $cid = $temp[1];
  208. $content = content_load($cid);
  209. $block = array();
  210. if (!strstr($content["title"], "[hide]")) {
  211. $block["title"] = $content["title"];
  212. }
  213. $block["body"] = $content["body"];
  214. // What type of mobile_display_mode has been set?
  215. if (fp_screen_is_mobile()) {
  216. if ($content["settings"]["mobile_display_mode"] == "hide") {
  217. return array(); // return an empty array
  218. }
  219. else if ($content["settings"]["mobile_display_mode"] == "collapse") {
  220. // Place the content in a collapsible fieldset.
  221. $block["body"] = fp_render_c_fieldset($block["body"], t("Press to read") . " " . $content["title"], TRUE);
  222. }
  223. }
  224. return $block;
  225. }
  226. /**
  227. * Example of hook_blocks.
  228. *
  229. * Modules wishing to provide blocks should implement this function.
  230. * The purpose is to describe which blocks you can render, by returning an array
  231. * as shown. The index of the array is the "delta" which hook_render_block($delta) will
  232. * use when deciding how to render the block on the screen.
  233. *
  234. * @see hook_render_block
  235. *
  236. */
  237. function hook_blocks() {
  238. return array(
  239. "b1" => t("Tools menu"),
  240. "admin_tools" => t("Admin Tools menu"),
  241. "login_form" => t("Login form"),
  242. );
  243. }
  244. /**
  245. * Example of hook_render_block
  246. *
  247. * Modules wishing to provide blocks should implement this function as well
  248. * as hook_blocks. It is expected that you will return an array describing
  249. * how to draw the block on the screen, as shown below. The "delta" is
  250. * the same as the index of the array element defined in hook_blocks().
  251. *
  252. * @param unknown_type $delta
  253. *
  254. * @see hook_blocks
  255. */
  256. function hook_render_block($delta) {
  257. $block = array();
  258. if ($delta == "tools") {
  259. $block["title"] = t("Tools");
  260. $block["body"] = fp_render_menu_block("", "tools");
  261. }
  262. if ($delta == "admin_tools") {
  263. $block["title"] = t("Administrative Tools");
  264. $block["body"] = fp_render_menu_block("", "admin-tools");
  265. }
  266. if ($delta == "login_form") {
  267. $block["title"] = t("Please log in below...");
  268. $block["body"] = fp_render_form("system_login_form");
  269. }
  270. // We don't want empty blocks to show up at all.
  271. if ($block["body"] == "") {
  272. return FALSE;
  273. }
  274. return $block;
  275. }
  276. /**
  277. * Submit handler for the manage blocks form.
  278. */
  279. function blocks_manage_blocks_form_submit($form, $form_state) {
  280. $selected_module = $form_state["values"]["module"];
  281. $selected_section = $form_state["values"]["section"];
  282. // begin by deleteing what is all ready there, if anything, for this section.
  283. db_query("DELETE FROM blocks WHERE section = '?' ", $selected_section);
  284. foreach ($form_state["values"] as $key => $value) {
  285. if (strstr($key, "block_region~~")) {
  286. $temp = explode("~~", $key);
  287. $module = $temp[1];
  288. $delta = $temp[2];
  289. // Okay, let's see where it was placed, and update our blocks table.
  290. $region = $form_state["values"]["block_region~~$module~~$delta"];
  291. $weight = $form_state["values"]["block_weight~~$module~~$delta"];
  292. if ($region != "") {
  293. db_query("INSERT INTO blocks (section, region, module, delta, weight)
  294. VALUES ('?', '?', '?', '?', '?')
  295. ", $selected_section, $region, $module, $delta, $weight);
  296. }
  297. }
  298. }
  299. fp_add_message(t("Blocks have been updated successfully."));
  300. }
  301. /**
  302. * Return an array of blocks which we can assign and display, as defined by other modules' hook_blocks
  303. * function.
  304. */
  305. function blocks_get_available_blocks() {
  306. $rtn = array();
  307. $modules = modules_implement_hook("blocks");
  308. foreach($modules as $module) {
  309. $arr = call_user_func($module . "_blocks");
  310. $rtn[$module] = $arr;
  311. }
  312. return $rtn;
  313. }
  314. /**
  315. * Look through our modules for hook_block_region and assemble them in an array.
  316. */
  317. function blocks_get_sections_and_regions() {
  318. $rtn = array();
  319. $modules = modules_implement_hook("block_regions");
  320. foreach($modules as $module) {
  321. $arr = call_user_func($module . "_block_regions");
  322. $rtn[$module] = $arr;
  323. }
  324. return $rtn;
  325. }
  326. /**
  327. * This function will actually render the HTML for the blocks requested for a particular section
  328. * and region.
  329. */
  330. function blocks_render_blocks($section, $region, $title_style = "curved") {
  331. $rtn = "";
  332. $rtn .= "<div class='blocks-blocks'>";
  333. $c = 0;
  334. $res = db_query("SELECT * FROM blocks WHERE section = '?' AND region = '?'
  335. ORDER BY weight", $section, $region);
  336. while ($cur = db_fetch_array($res)) {
  337. $module = $cur["module"];
  338. $delta = $cur["delta"];
  339. $block = FALSE;
  340. if (function_exists($module . "_render_block")) {
  341. $block = call_user_func($module . "_render_block", $delta);
  342. }
  343. if (!$block) {
  344. continue;
  345. }
  346. $extra_class = "";
  347. if ($c == 0) $extra_class = " block-first";
  348. $rtn .= "<div class='block-$module-$delta $extra_class'>";
  349. if ($block["title"] != "") {
  350. if ($title_style == "curved") {
  351. $rtn .= fp_render_curved_line($block["title"]);
  352. }
  353. else if ($title_stule == "square") {
  354. $rtn .= fp_render_square_line($block["title"]);
  355. }
  356. }
  357. $rtn .= "<div class='block-body'>" . $block["body"] . "</div>";
  358. $rtn .= "</div>";
  359. $c++;
  360. }
  361. $rtn .= "</div>";
  362. return $rtn;
  363. }

Functions

Namesort descending Description
blocks_blocks Implementation of hook_blocks
blocks_content_register_content_type Implementatin of content's hook_content_register_content_type
blocks_get_available_blocks Return an array of blocks which we can assign and display, as defined by other modules' hook_blocks function.
blocks_get_sections_and_regions Look through our modules for hook_block_region and assemble them in an array.
blocks_manage_blocks_form This form lets the user manage the various blocks in the system.
blocks_manage_blocks_form_submit Submit handler for the manage blocks form.
blocks_menu
blocks_perm
blocks_render_block Implementation of hook_render_block. We are going to render out our content_block content type.
blocks_render_blocks This function will actually render the HTML for the blocks requested for a particular section and region.
hook_blocks Example of hook_blocks.
hook_render_block Example of hook_render_block