tripal_fields_layout.module 26 KB

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