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

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