tripal_fields_layout.module 28 KB

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