tripal_fields_layout.module 28 KB

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