tripal_panes.module 28 KB

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