tripal_panes.module 28 KB

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