tripal_panes.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. $form['te_configure_panes']['pane_items'][$pane->pane_id][$fname]['field_id'] = array(
  308. '#type' => 'hidden',
  309. '#value' => $field->field_id,
  310. );
  311. $default_classes =
  312. db_select('tripal_pane_fields', 'tpf')
  313. ->fields('tpf', array('css_class'))
  314. ->condition('pane_id', $pane->pane_id)
  315. ->condition('field_id', $field->field_id)
  316. ->execute()
  317. ->fetchField();
  318. $form['te_configure_panes']['pane_items'][$pane->pane_id][$fname]['css_class'] = array(
  319. '#type' => 'textfield',
  320. '#default_value' => $default_classes,
  321. '#size' => 40
  322. );
  323. $has_field = TRUE;
  324. }
  325. if (!$has_field) {
  326. $form['te_configure_panes']['pane_items'][$pane->pane_id]['no_field'] = array(
  327. '#markup' => 'No field in this pane.',
  328. );
  329. } else {
  330. $form['te_configure_panes']['pane_items'][$pane->pane_id]['#theme_wrappers'] = array('tripal_panes_form_configure_panes');
  331. }
  332. if ($pane->pane_id != $selected_pane) {
  333. // Display only the selected pane setting. Hide the others
  334. $form['te_configure_panes']['pane_items'][$pane->pane_id]['#prefix'] = "<div style=\"display:none;\">";
  335. $form['te_configure_panes']['pane_items'][$pane->pane_id]['#suffix'] = "</div>";
  336. }
  337. }
  338. $form['te_configure_panes']['pane_select']['#options'] = $options;
  339. if ($has_pane) {
  340. $form['te_configure_panes']['save_button'] = array(
  341. '#type' => 'submit',
  342. '#value' => 'Save pane Configuration',
  343. '#name' => 'configure-pane-submit'
  344. );
  345. }
  346. else {
  347. $form['te_configure_panes']['instructions']['#markup'] = t('A pane must be added before any configuration changes can be made.');
  348. }
  349. }
  350. /**
  351. * A helper function for checking if the default panes are in the database.
  352. */
  353. function tripal_panes_check_default_field_panes($bundle) {
  354. // Make sure we have records for our default regions: te_base and te_hidden.
  355. // First check if the base region is in the database. If not, add it.
  356. $te_base = db_select('tripal_panes', 'tp')
  357. ->fields('tp')
  358. ->condition('name', 'te_base')
  359. ->condition('bundle_id', $bundle->id)
  360. ->execute()
  361. ->fetchObject();
  362. if (!$te_base) {
  363. $settings = array(
  364. '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.',
  365. );
  366. db_insert('tripal_panes')
  367. ->fields(array(
  368. 'bundle_id' => $bundle->id,
  369. 'name' => 'te_base',
  370. 'label' => 'Base Content',
  371. 'settings' => serialize($settings),
  372. 'weight' => -9999999
  373. ))
  374. ->execute();
  375. }
  376. // Now add all fields to the default region if they are not assigned
  377. $pane_id = db_select('tripal_panes', 'tp')
  378. ->fields('tp', array('pane_id'))
  379. ->condition('name', 'te_base')
  380. ->condition('bundle_id', $bundle->id)
  381. ->execute()
  382. ->fetchField();
  383. if(property_exists($bundle, 'type')) {
  384. $fields = db_select('field_config_instance', 'fci')
  385. ->fields('fci', array('id'))
  386. ->condition('entity_type', $bundle->type)
  387. ->condition('bundle', $bundle->name)
  388. ->execute();
  389. foreach($fields AS $field) {
  390. $penal_field_id = db_select('tripal_pane_fields', 'tpf')
  391. ->fields('tpf', array('pane_field_id'))
  392. ->condition('field_id', $field->id)
  393. ->execute()
  394. ->fetchField();
  395. if (!$penal_field_id) {
  396. db_insert('tripal_pane_fields')
  397. ->fields(array(
  398. 'pane_id' => $pane_id,
  399. 'field_id' => $field->id
  400. ))
  401. ->execute();
  402. }
  403. }
  404. }
  405. // Next check if the hidden region is in the database. If not, add it.
  406. $te_base = db_select('tripal_panes', 'tp')
  407. ->fields('tp')
  408. ->condition('name', 'te_disabled')
  409. ->condition('bundle_id', $bundle->id)
  410. ->execute()
  411. ->fetchObject();
  412. if (!$te_base) {
  413. $settings = array(
  414. 'message' => 'Place field here to hide them from display.',
  415. );
  416. db_insert('tripal_panes')
  417. ->fields(array(
  418. 'bundle_id' => $bundle->id,
  419. 'name' => 'te_disabled',
  420. 'label' => 'Disabled',
  421. 'settings' => serialize($settings),
  422. 'weight' => 9999999
  423. ))
  424. ->execute();
  425. }
  426. }
  427. /**
  428. * Returns the region to which a row on the Field UI page belongs.
  429. *
  430. * @param $row
  431. * The current row that is being rendered in the Field UI page.
  432. */
  433. function tripal_panes_field_ui_row_region($row) {
  434. $default_pane = 'te_base';
  435. $pane = '';
  436. $field_instance_id = $row['#field_instance_id']['#value'];
  437. // Get pane_id
  438. $pane_id = db_select('tripal_pane_fields', 'tpf')
  439. ->fields('tpf', array('pane_id'))
  440. ->condition('field_id', $field_instance_id)
  441. ->execute()
  442. ->fetchField();
  443. // Get pane name
  444. if ($pane_id) {
  445. $pane = db_select('tripal_panes', 'tp')
  446. ->fields('tp', array('name'))
  447. ->condition('pane_id', $pane_id)
  448. ->execute()
  449. ->fetchField();
  450. }
  451. // First get the pane
  452. if (!$pane) {
  453. $pane = $default_pane;
  454. }
  455. return $pane;
  456. }
  457. /**
  458. * Validates the field UI form to ensure proper pane assignments.
  459. *
  460. * @param $form
  461. * @param $form_state
  462. */
  463. function tripal_panes_field_ui_validate(&$form, &$form_state) {
  464. }
  465. /**
  466. * Responds to a submit from the field UI form for saving pane assignments.
  467. *
  468. * @param $form
  469. * @param $form_state
  470. */
  471. function tripal_panes_field_ui_submit($form, &$form_state) {
  472. // Add a pane
  473. if ($form_state ['clicked_button'] ['#name'] == 'add-pane-submit') {
  474. tripal_panes_action_add_pane($form, $form_state);
  475. }
  476. // Order panes
  477. else if ($form_state['clicked_button'] ['#name'] == 'order-pane-submit') {
  478. tripal_panes_action_order_panes($form, $form_state);
  479. }
  480. // Rename a pane
  481. else if (preg_match('/^arrange-pane-rename-/', $form_state ['clicked_button'] ['#name'])) {
  482. tripal_panes_action_rename_pane ($form, $form_state);
  483. }
  484. // Remove a pane
  485. else if (preg_match('/^arrange-pane-remove-/', $form_state ['clicked_button'] ['#name'])) {
  486. tripal_panes_action_remove_pane ($form_state);
  487. }
  488. // Configure panes
  489. else if ($form_state['clicked_button'] ['#name'] == 'configure-pane-submit') {
  490. tripal_panes_action_configure_panes($form, $form_state);
  491. }
  492. // Save fields for each pane
  493. else if ($form_state['clicked_button'] ['#name'] == 'op') {
  494. $bundle = $form_state['build_info']['args'][1];
  495. $fields = $form_state['values']['fields'];
  496. foreach($fields AS $field_name => $field_data){
  497. // Get field instance id
  498. $field_instance_id = $form['fields'][$field_name]['#field_instance_id']['#value'];
  499. // Get region pane_id
  500. $region = $field_data['region'];
  501. $pane_id = db_select('tripal_panes', 'tp')
  502. ->fields('tp', array('pane_id'))
  503. ->condition('name', $region)
  504. ->condition('bundle_id', $bundle->id)
  505. ->execute()
  506. ->fetchField();
  507. // Save
  508. $penal_field_id = db_select('tripal_pane_fields', 'tpf')
  509. ->fields('tpf', array('pane_field_id'))
  510. ->condition('field_id', $field_instance_id)
  511. ->execute()
  512. ->fetchField();
  513. if ($penal_field_id) {
  514. db_update('tripal_pane_fields')
  515. ->fields(array(
  516. 'pane_id' => $pane_id,
  517. ))
  518. ->condition('pane_field_id', $penal_field_id)
  519. ->execute();
  520. }
  521. else {
  522. db_insert('tripal_pane_fields')
  523. ->fields(array(
  524. 'pane_id' => $pane_id,
  525. 'field_id' => $field_instance_id
  526. ))
  527. ->execute();
  528. }
  529. }
  530. }
  531. }
  532. /**
  533. * Add a new pane
  534. */
  535. function tripal_panes_action_add_pane (&$form, &$form_state) {
  536. // Check if a pane label is provided
  537. $label = $form_state ['values'] ['pane_label'];
  538. if (! $label) {
  539. form_set_error('pane_label', t ("Please provide a label for the new pane."));
  540. }
  541. // Generate a valide pane name using the label.
  542. // i.e. removing leading numbers and spaces
  543. $name = strtolower(preg_replace ('/^\d|\s+/', '_', $label));
  544. $bundle = $form_state['build_info']['args'][1];
  545. $name_exists = db_select('tripal_panes', 'tp')
  546. ->fields('tp', array('pane_id'))
  547. ->condition('name', $name)
  548. ->condition('bundle_id', $bundle->id)
  549. ->execute()
  550. ->fetchField();
  551. if ($name_exists) {
  552. form_set_error('pane_label', t('The name is used by another pane. Please use a different label.'));
  553. }
  554. else {
  555. $form_state ['values'] ['pane_name'] = $name;
  556. $bundle_id = $form_state['build_info']['args'][1]->id;
  557. $name = $form_state['values']['pane_name'];
  558. $label = $form_state['values']['pane_label'];
  559. $settings = array(
  560. 'message' => 'Place field in this pane.',
  561. );
  562. db_insert('tripal_panes')
  563. ->fields(array(
  564. 'bundle_id' => $bundle_id,
  565. 'name' => $name,
  566. 'label' => $label,
  567. 'settings' => serialize($settings),
  568. 'weight' => 0
  569. ))
  570. ->execute();
  571. form_set_value($form['te_add_panes']['pane_label'], '', $form_state);
  572. }
  573. }
  574. /**
  575. * Rename pane
  576. */
  577. function tripal_panes_action_rename_pane (&$form, &$form_state) {
  578. $button = $form_state ['triggering_element'] ['#name'];
  579. $pane_id = str_replace ('arrange-pane-rename-', '', $button);
  580. $newlabel = $form_state['values']['pane_items'][$pane_id]['newlabel'];
  581. if (!trim($newlabel)) {
  582. form_set_error ('pane_label', t ("Please provide a label to rename a pane.") );
  583. }
  584. else {
  585. db_update('tripal_panes')
  586. ->fields(array(
  587. 'label' => $newlabel,
  588. 'name' => strtolower(preg_replace ('/^\d|\s+/', '_', $newlabel))
  589. ))
  590. ->condition('pane_id', $pane_id)
  591. ->execute();
  592. form_set_value($form['te_arrange_panes']['pane_items'][$pane_id]['newlabel'], '', $form_state);
  593. }
  594. }
  595. /**
  596. * Remove pane
  597. */
  598. function tripal_panes_action_remove_pane (&$form_state) {
  599. $button = $form_state ['triggering_element'] ['#name'];
  600. $pane_id = str_replace ('arrange-pane-remove-', '', $button);
  601. db_delete('tripal_panes')
  602. ->condition ('pane_id', $pane_id)
  603. ->execute();
  604. db_delete('tripal_pane_fields')
  605. ->condition ('pane_id', $pane_id)
  606. ->execute();
  607. }
  608. /**
  609. * Order pane
  610. */
  611. function tripal_panes_action_order_panes (&$form, &$form_state) {
  612. $panes = $form_state['values']['pane_items'];
  613. foreach ($panes AS $id => $pane) {
  614. if (key_exists('weight', $pane)) {
  615. db_query('UPDATE {tripal_panes} SET weight = :weight WHERE pane_id = :id',
  616. array(
  617. ':weight' => $pane['weight'],
  618. ':id' => $id
  619. )
  620. );
  621. }
  622. }
  623. }
  624. /**
  625. * Theme the Arrange panes as a draggable table
  626. *
  627. * @param $variables
  628. * @return
  629. */
  630. function theme_tripal_panes_form_arrange_panes ($variables) {
  631. $element = $variables['element'];
  632. $rows = array();
  633. foreach (element_children($element) as $id) {
  634. // Add a custom class that can be identified by 'drupal_add_tabledrag'
  635. $element[$id]['weight']['#attributes']['class'] = array('tripal_pane-item-weight');
  636. $element[$id]['label']['#printed'] = FALSE;
  637. $element[$id]['weight']['#printed'] = FALSE;
  638. $element[$id]['newlabel']['#printed'] = FALSE;
  639. $element[$id]['rename']['#printed'] = FALSE;
  640. $element[$id]['remove']['#printed'] = FALSE;
  641. $rows[] = array(
  642. 'data' => array(
  643. drupal_render($element[$id]['label']),
  644. drupal_render($element[$id]['weight']),
  645. drupal_render($element[$id]['newlabel']),
  646. drupal_render($element[$id]['rename']),
  647. drupal_render($element[$id]['remove'])
  648. ),
  649. // Add draggable to each row to support the tabledrag behaviour
  650. 'class' => array('draggable'),
  651. );
  652. }
  653. // Create table header
  654. $header = array(t('pane'), t('Weight'), array('data' => t('Rename'), 'colspan' => 2), t('Action'));
  655. // Create a unique id for drupal_add_tabledrag() to find the table object
  656. $table_id = 'tripal_pane-arrange_pane_table';
  657. // Create output
  658. $output = '';
  659. if (count($rows) > 0) {
  660. $output = theme('table', array(
  661. 'header' => $header,
  662. 'rows' => $rows,
  663. 'attributes' => array('id' => $table_id),
  664. ));
  665. }
  666. // Call to the drupal_add_tabledrag
  667. drupal_add_tabledrag($table_id, 'order', 'sibling', 'tripal_pane-item-weight');
  668. return $output;
  669. }
  670. /**
  671. * Configure panes
  672. */
  673. function tripal_panes_action_configure_panes (&$form, &$form_state) {
  674. $panes = $form_state['values']['pane_items'];
  675. foreach ($panes AS $id => $pane) {
  676. $table_layout = array();
  677. foreach($pane AS $key => $value) {
  678. // Save layout information
  679. if (is_array($value) && key_exists('table_group', $value)) {
  680. if ($value['table_group'] == 1) {
  681. $table_layout[] = $key;
  682. }
  683. }
  684. // Save CSS class for each field if provided
  685. if (is_array($value) && key_exists('css_class', $value) && $value['css_class']) {
  686. db_update('tripal_pane_fields')
  687. ->fields(array(
  688. 'css_class' => $value['css_class']
  689. ))
  690. ->condition('pane_id',$id)
  691. ->condition('field_id', $value['field_id'])
  692. ->execute();
  693. }
  694. }
  695. $pane_settings = db_select('tripal_panes', 'tp')
  696. ->fields('tp', array('settings'))
  697. ->condition('pane_id', $id)
  698. ->execute()
  699. ->fetchField();
  700. $new_settings = unserialize($pane_settings);
  701. $new_settings['table_layout'] = $table_layout;
  702. db_update('tripal_panes')
  703. ->fields(array(
  704. 'settings' => serialize($new_settings)
  705. ))
  706. ->condition('pane_id', $id)
  707. ->execute();
  708. }
  709. }
  710. /**
  711. * Theme the Configure panes as a table
  712. *
  713. * @param unknown $variables
  714. * @return unknown
  715. */
  716. function theme_tripal_panes_form_configure_panes ($variables) {
  717. $element = $variables['element'];
  718. $rows = array();
  719. foreach (element_children($element) as $id) {
  720. $element[$id]['table_group']['#printed'] = FALSE;
  721. $element[$id]['css_class']['#printed'] = FALSE;
  722. $rows[] = array(
  723. 'data' => array(
  724. $element[$id]['#title'],
  725. drupal_render($element[$id]['table_group']),
  726. drupal_render($element[$id]['css_class']),
  727. ),
  728. );
  729. }
  730. // Create table header
  731. $header = array(t('Field'), t('Layout'), t('CSS Classes'));
  732. // Create a unique id for drupal_add_tabledrag() to find the table object
  733. $table_id = 'tripal_pane-configure_pane_table-' . strtolower(preg_replace('/\s+/', '_', $element['#title']));
  734. $table_class = 'tripal_pane-configure_pane_table';
  735. // Create output
  736. $table = '';
  737. if (count($rows) > 0) {
  738. $table = theme('table', array(
  739. 'header' => $header,
  740. 'rows' => $rows,
  741. 'attributes' => array('id' => $table_id, 'class' => array($table_class)),
  742. ));
  743. }
  744. $collapsible_item = array('element' => array());
  745. $collapsible_item['element']['#children'] = '';
  746. $collapsible_item['element']['#description'] =
  747. '<div id="tripal_panes-pane_configure-fieldset-instruction">
  748. 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.
  749. <div>'
  750. . $table;
  751. $collapsible_item['element']['#title'] = $element['#title'];
  752. // add 'collapsible' to the class setting to enable collapsible fieldset
  753. $collapsible_item['element']['#attributes']['class'] = array('tripal_panes-pane_configure-fieldset');
  754. $output = theme('fieldset', $collapsible_item);
  755. return $output;
  756. }
  757. /**
  758. * Implements hook_theme().
  759. */
  760. function tripal_panes_theme($existing, $type, $theme, $path) {
  761. return array(
  762. 'tripal_panes_form_arrange_panes' => array(
  763. 'render element' => 'element',
  764. ),
  765. 'tripal_panes_form_configure_panes' => array(
  766. 'render element' => 'element',
  767. ),
  768. 'tripal_panes_generic' => array(
  769. 'render element' => 'element',
  770. 'template' => 'tripal_panes_generic',
  771. 'path' => "$path/theme/templates",
  772. ),
  773. );
  774. }
  775. /**
  776. * Implements hook_entity_view.
  777. */
  778. function tripal_panes_entity_view($entity, $type, $view_mode, $langcode) {
  779. switch ($type) {
  780. case 'TripalEntity':
  781. // Use the generic template to render the fields
  782. if ($view_mode == 'full') {
  783. $bundle = db_select('tripal_bundle', 'tb')
  784. ->fields('tb', array('id', 'name', 'label'))
  785. ->condition('name', $entity->bundle)
  786. ->execute()
  787. ->fetchObject();
  788. tripal_panes_check_default_field_panes($bundle);
  789. $results = db_select('tripal_panes', 'tp')
  790. ->fields('tp', array('pane_id','name', 'label', 'settings'))
  791. ->condition('bundle_id', $bundle->id)
  792. ->orderBy('tp.weight', 'ASC')
  793. ->execute();
  794. $panes = array();
  795. $fields = array();
  796. $disabled_pane_id = 0;
  797. foreach ($results AS $row) {
  798. if ($row->name == 'te_disabled') {
  799. $disabled_pane_id = $row->pane_id;
  800. }
  801. else {
  802. $panes[$row->pane_id] = $row;
  803. $fields[$row->pane_id] = array();
  804. }
  805. }
  806. // Organize fields into panes
  807. foreach (element_children($entity->content) as $field_name) {
  808. $field_instance = field_info_instance ($type, $field_name, $entity->bundle);
  809. // Get default pane_id
  810. $default_pane_id = db_select('tripal_panes', 'tp')
  811. ->fields('tp', array('pane_id'))
  812. ->condition('bundle_id', $bundle->id)
  813. ->condition('name', 'te_base')
  814. ->execute()
  815. ->fetchField();
  816. // Get pane_id for the field
  817. $pane_id = db_select('tripal_pane_fields', 'tpf')
  818. ->fields('tpf', array('pane_id'))
  819. ->condition('field_id', $field_instance['id'])
  820. ->execute()
  821. ->fetchField();
  822. $pane_id = $pane_id ? $pane_id : $default_pane_id;
  823. // Do not show disabled fields
  824. if ($pane_id != $disabled_pane_id) {
  825. // Construct field elements
  826. $fields[$pane_id][$field_name] = $entity->content[$field_name];
  827. // Add extra css classes
  828. $css_class = db_select('tripal_pane_fields', 'tpf')
  829. ->fields('tpf', array('css_class'))
  830. ->condition('pane_id', $pane_id)
  831. ->condition('field_id', $field_instance['id'])
  832. ->execute()
  833. ->fetchField();
  834. $fields[$pane_id][$field_name]['#css_class'] = $css_class;
  835. }
  836. // Unset the field
  837. unset ($entity->content[$field_name]);
  838. }
  839. drupal_add_css(drupal_get_path('module','tripal_panes') . '/theme/css/tripal_panes.css');
  840. $entity->content['tripal_panes_generic'] = array(
  841. '#theme' => 'tripal_panes_generic',
  842. '#bundle' => $bundle,
  843. '#panes' => $panes,
  844. '#fields' => $fields,
  845. );
  846. }
  847. break;
  848. }
  849. }
  850. /**
  851. * Ajax callback to return pane's form elements when configuring pane
  852. */
  853. function tripal_panes_ajax_get_pane_setting_fieldset($form, &$form_state) {
  854. $pane_id = $form_state['values']['pane_select'];
  855. return $form['te_configure_panes']['pane_items'];
  856. }