tripal_panes.module 28 KB

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