tripal_fields_layout.module 29 KB

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