tripal_panes.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. /**
  3. * Implements hook_form_init();
  4. */
  5. function tripal_panes_init() {
  6. $theme_dir = url(drupal_get_path('module', 'tripal_panes') . '/theme');
  7. drupal_add_js("var panes_theme_dir = '$theme_dir';", 'inline', 'header');
  8. }
  9. /**
  10. * Implements hook_form_FORM_ID_alter().
  11. *
  12. * The field_ui_display_overview_form is used for formatting the display
  13. * or layout of fields attached to an entity.
  14. */
  15. function tripal_panes_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  16. drupal_add_js(drupal_get_path('module','tripal_panes') . '/theme/js/tripal_panes.admin.js');
  17. drupal_set_message('For the Tripal v3.0a release there are two bugs to keep in mind. Dragging fields outside of a their assigned pane does not yet work. Use the "Region" select box instead. Also, dragging a field may set it\'s format to "hidden" in which case you will want to set it back', 'warning');
  18. drupal_add_css(drupal_get_path('module','tripal_panes') . '/theme/css/tripal_panes.css');
  19. $entity_type = $form['#entity_type'];
  20. $bundle_name = $form['#bundle'];
  21. // Get the bundle record.
  22. $bundle = db_select('tripal_bundle', 'tb')
  23. ->fields('tb')
  24. ->condition('name', $bundle_name)
  25. ->execute()
  26. ->fetchObject();
  27. if (module_exists('ds')) {
  28. drupal_set_message('Tripal is not compatible with the Display Suite (ds)
  29. module. If you would like to use Tripal-style panes for the layout
  30. of your pages please disable the Display Suite module. If you
  31. prefer to use the Display Suite module then disable the Tripal
  32. Panes (tripal_panes) module.', 'warning');
  33. }
  34. // Add a vertical tab fieldset at the bottom of the
  35. $form['overview_vert_tabs'] = array(
  36. '#type' => 'vertical_tabs'
  37. );
  38. $form['modes']['#group'] = 'overview_vert_tabs';
  39. $form['modes']['#weight'] = 1000;
  40. $form['te_add_panes'] = array(
  41. '#type' => 'fieldset',
  42. '#title' => 'Add a Pane',
  43. '#collapsible' => TRUE,
  44. '#collapsed' => TRUE,
  45. '#group' => 'overview_vert_tabs'
  46. );
  47. // Make sure our default pane are in the database.
  48. tripal_panes_check_default_field_panes($bundle);
  49. // Adds form elments as a vertical tab for adding a pane.
  50. tripal_panes_form_field_ui_display_overview_form_pane_add($form, $form_state);
  51. // Adds form elments as a vertical tab for arranging panes.
  52. tripal_panes_form_field_ui_display_overview_form_pane_arrange($form, $form_state, $bundle);
  53. // Adds form elments as a vertical tab for configuring panes.
  54. tripal_panes_form_field_ui_display_overview_form_pane_configure($form, $form_state, $bundle);
  55. // Now add each pane as a region.
  56. $form['fields']['#regions'] = array();
  57. $panes = db_select('tripal_panes', 'tp')
  58. ->fields('tp')
  59. ->condition('bundle_id', $bundle->id)
  60. ->orderBy('weight', 'ASC')
  61. ->orderBy('label', 'ASC')
  62. ->execute();
  63. $pane_options = array();
  64. while ($pane = $panes->fetchObject()) {
  65. $settings = unserialize($pane->settings);
  66. $form['fields']['#regions'][$pane->name] = array(
  67. 'title' => t($pane->label),
  68. 'message' => t($settings['message']),
  69. );
  70. $pane_options[$pane->name] = $pane->label;
  71. }
  72. // Set the table headers to add a new 'region' column.
  73. $form['fields']['#header'] = array(
  74. t('Field'),
  75. t('Weight'),
  76. t('Parent'),
  77. t('Label'),
  78. array('data' => t('Format'), 'colspan' => 3),
  79. t('Region')
  80. );
  81. // Change the region callback for each field to place each field in the
  82. // proper "pane" region. Also, set the default region to be the base region.
  83. $fields = $form['fields'];
  84. $default_pane = 'te_base';
  85. foreach (element_children($fields) as $field_name) {
  86. $field_instance = field_info_instance($entity_type, $field_name, $bundle_name);
  87. $pane_id = db_select('tripal_pane_fields', 'tpf')
  88. ->fields('tpf', array('pane_id'))
  89. ->condition('field_id', $field_instance['id'])
  90. ->execute()
  91. ->fetchField();
  92. if ($pane_id) {
  93. $default_pane = db_select('tripal_panes', 'tp')
  94. ->fields('tp', array('name'))
  95. ->condition('pane_id', $pane_id)
  96. ->execute()
  97. ->fetchField();
  98. }
  99. $form['fields'][$field_name]['#region_callback'] = 'tripal_panes_field_ui_row_region';
  100. $form['fields'][$field_name]['region'] = array(
  101. '#type' => 'select',
  102. '#options' => $pane_options,
  103. '#default_value' => $default_pane,
  104. '#attributes' => array(
  105. 'class' => array('te-field-region'),
  106. )
  107. );
  108. $form['fields'][$field_name]['#field_instance_id'] = array(
  109. '#type' => 'value',
  110. '#value' => $field_instance['id']
  111. );
  112. }
  113. // Add validate and submit handlers. Rearrange the submit callbacks
  114. // so ours is first.
  115. $form['#validate'][] = 'tripal_panes_field_ui_validate';
  116. $submit = $form['#submit'];
  117. $form['#submit'] = array('tripal_panes_field_ui_submit');
  118. $form['#submit'] = array_merge($form['#submit'], $submit);
  119. //dpm($form);
  120. }
  121. /**
  122. * Add a pane Form
  123. */
  124. function tripal_panes_form_field_ui_display_overview_form_pane_add (&$form, &$form_state) {
  125. $form_state['input']['pane_label'] = key_exists('pane_label', $form_state['input']) ? $form_state['values']['pane_label'] : NULL;
  126. $form['te_add_panes']['instructions'] = array(
  127. '#type' => 'item',
  128. '#markup' => t('You may add as many panes to your page layout as
  129. desired. panes can be used to organize fields into categories.')
  130. );
  131. $form['te_add_panes']['pane_name'] = array(
  132. '#type' => 'hidden',
  133. '#title' => 'pane Name',
  134. '#description' => t('The name is automatically generated for a label. it
  135. contains alphanumeric values and underscores and does not begin with
  136. a number.')
  137. );
  138. $form['te_add_panes']['pane_label'] = array(
  139. '#type' => 'textfield',
  140. '#title' => 'Pane Label',
  141. '#default_value' => '',
  142. '#description' => t('Please provide a human readable label for this
  143. pane. This is the name that will appear to site visitors.')
  144. );
  145. $form['te_add_panes']['add_button'] = array(
  146. '#type' => 'submit',
  147. '#value' => 'Add pane',
  148. '#name' => 'add-pane-submit'
  149. );
  150. }
  151. /**
  152. * Arrange panes Form
  153. */
  154. function tripal_panes_form_field_ui_display_overview_form_pane_arrange (&$form, &$form_state, $bundle) {
  155. $form['te_arrange_panes'] = array(
  156. '#type' => 'fieldset',
  157. '#title' => 'Arrange Panes',
  158. '#collapsible' => TRUE,
  159. '#collapsed' => TRUE,
  160. '#group' => 'overview_vert_tabs'
  161. );
  162. $form['te_arrange_panes']['instructions'] = array(
  163. '#type' => 'item',
  164. '#markup' => t('Drag and drop the pane to change its order.')
  165. );
  166. $form['te_arrange_panes']['pane_items']['#tree'] = TRUE;
  167. // Get available panes
  168. $result = db_select('tripal_panes', 'tp')
  169. ->fields('tp', array('pane_id', 'name', 'label', 'weight'))
  170. ->condition('name', 'te_base', '<>')
  171. ->condition('name', 'te_disabled', '<>')
  172. ->condition('bundle_id', $bundle->id)
  173. ->orderby('weight', 'asc')
  174. ->execute();
  175. $has_pane = FALSE;
  176. foreach ($result as $item) {
  177. $form_state['input']['pane_items'][$item->pane_id]['newlabel'] = key_exists($item->pane_id, $form_state['input']) && $form_state['input'][$item->pane_id]['newlabel'] ? $form_state['values']['pane_items'][$item->pane_id]['newlabel'] : NULL;
  178. $form['te_arrange_panes']['pane_items'][$item->pane_id] = array(
  179. 'label' => array(
  180. '#markup' => check_plain($item->label),
  181. ),
  182. 'weight' => array(
  183. '#type' => 'weight',
  184. '#title' => t('Weight'),
  185. '#default_value' => $item->weight,
  186. '#delta' => 50,
  187. '#title_display' => 'invisible',
  188. ),
  189. 'newlabel' => array(
  190. '#type' => 'textfield',
  191. '#default_value' => '',
  192. '#size' => 10
  193. ),
  194. 'rename' => array(
  195. '#type' => 'submit',
  196. '#value' => 'Rename',
  197. '#name' => "arrange-pane-rename-$item->pane_id",
  198. ),
  199. 'remove' => array(
  200. '#type' => 'submit',
  201. '#value' => 'Remove',
  202. '#name' => "arrange-pane-remove-$item->pane_id",
  203. )
  204. );
  205. $has_pane = TRUE;
  206. }
  207. if ($has_pane) {
  208. $form['te_arrange_panes']['pane_items']['#theme_wrappers'] = array('tripal_panes_form_arrange_panes');
  209. $form['te_arrange_panes']['save_button'] = array(
  210. '#type' => 'submit',
  211. '#value' => 'Save pane Order',
  212. '#name' => 'order-pane-submit'
  213. );
  214. }
  215. else {
  216. $form['te_arrange_panes']['instructions']['#markup'] = t('A pane must be added before any arrangements can be made.');
  217. }
  218. }
  219. /**
  220. * Configure panes Form
  221. */
  222. function tripal_panes_form_field_ui_display_overview_form_pane_configure (&$form, &$form_state, $bundle) {
  223. $form['te_configure_panes'] = array(
  224. '#type' => 'fieldset',
  225. '#title' => 'Configure Panes',
  226. '#collapsible' => TRUE,
  227. '#collapsed' => TRUE,
  228. '#group' => 'overview_vert_tabs'
  229. );
  230. // Add a dropdown for selecting pane to configure
  231. $form['te_configure_panes']['pane_select'] = array(
  232. '#type' => 'select',
  233. '#title' => t('Pane Name'),
  234. '#description' => t('Select a pane to change its layout. Fields can be grouped into a table if Table layout is selected.'),
  235. '#ajax' => array(
  236. 'callback' => 'tripal_panes_ajax_get_pane_setting_fieldset',
  237. 'wrapper' => 'tripal-fields-layout-pane-setting',
  238. 'effect' => 'fade'
  239. )
  240. );
  241. $form['te_configure_panes']['pane_items'] = array (
  242. '#tree' => TRUE,
  243. '#prefix' => '<div id="tripal-fields-layout-pane-setting">',
  244. '#suffix' => '</div>'
  245. );
  246. // Get available panes
  247. $panes = db_select('tripal_panes', 'tp')
  248. ->fields('tp', array('pane_id', 'name', 'label', 'weight', 'settings'))
  249. ->condition('name', 'te_disabled', '<>')
  250. ->condition('bundle_id', $bundle->id)
  251. ->orderby('weight', 'asc')
  252. ->execute();
  253. $has_pane = FALSE;
  254. $options = array(0 => 'Select a Pane');
  255. $selected_pane = key_exists('values', $form_state) ? $form_state['values']['pane_select'] : 0;
  256. foreach ($panes as $pane) {
  257. $options[$pane->pane_id] = $pane->label;
  258. $has_pane = TRUE;
  259. $pane_settings = unserialize($pane->settings);
  260. $table_layout = key_exists('table_layout', $pane_settings) ? $pane_settings['table_layout'] : array();
  261. $form['te_configure_panes']['pane_items'][$pane->pane_id] = array(
  262. '#type' => 'fieldset',
  263. '#title' => check_plain($pane->label),
  264. '#collapsible' => TRUE,
  265. '#collapsed' => FALSE,
  266. );
  267. //Add fields to each pane for changing configuration
  268. $fields = db_select('tripal_pane_fields', 'tpf')
  269. ->fields('tpf', array('field_id'))
  270. ->condition('pane_id', $pane->pane_id)
  271. ->execute();
  272. $has_field = FALSE;
  273. foreach ($fields AS $field) {
  274. $field_obj = db_select('field_config_instance', 'tci')
  275. ->fields('tci', array('field_name','data'))
  276. ->condition('id', $field->field_id)
  277. ->execute()
  278. ->fetchObject();
  279. $field_arr = unserialize($field_obj->data);
  280. $fname = $field_obj->field_name;
  281. if($fname == 'featureprop') {
  282. continue;
  283. }
  284. $flable = $field_arr['label'];
  285. $form['te_configure_panes']['pane_items'][$pane->pane_id][$fname] = array(
  286. '#type' => 'item',
  287. '#title' => $flable,
  288. '#weight' => $field_arr['display']['default']['weight']
  289. );
  290. $default_value = 0;
  291. if (in_array($fname, $table_layout)) {
  292. $default_value = 1;
  293. }
  294. $form['te_configure_panes']['pane_items'][$pane->pane_id][$fname]['table_group'] = array(
  295. '#type' => 'radios',
  296. '#options' => array(
  297. 0 => 'Default',
  298. 1 => 'Table',
  299. ),
  300. '#default_value' => $default_value,
  301. );
  302. $has_field = TRUE;
  303. }
  304. if (!$has_field) {
  305. $form['te_configure_panes']['pane_items'][$pane->pane_id]['no_field'] = array(
  306. '#markup' => 'No field in this pane.',
  307. );
  308. } else {
  309. $form['te_configure_panes']['pane_items'][$pane->pane_id]['#theme_wrappers'] = array('tripal_panes_form_configure_panes');
  310. }
  311. if ($pane->pane_id != $selected_pane) {
  312. $form['te_configure_panes']['pane_items'][$pane->pane_id]['#prefix'] = "<div style=\"display:none;\">";
  313. $form['te_configure_panes']['pane_items'][$pane->pane_id]['#suffix'] = "</div>";
  314. //continue; // Display only the selected pane setting
  315. }
  316. }
  317. $form['te_configure_panes']['pane_select']['#options'] = $options;
  318. if ($has_pane) {
  319. $form['te_configure_panes']['save_button'] = array(
  320. '#type' => 'submit',
  321. '#value' => 'Save pane Configuration',
  322. '#name' => 'configure-pane-submit'
  323. );
  324. }
  325. else {
  326. $form['te_configure_panes']['instructions']['#markup'] = t('A pane must be added before any configuration changes can be made.');
  327. }
  328. }
  329. /**
  330. * A helper function for checking if the default panes are in the database.
  331. */
  332. function tripal_panes_check_default_field_panes($bundle) {
  333. // Make sure we have records for our default regions: te_base and te_hidden.
  334. // First check if the base region is in the database. If not, add it.
  335. $te_base = db_select('tripal_panes', 'tp')
  336. ->fields('tp')
  337. ->condition('name', 'te_base')
  338. ->condition('bundle_id', $bundle->id)
  339. ->execute()
  340. ->fetchObject();
  341. if (!$te_base) {
  342. $settings = array(
  343. 'message' => 'Fields that should appear at the top of each page should be placed in the base pane. These fields are always present and help orient the user by at least indicating the necessary information to uniquely identiy the content.',
  344. );
  345. db_insert('tripal_panes')
  346. ->fields(array(
  347. 'bundle_id' => $bundle->id,
  348. 'name' => 'te_base',
  349. 'label' => 'Base Content',
  350. 'settings' => serialize($settings),
  351. 'weight' => -9999999
  352. ))
  353. ->execute();
  354. }
  355. // Now add all fields to the default region if they are not assigned
  356. $pane_id = db_select('tripal_panes', 'tp')
  357. ->fields('tp', array('pane_id'))
  358. ->condition('name', 'te_base')
  359. ->condition('bundle_id', $bundle->id)
  360. ->execute()
  361. ->fetchField();
  362. if(property_exists($bundle, 'type')) {
  363. $fields = db_select('field_config_instance', 'fci')
  364. ->fields('fci', array('id'))
  365. ->condition('entity_type', $bundle->type)
  366. ->condition('bundle', $bundle->name)
  367. ->execute();
  368. foreach($fields AS $field) {
  369. $penal_field_id = db_select('tripal_pane_fields', 'tpf')
  370. ->fields('tpf', array('pane_field_id'))
  371. ->condition('field_id', $field->id)
  372. ->execute()
  373. ->fetchField();
  374. if (!$penal_field_id) {
  375. db_insert('tripal_pane_fields')
  376. ->fields(array(
  377. 'pane_id' => $pane_id,
  378. 'field_id' => $field->id
  379. ))
  380. ->execute();
  381. }
  382. }
  383. }
  384. // Next check if the hidden region is in the database. If not, add it.
  385. $te_base = db_select('tripal_panes', 'tp')
  386. ->fields('tp')
  387. ->condition('name', 'te_disabled')
  388. ->condition('bundle_id', $bundle->id)
  389. ->execute()
  390. ->fetchObject();
  391. if (!$te_base) {
  392. $settings = array(
  393. 'message' => 'Place field here to hide them from display.',
  394. );
  395. db_insert('tripal_panes')
  396. ->fields(array(
  397. 'bundle_id' => $bundle->id,
  398. 'name' => 'te_disabled',
  399. 'label' => 'Disabled',
  400. 'settings' => serialize($settings),
  401. 'weight' => 9999999
  402. ))
  403. ->execute();
  404. }
  405. }
  406. /**
  407. * Returns the region to which a row on the Field UI page belongs.
  408. *
  409. * @param $row
  410. * The current row that is being rendered in the Field UI page.
  411. */
  412. function tripal_panes_field_ui_row_region($row) {
  413. $default_pane = 'te_base';
  414. $pane = '';
  415. $field_instance_id = $row['#field_instance_id']['#value'];
  416. // Get pane_id
  417. $pane_id = db_select('tripal_pane_fields', 'tpf')
  418. ->fields('tpf', array('pane_id'))
  419. ->condition('field_id', $field_instance_id)
  420. ->execute()
  421. ->fetchField();
  422. // Get pane name
  423. if ($pane_id) {
  424. $pane = db_select('tripal_panes', 'tp')
  425. ->fields('tp', array('name'))
  426. ->condition('pane_id', $pane_id)
  427. ->execute()
  428. ->fetchField();
  429. }
  430. // First get the pane
  431. if (!$pane) {
  432. $pane = $default_pane;
  433. }
  434. return $pane;
  435. }
  436. /**
  437. * Validates the field UI form to ensure proper pane assignments.
  438. *
  439. * @param $form
  440. * @param $form_state
  441. */
  442. function tripal_panes_field_ui_validate(&$form, &$form_state) {
  443. }
  444. /**
  445. * Responds to a submit from the field UI form for saving pane assignments.
  446. *
  447. * @param $form
  448. * @param $form_state
  449. */
  450. function tripal_panes_field_ui_submit($form, &$form_state) {
  451. // Add a pane
  452. if ($form_state ['clicked_button'] ['#name'] == 'add-pane-submit') {
  453. tripal_panes_action_add_pane($form, $form_state);
  454. }
  455. // Order panes
  456. else if ($form_state['clicked_button'] ['#name'] == 'order-pane-submit') {
  457. tripal_panes_action_order_panes($form, $form_state);
  458. }
  459. // Rename a pane
  460. else if (preg_match('/^arrange-pane-rename-/', $form_state ['clicked_button'] ['#name'])) {
  461. tripal_panes_action_rename_pane ($form, $form_state);
  462. }
  463. // Remove a pane
  464. else if (preg_match('/^arrange-pane-remove-/', $form_state ['clicked_button'] ['#name'])) {
  465. tripal_panes_action_remove_pane ($form_state);
  466. }
  467. // Configure panes
  468. else if ($form_state['clicked_button'] ['#name'] == 'configure-pane-submit') {
  469. tripal_panes_action_configure_panes($form, $form_state);
  470. }
  471. // Save fields for each pane
  472. else if ($form_state['clicked_button'] ['#name'] == 'op') {
  473. $bundle = $form_state['build_info']['args'][1];
  474. $fields = $form_state['values']['fields'];
  475. foreach($fields AS $field_name => $field_data){
  476. // Get field instance id
  477. $field_instance_id = $form['fields'][$field_name]['#field_instance_id']['#value'];
  478. // Get region pane_id
  479. $region = $field_data['region'];
  480. $pane_id = db_select('tripal_panes', 'tp')
  481. ->fields('tp', array('pane_id'))
  482. ->condition('name', $region)
  483. ->condition('bundle_id', $bundle->id)
  484. ->execute()
  485. ->fetchField();
  486. // Save
  487. $penal_field_id = db_select('tripal_pane_fields', 'tpf')
  488. ->fields('tpf', array('pane_field_id'))
  489. ->condition('field_id', $field_instance_id)
  490. ->execute()
  491. ->fetchField();
  492. if ($penal_field_id) {
  493. db_update('tripal_pane_fields')
  494. ->fields(array(
  495. 'pane_id' => $pane_id,
  496. ))
  497. ->condition('pane_field_id', $penal_field_id)
  498. ->execute();
  499. }
  500. else {
  501. db_insert('tripal_pane_fields')
  502. ->fields(array(
  503. 'pane_id' => $pane_id,
  504. 'field_id' => $field_instance_id
  505. ))
  506. ->execute();
  507. }
  508. }
  509. }
  510. }
  511. /**
  512. * Add a new pane
  513. */
  514. function tripal_panes_action_add_pane (&$form, &$form_state) {
  515. // Check if a pane label is provided
  516. $label = $form_state ['values'] ['pane_label'];
  517. if (! $label) {
  518. form_set_error('pane_label', t ("Please provide a label for the new pane."));
  519. }
  520. // Generate a valide pane name using the label.
  521. // i.e. removing leading numbers and spaces
  522. $name = strtolower(preg_replace ('/^\d|\s+/', '_', $label));
  523. $bundle = $form_state['build_info']['args'][1];
  524. $name_exists = db_select('tripal_panes', 'tp')
  525. ->fields('tp', array('pane_id'))
  526. ->condition('name', $name)
  527. ->condition('bundle_id', $bundle->id)
  528. ->execute()
  529. ->fetchField();
  530. if ($name_exists) {
  531. form_set_error('pane_label', t('The name is used by another pane. Please use a different label.'));
  532. }
  533. else {
  534. $form_state ['values'] ['pane_name'] = $name;
  535. $bundle_id = $form_state['build_info']['args'][1]->id;
  536. $name = $form_state['values']['pane_name'];
  537. $label = $form_state['values']['pane_label'];
  538. $settings = array(
  539. 'message' => 'Place field in this pane.',
  540. );
  541. db_insert('tripal_panes')
  542. ->fields(array(
  543. 'bundle_id' => $bundle_id,
  544. 'name' => $name,
  545. 'label' => $label,
  546. 'settings' => serialize($settings),
  547. 'weight' => 0
  548. ))
  549. ->execute();
  550. form_set_value($form['te_add_panes']['pane_label'], '', $form_state);
  551. }
  552. }
  553. /**
  554. * Rename pane
  555. */
  556. function tripal_panes_action_rename_pane (&$form, &$form_state) {
  557. $button = $form_state ['triggering_element'] ['#name'];
  558. $pane_id = str_replace ('arrange-pane-rename-', '', $button);
  559. $newlabel = $form_state['values']['pane_items'][$pane_id]['newlabel'];
  560. if (!trim($newlabel)) {
  561. form_set_error ('pane_label', t ("Please provide a label to rename a pane.") );
  562. }
  563. else {
  564. db_update('tripal_panes')
  565. ->fields(array(
  566. 'label' => $newlabel,
  567. 'name' => strtolower(preg_replace ('/^\d|\s+/', '_', $newlabel))
  568. ))
  569. ->condition('pane_id', $pane_id)
  570. ->execute();
  571. form_set_value($form['te_arrange_panes']['pane_items'][$pane_id]['newlabel'], '', $form_state);
  572. }
  573. }
  574. /**
  575. * Remove pane
  576. */
  577. function tripal_panes_action_remove_pane (&$form_state) {
  578. $button = $form_state ['triggering_element'] ['#name'];
  579. $pane_id = str_replace ('arrange-pane-remove-', '', $button);
  580. db_delete('tripal_panes')
  581. ->condition ('pane_id', $pane_id)
  582. ->execute();
  583. db_delete('tripal_pane_fields')
  584. ->condition ('pane_id', $pane_id)
  585. ->execute();
  586. }
  587. /**
  588. * Order pane
  589. */
  590. function tripal_panes_action_order_panes (&$form, &$form_state) {
  591. $panes = $form_state['values']['pane_items'];
  592. foreach ($panes AS $id => $pane) {
  593. if (key_exists('weight', $pane)) {
  594. db_query('UPDATE {tripal_panes} SET weight = :weight WHERE pane_id = :id',
  595. array(
  596. ':weight' => $pane['weight'],
  597. ':id' => $id
  598. )
  599. );
  600. }
  601. }
  602. }
  603. /**
  604. * Theme the Arrange panes as a draggable table
  605. *
  606. * @param $variables
  607. * @return
  608. */
  609. function theme_tripal_panes_form_arrange_panes ($variables) {
  610. $element = $variables['element'];
  611. $rows = array();
  612. foreach (element_children($element) as $id) {
  613. // Add a custom class that can be identified by 'drupal_add_tabledrag'
  614. $element[$id]['weight']['#attributes']['class'] = array('tripal_pane-item-weight');
  615. $element[$id]['label']['#printed'] = FALSE;
  616. $element[$id]['weight']['#printed'] = FALSE;
  617. $element[$id]['newlabel']['#printed'] = FALSE;
  618. $element[$id]['rename']['#printed'] = FALSE;
  619. $element[$id]['remove']['#printed'] = FALSE;
  620. $rows[] = array(
  621. 'data' => array(
  622. drupal_render($element[$id]['label']),
  623. drupal_render($element[$id]['weight']),
  624. drupal_render($element[$id]['newlabel']),
  625. drupal_render($element[$id]['rename']),
  626. drupal_render($element[$id]['remove'])
  627. ),
  628. // Add draggable to each row to support the tabledrag behaviour
  629. 'class' => array('draggable'),
  630. );
  631. }
  632. // Create table header
  633. $header = array(t('pane'), t('Weight'), array('data' => t('Rename'), 'colspan' => 2), t('Action'));
  634. // Create a unique id for drupal_add_tabledrag() to find the table object
  635. $table_id = 'tripal_pane-arrange_pane_table';
  636. // Create output
  637. $output = '';
  638. if (count($rows) > 0) {
  639. $output = theme('table', array(
  640. 'header' => $header,
  641. 'rows' => $rows,
  642. 'attributes' => array('id' => $table_id),
  643. ));
  644. }
  645. // Call to the drupal_add_tabledrag
  646. drupal_add_tabledrag($table_id, 'order', 'sibling', 'tripal_pane-item-weight');
  647. return $output;
  648. }
  649. /**
  650. * Configure panes
  651. */
  652. function tripal_panes_action_configure_panes (&$form, &$form_state) {
  653. $panes = $form_state['values']['pane_items'];
  654. foreach ($panes AS $id => $pane) {
  655. $table_layout = array();
  656. foreach($pane AS $key => $value) {
  657. if (is_array($value) && key_exists('table_group', $value)) {
  658. if ($value['table_group'] == 1) {
  659. $table_layout[] = $key;
  660. }
  661. }
  662. }
  663. $pane_settings = db_select('tripal_panes', 'tp')
  664. ->fields('tp', array('settings'))
  665. ->condition('pane_id', $id)
  666. ->execute()
  667. ->fetchField();
  668. $new_settings = unserialize($pane_settings);
  669. $new_settings['table_layout'] = $table_layout;
  670. db_update('tripal_panes')
  671. ->fields(array(
  672. 'settings' => serialize($new_settings)
  673. ))
  674. ->condition('pane_id', $id)
  675. ->execute();
  676. }
  677. }
  678. /**
  679. * Theme the Configure panes as a table
  680. *
  681. * @param unknown $variables
  682. * @return unknown
  683. */
  684. function theme_tripal_panes_form_configure_panes ($variables) {
  685. $element = $variables['element'];
  686. $rows = array();
  687. foreach (element_children($element) as $id) {
  688. $element[$id]['table_group']['#printed'] = FALSE;
  689. $rows[] = array(
  690. 'data' => array(
  691. $element[$id]['#title'],
  692. drupal_render($element[$id]['table_group']),
  693. ),
  694. );
  695. }
  696. // Create table header
  697. $header = array(t('Field'), t('Layout'));
  698. // Create a unique id for drupal_add_tabledrag() to find the table object
  699. $table_id = 'tripal_pane-configure_pane_table-' . strtolower(preg_replace('/\s+/', '_', $element['#title']));
  700. $table_class = 'tripal_pane-configure_pane_table';
  701. // Create output
  702. $table = '';
  703. if (count($rows) > 0) {
  704. $table = theme('table', array(
  705. 'header' => $header,
  706. 'rows' => $rows,
  707. 'attributes' => array('id' => $table_id, 'class' => array($table_class)),
  708. ));
  709. }
  710. $collapsible_item = array('element' => array());
  711. $collapsible_item['element']['#children'] = '';
  712. $collapsible_item['element']['#description'] =
  713. '<div id="tripal_panes-pane_configure-fieldset-instruction">
  714. Select a group to organize fields into table(s) in this pane.
  715. <div>'
  716. . $table;
  717. $collapsible_item['element']['#title'] = $element['#title'];
  718. // add 'collapsible' to the class setting to enable collapsible fieldset
  719. $collapsible_item['element']['#attributes']['class'] = array('tripal_panes-pane_configure-fieldset');
  720. $output = theme('fieldset', $collapsible_item);
  721. return $output;
  722. }
  723. /**
  724. * Implements hook_theme().
  725. */
  726. function tripal_panes_theme($existing, $type, $theme, $path) {
  727. return array(
  728. 'tripal_panes_form_arrange_panes' => array(
  729. 'render element' => 'element',
  730. ),
  731. 'tripal_panes_form_configure_panes' => array(
  732. 'render element' => 'element',
  733. ),
  734. 'tripal_panes_generic' => array(
  735. 'render element' => 'element',
  736. 'template' => 'tripal_panes_generic',
  737. 'path' => "$path/theme/templates",
  738. ),
  739. );
  740. }
  741. /**
  742. * Implements hook_entity_view.
  743. */
  744. function tripal_panes_entity_view($entity, $type, $view_mode, $langcode) {
  745. switch ($type) {
  746. case 'TripalEntity':
  747. // Use the generic template to render the fields
  748. if ($view_mode == 'full') {
  749. $bundle = db_select('tripal_bundle', 'tb')
  750. ->fields('tb', array('id', 'name', 'label'))
  751. ->condition('name', $entity->bundle)
  752. ->execute()
  753. ->fetchObject();
  754. tripal_panes_check_default_field_panes($bundle);
  755. $results = db_select('tripal_panes', 'tp')
  756. ->fields('tp', array('pane_id','name', 'label', 'settings'))
  757. ->condition('bundle_id', $bundle->id)
  758. ->orderBy('tp.weight', 'ASC')
  759. ->execute();
  760. $panes = array();
  761. $fields = array();
  762. $disabled_pane_id = 0;
  763. foreach ($results AS $row) {
  764. if ($row->name == 'te_disabled') {
  765. $disabled_pane_id = $row->pane_id;
  766. }
  767. else {
  768. $panes[$row->pane_id] = $row;
  769. $fields[$row->pane_id] = array();
  770. }
  771. }
  772. // Organize fields into panes
  773. foreach (element_children($entity->content) as $field_name) {
  774. $field_instance = field_info_instance ($type, $field_name, $entity->bundle);
  775. // Get default pane_id
  776. $default_pane_id = db_select('tripal_panes', 'tp')
  777. ->fields('tp', array('pane_id'))
  778. ->condition('bundle_id', $bundle->id)
  779. ->condition('name', 'te_base')
  780. ->execute()
  781. ->fetchField();
  782. // Get pane_id for the field
  783. $pane_id = db_select('tripal_pane_fields', 'tpf')
  784. ->fields('tpf', array('pane_id'))
  785. ->condition('field_id', $field_instance['id'])
  786. ->execute()
  787. ->fetchField();
  788. $pane_id = $pane_id ? $pane_id : $default_pane_id;
  789. // Do not show disabled fields
  790. if ($pane_id != $disabled_pane_id) {
  791. $fields[$pane_id][$field_name] = $entity->content[$field_name];
  792. }
  793. // Unset the field
  794. unset ($entity->content[$field_name]);
  795. }
  796. drupal_add_css(drupal_get_path('module','tripal_panes') . '/theme/css/tripal_panes.css');
  797. $entity->content['tripal_panes_generic'] = array(
  798. '#theme' => 'tripal_panes_generic',
  799. '#panes' => $panes,
  800. '#fields' => $fields,
  801. );
  802. }
  803. break;
  804. }
  805. }
  806. /**
  807. * Ajax callback to return pane's form elements when configuring pane
  808. */
  809. function tripal_panes_ajax_get_pane_setting_fieldset($form, &$form_state) {
  810. $pane_id = $form_state['values']['pane_select'];
  811. return $form['te_configure_panes']['pane_items'];
  812. }