tripal_panes.module 28 KB

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