tripal_fields_layout.module 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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('name', $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. // Make sure our default panels are in the database.
  57. _tripal_fields_layout_check_default_field_panels($bundle);
  58. // Add a Panel
  59. tripal_fields_layout_form_field_ui_display_overview_form_panel_add($form, $form_state);
  60. // Arrange Panels
  61. tripal_fields_layout_form_field_ui_display_overview_form_panel_arrange($form, $form_state, $bundle);
  62. // Configure Panels
  63. tripal_fields_layout_form_field_ui_display_overview_form_panel_configure($form, $form_state, $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. //dpm($form);
  129. }
  130. /**
  131. * Add a Panel Form
  132. */
  133. function tripal_fields_layout_form_field_ui_display_overview_form_panel_add (&$form, &$form_state) {
  134. $form_state['input']['panel_label'] = key_exists('panel_label', $form_state['input']) ? $form_state['values']['panel_label'] : NULL;
  135. $form['te_add_panels']['instructions'] = array(
  136. '#type' => 'item',
  137. '#markup' => t('You may add as many panels to your page layout as
  138. desired. Panels can be used to organize fields into categories....')
  139. );
  140. $form['te_add_panels']['panel_name'] = array(
  141. '#type' => 'hidden',
  142. '#title' => 'Panel Name',
  143. '#description' => t('The name is automatically generated for a label. it
  144. contains alphanumeric values and underscores and does not begin with
  145. a number.')
  146. );
  147. $form['te_add_panels']['panel_label'] = array(
  148. '#type' => 'textfield',
  149. '#title' => 'Panel Label',
  150. '#default_value' => '',
  151. '#description' => t('Please provide a human readable label for this
  152. panel. This is the name that will appear to site visitors.')
  153. );
  154. $form['te_add_panels']['add_button'] = array(
  155. '#type' => 'submit',
  156. '#value' => 'Add Panel',
  157. '#name' => 'add-panel-submit'
  158. );
  159. }
  160. /**
  161. * Arrange Panels Form
  162. */
  163. function tripal_fields_layout_form_field_ui_display_overview_form_panel_arrange (&$form, &$form_state, $bundle) {
  164. $form['te_arrange_panels'] = array(
  165. '#type' => 'fieldset',
  166. '#title' => 'Arrange Panels',
  167. '#collapsible' => TRUE,
  168. '#collapsed' => TRUE,
  169. '#group' => 'overview_vert_tabs'
  170. );
  171. $form['te_arrange_panels']['instructions'] = array(
  172. '#type' => 'item',
  173. '#markup' => t('Drag and drop the panel to change its order.')
  174. );
  175. $form['te_arrange_panels']['panel_items']['#tree'] = TRUE;
  176. // Get available panels
  177. $result = db_select('tripal_panels', 'tp')
  178. ->fields('tp', array('panel_id', 'name', 'label', 'weight'))
  179. ->condition('name', 'te_base', '<>')
  180. ->condition('name', 'te_disabled', '<>')
  181. ->condition('bundle_id', $bundle->id)
  182. ->orderby('weight', 'asc')
  183. ->execute();
  184. $has_panel = FALSE;
  185. foreach ($result as $item) {
  186. $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;
  187. $form['te_arrange_panels']['panel_items'][$item->panel_id] = array(
  188. 'label' => array(
  189. '#markup' => check_plain($item->label),
  190. ),
  191. 'weight' => array(
  192. '#type' => 'weight',
  193. '#title' => t('Weight'),
  194. '#default_value' => $item->weight,
  195. '#delta' => 50,
  196. '#title_display' => 'invisible',
  197. ),
  198. 'newlabel' => array(
  199. '#type' => 'textfield',
  200. '#default_value' => '',
  201. '#size' => 10
  202. ),
  203. 'rename' => array(
  204. '#type' => 'submit',
  205. '#value' => 'Rename',
  206. '#name' => "arrange-panel-rename-$item->panel_id",
  207. ),
  208. 'remove' => array(
  209. '#type' => 'submit',
  210. '#value' => 'Remove',
  211. '#name' => "arrange-panel-remove-$item->panel_id",
  212. )
  213. );
  214. $has_panel = TRUE;
  215. }
  216. if ($has_panel) {
  217. $form['te_arrange_panels']['panel_items']['#theme_wrappers'] = array('tripal_fields_layout_form_arrange_panels');
  218. $form['te_arrange_panels']['save_button'] = array(
  219. '#type' => 'submit',
  220. '#value' => 'Save Panel Order',
  221. '#name' => 'order-panel-submit'
  222. );
  223. }
  224. else {
  225. $form['te_arrange_panels']['instructions']['#markup'] = t('You need to add some panel first.');
  226. }
  227. }
  228. /**
  229. * Configure Panels Form
  230. */
  231. function tripal_fields_layout_form_field_ui_display_overview_form_panel_configure (&$form, &$form_state, $bundle) {
  232. $form['te_configure_panels'] = array(
  233. '#type' => 'fieldset',
  234. '#title' => 'Configure Panels',
  235. '#collapsible' => TRUE,
  236. '#collapsed' => TRUE,
  237. '#group' => 'overview_vert_tabs'
  238. );
  239. // Add a dropdown for selecting panel to configure
  240. $form['te_configure_panels']['panel_select'] = array(
  241. '#type' => 'select',
  242. '#title' => t('Panel'),
  243. '#description' => t('Select a panel to change its layout. Fields can be grouped into a table if Table layout is selected.'),
  244. '#ajax' => array(
  245. 'callback' => 'tripal_fields_layout_ajax_get_panel_setting_fieldset',
  246. 'wrapper' => 'tripal-fields-layout-panel-setting',
  247. 'effect' => 'fade'
  248. )
  249. );
  250. $form['te_configure_panels']['panel_items'] = array (
  251. '#tree' => TRUE,
  252. '#prefix' => '<div id="tripal-fields-layout-panel-setting">',
  253. '#suffix' => '</div>'
  254. );
  255. // Get available panels
  256. $panels = db_select('tripal_panels', 'tp')
  257. ->fields('tp', array('panel_id', 'name', 'label', 'weight', 'settings'))
  258. ->condition('name', 'te_disabled', '<>')
  259. ->condition('bundle_id', $bundle->id)
  260. ->orderby('weight', 'asc')
  261. ->execute();
  262. $has_panel = FALSE;
  263. $options = array(0 => 'Select a panel');
  264. $selected_panel = key_exists('values', $form_state) ? $form_state['values']['panel_select'] : 0;
  265. foreach ($panels as $panel) {
  266. $options[$panel->panel_id] = $panel->label;
  267. $has_panel = TRUE;
  268. if ($panel->panel_id != $selected_panel) {
  269. continue; // Display only the selected panel setting
  270. }
  271. $panel_settings = unserialize($panel->settings);
  272. $table_layout = key_exists('table_layout', $panel_settings) ? $panel_settings['table_layout'] : array();
  273. $form['te_configure_panels']['panel_items'][$panel->panel_id] = array(
  274. '#type' => 'fieldset',
  275. '#title' => check_plain($panel->label),
  276. '#collapsible' => TRUE,
  277. '#collapsed' => FALSE,
  278. );
  279. //Add fields to each panel for changing configuration
  280. $fields = db_select('tripal_panel_fields', 'tpf')
  281. ->fields('tpf', array('field_id'))
  282. ->condition('panel_id', $panel->panel_id)
  283. ->execute();
  284. $has_field = FALSE;
  285. foreach ($fields AS $field) {
  286. $field_obj = db_select('field_config_instance', 'tci')
  287. ->fields('tci', array('field_name','data'))
  288. ->condition('id', $field->field_id)
  289. ->execute()
  290. ->fetchObject();
  291. $field_arr = unserialize($field_obj->data);
  292. $fname = $field_obj->field_name;
  293. if($fname == 'featureprop') {
  294. continue;
  295. }
  296. $flable = $field_arr['label'];
  297. $form['te_configure_panels']['panel_items'][$panel->panel_id][$fname] = array(
  298. '#type' => 'item',
  299. '#title' => $flable,
  300. '#weight' => $field_arr['display']['default']['weight']
  301. );
  302. $default_value = 0;
  303. if (in_array($fname, $table_layout)) {
  304. $default_value = 1;
  305. }
  306. $form['te_configure_panels']['panel_items'][$panel->panel_id][$fname]['table_group'] = array(
  307. '#type' => 'radios',
  308. '#options' => array(
  309. 0 => 'Default',
  310. 1 => 'Table',
  311. ),
  312. '#default_value' => $default_value,
  313. );
  314. $has_field = TRUE;
  315. }
  316. if (!$has_field) {
  317. $form['te_configure_panels']['panel_items'][$panel->panel_id]['no_field'] = array(
  318. '#markup' => 'No field in this panel.',
  319. );
  320. } else {
  321. $form['te_configure_panels']['panel_items'][$panel->panel_id]['#theme_wrappers'] = array('tripal_fields_layout_form_configure_panels');
  322. }
  323. }
  324. $form['te_configure_panels']['panel_select']['#options'] = $options;
  325. if ($has_panel) {
  326. $form['te_configure_panels']['save_button'] = array(
  327. '#type' => 'submit',
  328. '#value' => 'Save Panel Configuration',
  329. '#name' => 'configure-panel-submit'
  330. );
  331. }
  332. else {
  333. $form['te_configure_panels']['instructions']['#markup'] = t('You need to add some panel first.');
  334. }
  335. }
  336. /**
  337. * A helper function for checking if the default panels are in the database.
  338. */
  339. function _tripal_fields_layout_check_default_field_panels($bundle) {
  340. // Make sure we have records for our default regions: te_base and te_hidden.
  341. // First check if the base region is in the database. If not, add it.
  342. $te_base = db_select('tripal_panels', 'tp')
  343. ->fields('tp')
  344. ->condition('name', 'te_base')
  345. ->condition('bundle_id', $bundle->id)
  346. ->execute()
  347. ->fetchObject();
  348. if (!$te_base) {
  349. $settings = array(
  350. '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.',
  351. );
  352. db_insert('tripal_panels')
  353. ->fields(array(
  354. 'bundle_id' => $bundle->id,
  355. 'name' => 'te_base',
  356. 'label' => 'Base Content',
  357. 'settings' => serialize($settings),
  358. 'weight' => -9999999
  359. ))
  360. ->execute();
  361. }
  362. // Now add all fields to the default region if they are not assigned
  363. $panel_id = db_select('tripal_panels', 'tp')
  364. ->fields('tp', array('panel_id'))
  365. ->condition('name', 'te_base')
  366. ->condition('bundle_id', $bundle->id)
  367. ->execute()
  368. ->fetchField();
  369. if(property_exists($bundle, 'type')) {
  370. $fields = db_select('field_config_instance', 'fci')
  371. ->fields('fci', array('id'))
  372. ->condition('entity_type', $bundle->type)
  373. ->condition('bundle', $bundle->name)
  374. ->execute();
  375. foreach($fields AS $field) {
  376. $penal_field_id = db_select('tripal_panel_fields', 'tpf')
  377. ->fields('tpf', array('panel_field_id'))
  378. ->condition('field_id', $field->id)
  379. ->execute()
  380. ->fetchField();
  381. if (!$penal_field_id) {
  382. db_insert('tripal_panel_fields')
  383. ->fields(array(
  384. 'panel_id' => $panel_id,
  385. 'field_id' => $field->id
  386. ))
  387. ->execute();
  388. }
  389. }
  390. }
  391. // Next check if the hidden region is in the database. If not, add it.
  392. $te_base = db_select('tripal_panels', 'tp')
  393. ->fields('tp')
  394. ->condition('name', 'te_disabled')
  395. ->condition('bundle_id', $bundle->id)
  396. ->execute()
  397. ->fetchObject();
  398. if (!$te_base) {
  399. $settings = array(
  400. 'message' => 'Place field here to hide them from display.',
  401. );
  402. db_insert('tripal_panels')
  403. ->fields(array(
  404. 'bundle_id' => $bundle->id,
  405. 'name' => 'te_disabled',
  406. 'label' => 'Disabled',
  407. 'settings' => serialize($settings),
  408. 'weight' => 9999999
  409. ))
  410. ->execute();
  411. }
  412. }
  413. /**
  414. * Returns the region to which a row on the Field UI page belongs.
  415. *
  416. * @param $row
  417. * The current row that is being rendered in the Field UI page.
  418. */
  419. function tripal_fields_layout_field_ui_row_region($row) {
  420. $default_panel = 'te_base';
  421. $panel = '';
  422. $field_instance_id = $row['#field_instance_id']['#value'];
  423. // Get panel_id
  424. $panel_id = db_select('tripal_panel_fields', 'tpf')
  425. ->fields('tpf', array('panel_id'))
  426. ->condition('field_id', $field_instance_id)
  427. ->execute()
  428. ->fetchField();
  429. // Get panel name
  430. if ($panel_id) {
  431. $panel = db_select('tripal_panels', 'tp')
  432. ->fields('tp', array('name'))
  433. ->condition('panel_id', $panel_id)
  434. ->execute()
  435. ->fetchField();
  436. }
  437. // First get the panel
  438. if (!$panel) {
  439. $panel = $default_panel;
  440. }
  441. return $panel;
  442. }
  443. /**
  444. * Validates the field UI form to ensure proper panel assignments.
  445. *
  446. * @param $form
  447. * @param $form_state
  448. */
  449. function tripal_fields_layout_field_ui_validate(&$form, &$form_state) {
  450. }
  451. /**
  452. * Responds to a submit from the field UI form for saving panel assignments.
  453. *
  454. * @param $form
  455. * @param $form_state
  456. */
  457. function tripal_fields_layout_field_ui_submit($form, &$form_state) {
  458. // Add a panel
  459. if ($form_state ['clicked_button'] ['#name'] == 'add-panel-submit') {
  460. tripal_fields_layout_action_add_panel($form, $form_state);
  461. }
  462. // Order panels
  463. else if ($form_state['clicked_button'] ['#name'] == 'order-panel-submit') {
  464. tripal_fields_layout_action_order_panels($form, $form_state);
  465. }
  466. // Rename a panel
  467. else if (preg_match('/^arrange-panel-rename-/', $form_state ['clicked_button'] ['#name'])) {
  468. tripal_fields_layout_action_rename_panel ($form, $form_state);
  469. }
  470. // Remove a panel
  471. else if (preg_match('/^arrange-panel-remove-/', $form_state ['clicked_button'] ['#name'])) {
  472. tripal_fields_layout_action_remove_panel ($form_state);
  473. }
  474. // Configure panels
  475. else if ($form_state['clicked_button'] ['#name'] == 'configure-panel-submit') {
  476. tripal_fields_layout_action_configure_panels($form, $form_state);
  477. }
  478. // Save fields for each panel
  479. else if ($form_state['clicked_button'] ['#name'] == 'op') {
  480. $bundle = $form_state['build_info']['args'][1];
  481. $fields = $form_state['values']['fields'];
  482. foreach($fields AS $field_name => $field_data){
  483. // Get field instance id
  484. $field_instance_id = $form['fields'][$field_name]['#field_instance_id']['#value'];
  485. // Get region panel_id
  486. $region = $field_data['region'];
  487. $panel_id = db_select('tripal_panels', 'tp')
  488. ->fields('tp', array('panel_id'))
  489. ->condition('name', $region)
  490. ->condition('bundle_id', $bundle->id)
  491. ->execute()
  492. ->fetchField();
  493. // Save
  494. $penal_field_id = db_select('tripal_panel_fields', 'tpf')
  495. ->fields('tpf', array('panel_field_id'))
  496. ->condition('field_id', $field_instance_id)
  497. ->execute()
  498. ->fetchField();
  499. if ($penal_field_id) {
  500. db_update('tripal_panel_fields')
  501. ->fields(array(
  502. 'panel_id' => $panel_id,
  503. ))
  504. ->condition('panel_field_id', $penal_field_id)
  505. ->execute();
  506. }
  507. else {
  508. db_insert('tripal_panel_fields')
  509. ->fields(array(
  510. 'panel_id' => $panel_id,
  511. 'field_id' => $field_instance_id
  512. ))
  513. ->execute();
  514. }
  515. }
  516. }
  517. }
  518. /**
  519. * Add a new panel
  520. */
  521. function tripal_fields_layout_action_add_panel (&$form, &$form_state) {
  522. // Check if a panel label is provided
  523. $label = $form_state ['values'] ['panel_label'];
  524. if (! $label) {
  525. form_set_error('panel_label', t ("Please provide a label for the new panel."));
  526. }
  527. // Generate a valide panel name using the label.
  528. // i.e. removing leading numbers and spaces
  529. $name = strtolower(preg_replace ('/^\d|\s+/', '_', $label));
  530. $bundle = $form_state['build_info']['args'][1];
  531. $name_exists = db_select('tripal_panels', 'tp')
  532. ->fields('tp', array('panel_id'))
  533. ->condition('name', $name)
  534. ->condition('bundle_id', $bundle->id)
  535. ->execute()
  536. ->fetchField();
  537. if ($name_exists) {
  538. form_set_error('panel_label', t('The name is used by another panel. Please use a different label.'));
  539. }
  540. else {
  541. $form_state ['values'] ['panel_name'] = $name;
  542. $bundle_id = $form_state['build_info']['args'][1]->id;
  543. $name = $form_state['values']['panel_name'];
  544. $label = $form_state['values']['panel_label'];
  545. $settings = array(
  546. 'message' => 'Place field in this panel.',
  547. );
  548. db_insert('tripal_panels')
  549. ->fields(array(
  550. 'bundle_id' => $bundle_id,
  551. 'name' => $name,
  552. 'label' => $label,
  553. 'settings' => serialize($settings),
  554. 'weight' => 0
  555. ))
  556. ->execute();
  557. form_set_value($form['te_add_panels']['panel_label'], '', $form_state);
  558. }
  559. }
  560. /**
  561. * Rename panel
  562. */
  563. function tripal_fields_layout_action_rename_panel (&$form, &$form_state) {
  564. $button = $form_state ['triggering_element'] ['#name'];
  565. $panel_id = str_replace ('arrange-panel-rename-', '', $button);
  566. $newlabel = $form_state['values']['panel_items'][$panel_id]['newlabel'];
  567. if (!trim($newlabel)) {
  568. form_set_error ('panel_label', t ("Please provide a label to rename a panel.") );
  569. }
  570. else {
  571. db_update('tripal_panels')
  572. ->fields(array(
  573. 'label' => $newlabel,
  574. 'name' => strtolower(preg_replace ('/^\d|\s+/', '_', $newlabel))
  575. ))
  576. ->condition('panel_id', $panel_id)
  577. ->execute();
  578. form_set_value($form['te_arrange_panels']['panel_items'][$panel_id]['newlabel'], '', $form_state);
  579. }
  580. }
  581. /**
  582. * Remove panel
  583. */
  584. function tripal_fields_layout_action_remove_panel (&$form_state) {
  585. $button = $form_state ['triggering_element'] ['#name'];
  586. $panel_id = str_replace ('arrange-panel-remove-', '', $button);
  587. db_delete('tripal_panels')
  588. ->condition ('panel_id', $panel_id)
  589. ->execute();
  590. db_delete('tripal_panel_fields')
  591. ->condition ('panel_id', $panel_id)
  592. ->execute();
  593. }
  594. /**
  595. * Order panel
  596. */
  597. function tripal_fields_layout_action_order_panels (&$form, &$form_state) {
  598. $panels = $form_state['values']['panel_items'];
  599. foreach ($panels AS $id => $panel) {
  600. if (key_exists('weight', $panel)) {
  601. db_query('UPDATE {tripal_panels} SET weight = :weight WHERE panel_id = :id',
  602. array(
  603. ':weight' => $panel['weight'],
  604. ':id' => $id
  605. )
  606. );
  607. }
  608. }
  609. }
  610. /**
  611. * Theme the Arrange Panels as a draggable table
  612. *
  613. * @param unknown $variables
  614. * @return unknown
  615. */
  616. function theme_tripal_fields_layout_form_arrange_panels ($variables) {
  617. $element = $variables['element'];
  618. $rows = array();
  619. foreach (element_children($element) as $id) {
  620. // Add a custom class that can be identified by 'drupal_add_tabledrag'
  621. $element[$id]['weight']['#attributes']['class'] = array('tripal_panel-item-weight');
  622. $element[$id]['label']['#printed'] = FALSE;
  623. $element[$id]['weight']['#printed'] = FALSE;
  624. $element[$id]['newlabel']['#printed'] = FALSE;
  625. $element[$id]['rename']['#printed'] = FALSE;
  626. $element[$id]['remove']['#printed'] = FALSE;
  627. $rows[] = array(
  628. 'data' => array(
  629. drupal_render($element[$id]['label']),
  630. drupal_render($element[$id]['weight']),
  631. drupal_render($element[$id]['newlabel']),
  632. drupal_render($element[$id]['rename']),
  633. drupal_render($element[$id]['remove'])
  634. ),
  635. // Add draggable to each row to support the tabledrag behaviour
  636. 'class' => array('draggable'),
  637. );
  638. }
  639. // Create table header
  640. $header = array(t('Panel'), t('Weight'), array('data' => t('Rename'), 'colspan' => 2), t('Action'));
  641. // Create a unique id for drupal_add_tabledrag() to find the table object
  642. $table_id = 'tripal_panel-arrange_panel_table';
  643. // Create output
  644. $output = '';
  645. if (count($rows) > 0) {
  646. $output = theme('table', array(
  647. 'header' => $header,
  648. 'rows' => $rows,
  649. 'attributes' => array('id' => $table_id),
  650. ));
  651. }
  652. // Call to the drupal_add_tabledrag
  653. drupal_add_tabledrag($table_id, 'order', 'sibling', 'tripal_panel-item-weight');
  654. return $output;
  655. }
  656. /**
  657. * Configure panels
  658. */
  659. function tripal_fields_layout_action_configure_panels (&$form, &$form_state) {
  660. $panels = $form_state['values']['panel_items'];
  661. foreach ($panels AS $id => $panel) {
  662. $table_layout = array();
  663. foreach($panel AS $key => $value) {
  664. if (is_array($value) && key_exists('table_group', $value)) {
  665. if ($value['table_group'] == 1) {
  666. $table_layout[] = $key;
  667. }
  668. }
  669. }
  670. $panel_settings = db_select('tripal_panels', 'tp')
  671. ->fields('tp', array('settings'))
  672. ->condition('panel_id', $id)
  673. ->execute()
  674. ->fetchField();
  675. $new_settings = unserialize($panel_settings);
  676. $new_settings['table_layout'] = $table_layout;
  677. db_update('tripal_panels')
  678. ->fields(array(
  679. 'settings' => serialize($new_settings)
  680. ))
  681. ->condition('panel_id', $id)
  682. ->execute();
  683. }
  684. }
  685. /**
  686. * Theme the Configure Panels as a table
  687. *
  688. * @param unknown $variables
  689. * @return unknown
  690. */
  691. function theme_tripal_fields_layout_form_configure_panels ($variables) {
  692. $element = $variables['element'];
  693. $rows = array();
  694. foreach (element_children($element) as $id) {
  695. $element[$id]['table_group']['#printed'] = FALSE;
  696. $rows[] = array(
  697. 'data' => array(
  698. $element[$id]['#title'],
  699. drupal_render($element[$id]['table_group']),
  700. ),
  701. );
  702. }
  703. // Create table header
  704. $header = array(t('Field'), t('Layout'));
  705. // Create a unique id for drupal_add_tabledrag() to find the table object
  706. $table_id = 'tripal_panel-configure_panel_table-' . strtolower(preg_replace('/\s+/', '_', $element['#title']));
  707. $table_class = 'tripal_panel-configure_panel_table';
  708. // Create output
  709. $table = '';
  710. if (count($rows) > 0) {
  711. $table = theme('table', array(
  712. 'header' => $header,
  713. 'rows' => $rows,
  714. 'attributes' => array('id' => $table_id, 'class' => array($table_class)),
  715. ));
  716. }
  717. $collapsible_item = array('element' => array());
  718. $collapsible_item['element']['#children'] = '';
  719. $collapsible_item['element']['#description'] =
  720. '<div id="tripal_fields_layout-panel_configure-fieldset-instruction">
  721. Select a group to organize fields into table(s) in this panel.
  722. <div>'
  723. . $table;
  724. $collapsible_item['element']['#title'] = $element['#title'];
  725. // add 'collapsible' to the class setting to enable collapsible fieldset
  726. $collapsible_item['element']['#attributes']['class'] = array('tripal_fields_layout-panel_configure-fieldset');
  727. $output = theme('fieldset', $collapsible_item);
  728. return $output;
  729. }
  730. /**
  731. * Implements hook_theme().
  732. */
  733. function tripal_fields_layout_theme($existing, $type, $theme, $path) {
  734. return array(
  735. 'tripal_fields_layout_form_arrange_panels' => array(
  736. 'render element' => 'element',
  737. ),
  738. 'tripal_fields_layout_form_configure_panels' => array(
  739. 'render element' => 'element',
  740. ),
  741. 'tripal_fields_layout_generic' => array(
  742. 'render element' => 'element',
  743. 'template' => 'tripal_fields_layout_generic',
  744. 'path' => "$path/theme/templates",
  745. ),
  746. );
  747. }
  748. /**
  749. * Implements hook_entity_view.
  750. */
  751. function tripal_fields_layout_entity_view($entity, $type, $view_mode, $langcode) {
  752. switch ($type) {
  753. case 'TripalEntity':
  754. // Use the generic template to render the fields
  755. if ($view_mode == 'full') {
  756. $bundle = db_select('tripal_bundle', 'tb')
  757. ->fields('tb', array('id', 'name', 'label'))
  758. ->condition('name', $entity->bundle)
  759. ->execute()
  760. ->fetchObject();
  761. _tripal_fields_layout_check_default_field_panels($bundle);
  762. $results = db_select('tripal_panels', 'tp')
  763. ->fields('tp', array('panel_id','name', 'label', 'settings'))
  764. ->condition('bundle_id', $bundle->id)
  765. ->orderBy('tp.weight', 'ASC')
  766. ->execute();
  767. $panels = array();
  768. $fields = array();
  769. $disabled_panel_id = 0;
  770. foreach ($results AS $row) {
  771. if ($row->name == 'te_disabled') {
  772. $disabled_panel_id = $row->panel_id;
  773. }
  774. else {
  775. $panels[$row->panel_id] = $row;
  776. $fields[$row->panel_id] = array();
  777. }
  778. }
  779. // Organize fields into panels
  780. foreach (element_children($entity->content) as $field_name) {
  781. $field_instance = field_info_instance ($type, $field_name, $entity->bundle);
  782. // Get default panel_id
  783. $default_panel_id = db_select('tripal_panels', 'tp')
  784. ->fields('tp', array('panel_id'))
  785. ->condition('bundle_id', $bundle->id)
  786. ->condition('name', 'te_base')
  787. ->execute()
  788. ->fetchField();
  789. // Get panel_id for the field
  790. $panel_id = db_select('tripal_panel_fields', 'tpf')
  791. ->fields('tpf', array('panel_id'))
  792. ->condition('field_id', $field_instance['id'])
  793. ->execute()
  794. ->fetchField();
  795. $panel_id = $panel_id ? $panel_id : $default_panel_id;
  796. // Do not show disabled fields
  797. if ($panel_id != $disabled_panel_id) {
  798. $fields[$panel_id][$field_name] = $entity->content[$field_name];
  799. }
  800. // Unset the field
  801. unset ($entity->content[$field_name]);
  802. }
  803. drupal_add_css(drupal_get_path('module','tripal_fields_layout') . '/theme/css/tripal_fields_layout.css');
  804. $entity->content['tripal_fields_layout_generic'] = array(
  805. '#theme' => 'tripal_fields_layout_generic',
  806. '#panels' => $panels,
  807. '#fields' => $fields,
  808. );
  809. }
  810. break;
  811. }
  812. }
  813. /**
  814. * Implements hook_entity_view.
  815. */
  816. function tripal_fields_layout_ajax_get_panel_setting_fieldset($form, &$form_state) {
  817. $panel_id = $form_state['values']['panel_select'];
  818. return $form['te_configure_panels']['panel_items'];
  819. }