tripal_fields_layout.module 28 KB

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