tripal_panes.module 30 KB

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