tripal_entities.module 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. <?php
  2. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  3. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  4. /**
  5. * Implement hook_entity_info().
  6. */
  7. function tripal_entities_entity_info() {
  8. $entities = array();
  9. $entities['chado_data'] = array(
  10. // A human readable label to identify our entity.
  11. 'label' => t('Chado Data'),
  12. 'plural label' => t('Vocabulary Terms'),
  13. // The controller for our Entity, extending the Drupal core controller.
  14. 'controller class' => 'ChadoDataController',
  15. // The table for this entity defined in hook_schema()
  16. 'base table' => 'chado_data',
  17. // Returns the uri elements of an entity.
  18. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  19. // IF fieldable == FALSE, we can't attach fields.
  20. 'fieldable' => TRUE,
  21. // entity_keys tells the controller what database fields are used for key
  22. // functions. It is not required if we don't have bundles or revisions.
  23. // Here we do not support a revision, so that entity key is omitted.
  24. 'entity keys' => array(
  25. 'id' => 'entity_id',
  26. 'bundle' => 'type',
  27. ),
  28. 'bundle keys' => array(
  29. 'bundle' => 'type',
  30. ),
  31. // FALSE disables caching. Caching functionality is handled by Drupal core.
  32. 'static cache' => FALSE,
  33. // Bundles are defined by the model types below
  34. 'bundles' => array(),
  35. );
  36. // Bundles are alternative groups of fields or configuration
  37. // associated with a base entity type.
  38. // We want to dynamically add the bundles (or term types) to the entity.
  39. $values = array(
  40. 'cv_id' => array(
  41. 'name' => 'sequence'
  42. ),
  43. 'name' => 'gene',
  44. );
  45. $cvterm = chado_generate_var('cvterm', $values);
  46. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  47. $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  48. $entities['trp_vocabulary_term']['bundles'][$bundle_id] = array(
  49. 'label' => $label,
  50. 'admin' => array(
  51. 'path' => 'admin/structure/chado_data/manage',
  52. 'access arguments' => array('administer chado_data entities'),
  53. ),
  54. 'entity keys' => array(
  55. 'id' => 'entity_id',
  56. 'bundle' => 'bundle',
  57. ),
  58. );
  59. return $entities;
  60. }
  61. /**
  62. * Fetch a basic object.
  63. *
  64. * This function ends up being a shim between the menu system and
  65. * chado_data_load_multiple().
  66. *
  67. * @param int $entity_id
  68. * Integer specifying the basic entity id.
  69. * @param bool $reset
  70. * A boolean indicating that the internal cache should be reset.
  71. *
  72. * @return object
  73. * A fully-loaded $chado_data object or FALSE if it cannot be loaded.
  74. *
  75. */
  76. function chado_data_load($entity_id = NULL, $reset = FALSE) {
  77. $entity_ids = (isset($entity_id) ? array($entity_id) : array());
  78. $basic = chado_data_load_multiple($entity_ids, array(), $reset);
  79. return $basic ? reset($basic) : FALSE;
  80. }
  81. /**
  82. * Loads multiple basic entities.
  83. *
  84. * We only need to pass this request along to entity_load(), which
  85. * will in turn call the load() method of our entity controller class.
  86. */
  87. function chado_data_load_multiple($entity_ids = array(), $conditions = array(), $reset = FALSE){
  88. return entity_load('chado_data', $entity_ids, $conditions, $reset);
  89. }
  90. /**
  91. * Implements hook_menu().
  92. */
  93. function tripal_entities_menu() {
  94. // This provides a place for Field API to hang its own
  95. // interface and has to be the same as what was defined
  96. // in basic_entity_info() above.
  97. $items['admin/structure/chado_data/manage'] = array(
  98. 'title' => 'Chado Data',
  99. 'description' => t('Manage chado records, including default status, fields, settings, etc.'),
  100. 'page callback' => 'tripal_entities_list_entities',
  101. 'access arguments' => array('administer chado_data'),
  102. );
  103. // Add entities.
  104. $items['admin/structure/chado_data/manage/add'] = array(
  105. 'title' => 'Add Chado Data',
  106. 'page callback' => 'drupal_get_form',
  107. 'page arguments' => array('chado_data_form'),
  108. 'access arguments' => array('create chado_data entities'),
  109. 'type' => MENU_LOCAL_ACTION,
  110. );
  111. // List of all chado_data entities.
  112. $items['admin/structure/chado_data/manage/list'] = array(
  113. 'title' => 'List',
  114. 'type' => MENU_DEFAULT_LOCAL_TASK,
  115. );
  116. // The page to view our entities - needs to follow what
  117. // is defined in basic_uri and will use load_basic to retrieve
  118. // the necessary entity info.
  119. $items['chado_data/%chado_data'] = array(
  120. 'title callback' => 'chado_data_title',
  121. 'title arguments' => array(1),
  122. 'page callback' => 'chado_data_view',
  123. 'page arguments' => array(1),
  124. 'access arguments' => array('view chado_data'),
  125. 'type' => MENU_CALLBACK,
  126. );
  127. // 'View' tab for an individual entity page.
  128. $items['chado_data/%chado_data/view'] = array(
  129. 'title' => 'View',
  130. 'type' => MENU_DEFAULT_LOCAL_TASK,
  131. 'weight' => -10,
  132. );
  133. // 'Edit' tab for an individual entity page.
  134. $items['chado_data/%chado_data/edit'] = array(
  135. 'title' => 'Edit',
  136. 'page callback' => 'drupal_get_form',
  137. 'page arguments' => array('chado_data_form', 1),
  138. 'access arguments' => array('edit any chado_data entity'),
  139. 'type' => MENU_LOCAL_TASK,
  140. );
  141. // Add example entities.
  142. $items['examples/entity_example/basic/add'] = array(
  143. 'title' => 'Add Chado Data',
  144. 'page callback' => 'drupal_get_form',
  145. 'page arguments' => array('chado_data_form'),
  146. 'access arguments' => array('create chado_data entities'),
  147. );
  148. return $items;
  149. }
  150. /**
  151. * We save the entity by calling the controller.
  152. */
  153. function chado_data_save(&$entity) {
  154. return entity_get_controller('chado_data')->save($entity);
  155. }
  156. /**
  157. * Use the controller to delete the entity.
  158. */
  159. function chado_data_delete($entity) {
  160. entity_get_controller('chado_data')->delete($entity);
  161. }
  162. /**
  163. * Implements hook_permission().
  164. */
  165. function tripal_entities_permission() {
  166. $permissions = array(
  167. 'administer chado_data entities' => array(
  168. 'title' => t('Administer Chado data entity'),
  169. ),
  170. 'view any chado_data entity' => array(
  171. 'title' => t('View any Chado data entity'),
  172. ),
  173. 'edit any chado_data entity' => array(
  174. 'title' => t('Edit any Chado data entity'),
  175. ),
  176. 'create chado_data entities' => array(
  177. 'title' => t('Create Chado data entities'),
  178. ),
  179. );
  180. return $permissions;
  181. }
  182. /**
  183. * Returns a render array with all chado_data entities.
  184. *
  185. * @see pager_example.module
  186. */
  187. function tripal_entities_list_entities() {
  188. $content = array();
  189. // Load all of our entities.
  190. $entities = chado_data_load_multiple();
  191. if (!empty($entities)) {
  192. foreach ($entities as $entity) {
  193. // Create tabular rows for our entities.
  194. $rows[] = array(
  195. 'data' => array(
  196. 'id' => $entity->entity_id,
  197. 'title' => l($entity->title, 'chado_data/' . $entity->entity_id),
  198. 'type' => $entity->type,
  199. ),
  200. );
  201. }
  202. // Put our entities into a themed table. See theme_table() for details.
  203. $content['entity_table'] = array(
  204. '#theme' => 'table',
  205. '#rows' => $rows,
  206. '#header' => array(t('ID'), t('Item Description'), t('Bundle')),
  207. );
  208. }
  209. else {
  210. // There were no entities. Tell the user.
  211. $content[] = array(
  212. '#type' => 'item',
  213. '#markup' => t('No chado data entities currently exist.'),
  214. );
  215. }
  216. return $content;
  217. }
  218. /**
  219. *
  220. */
  221. function chado_data_title($entity){
  222. return $entity->title;
  223. }
  224. /**
  225. * Implements the uri callback.
  226. */
  227. function chado_data_uri($entity) {
  228. return array(
  229. 'path' => 'chado_data/' . $entity->entity_id,
  230. );
  231. }
  232. /**
  233. * Menu callback to display an entity.
  234. *
  235. * As we load the entity for display, we're responsible for invoking a number
  236. * of hooks in their proper order.
  237. *
  238. * @see hook_entity_prepare_view()
  239. * @see hook_entity_view()
  240. * @see hook_entity_view_alter()
  241. */
  242. function chado_data_view($entity, $view_mode = 'full') {
  243. // Our entity type, for convenience.
  244. $entity_type = 'chado_data';
  245. // Start setting up the content.
  246. $entity->content = array(
  247. '#view_mode' => $view_mode,
  248. );
  249. // Build fields content - this is where the Field API really comes in to play.
  250. // The task has very little code here because it all gets taken care of by
  251. // field module. field_attach_prepare_view() lets the fields load any
  252. // data they need before viewing.
  253. field_attach_prepare_view($entity_type, array($entity->entity_id => $entity),
  254. $view_mode);
  255. // We call entity_prepare_view() so it can invoke hook_entity_prepare_view()
  256. // for us.
  257. entity_prepare_view($entity_type, array($entity->entity_id => $entity));
  258. // Now field_attach_view() generates the content for the fields.
  259. $entity->content += field_attach_view($entity_type, $entity, $view_mode);
  260. // OK, Field API done, now we can set up some of our own data.
  261. // $entity->content['created'] = array(
  262. // '#type' => 'item',
  263. // '#title' => t('Created date'),
  264. // '#markup' => format_date($entity->created),
  265. // );
  266. // Now to invoke some hooks. We need the language code for
  267. // hook_entity_view(), so let's get that.
  268. global $language;
  269. $langcode = $language->language;
  270. // And now invoke hook_entity_view().
  271. module_invoke_all('entity_view', $entity, $entity_type, $view_mode, $langcode);
  272. // Now invoke hook_entity_view_alter().
  273. drupal_alter(array('chado_data', 'entity_view'), $entity->content, $entity_type);
  274. // And finally return the content.
  275. return $entity->content;
  276. }
  277. /**
  278. *
  279. */
  280. function chado_data_page_view($entity, $view_mode = 'full'){
  281. $entity->content = array();
  282. // Build fields content.
  283. field_attach_prepare_view('chado_data', array($entity->entity_id => $entity), $view_mode);
  284. entity_prepare_view('chado_data', array($entity->entity_id => $entity));
  285. $entity->content += field_attach_view('chado_data', $entity, $view_mode);
  286. return $entity->content;
  287. }
  288. /**
  289. *
  290. */
  291. function tripal_entities_vocbulary_term_uri($entity) {
  292. return array(
  293. 'path' => 'chado_data/' . $entity->entity_id,
  294. );
  295. }
  296. /**
  297. *
  298. */
  299. function chado_data_form($form, &$form_state, $entity = NULL) {
  300. // Set the defaults.
  301. $cv_id = NULL;
  302. $term_name = NULL;
  303. $entity_id = NULL;
  304. $cvterm = NULL;
  305. // Set defaults if an entity was provided.
  306. if ($entity) {
  307. $entity_id = $entity->entity_id;
  308. $values = array('cvterm_id' => $entity->cvterm_id);
  309. $cvterm = chado_generate_var('cvterm', $values);
  310. $cv_id = $cvterm->cv_id->cv_id;
  311. $term_name = $cvterm->name;
  312. }
  313. // Set defaults using the form state.
  314. if (array_key_exists('values', $form_state)) {
  315. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
  316. $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
  317. // Get the cvterm that matches
  318. $values = array(
  319. 'cv_id' => $cv_id,
  320. 'name' => $term_name
  321. );
  322. $cvterm = chado_generate_var('cvterm', $values);
  323. }
  324. // Add in the IDs for the entity.
  325. if ($entity) {
  326. $form['entity_id'] = array(
  327. '#type' => 'hidden',
  328. '#value' => $entity_id,
  329. );
  330. $form['record_id'] = array(
  331. '#type' => 'hidden',
  332. '#value' => $entity->record_id,
  333. );
  334. }
  335. // Let the user select the vocabulary and chado_data but only if they haven't
  336. // already selected a chado_data.
  337. $cvs = tripal_get_cv_select_options();
  338. if (!$term_name) {
  339. $form['cv_id'] = array(
  340. '#type' => 'select',
  341. '#title' => t('Vocabulary'),
  342. '#options' => $cvs,
  343. '#required' => TRUE,
  344. '#description' => t('Select a vocabulary that contains the term for the type of data you want to add.'),
  345. '#default_value' => $cv_id,
  346. '#ajax' => array(
  347. 'callback' => "chado_data_form_ajax_callback",
  348. 'wrapper' => 'chado_data_form',
  349. 'effect' => 'fade',
  350. 'method' => 'replace'
  351. )
  352. );
  353. }
  354. // If we have a CV ID then we want to provide an autocomplete field
  355. if ($cv_id and !$term_name) {
  356. $form['cvterm_select']['term_name'] = array(
  357. '#title' => t('Record Type'),
  358. '#type' => 'textfield',
  359. '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."),
  360. '#required' => TRUE,
  361. '#default_value' => $term_name,
  362. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  363. );
  364. $form['cvterm_select']['select_button'] = array(
  365. '#type' => 'submit',
  366. '#value' => t('Use this term'),
  367. '#name' => 'select_cvterm',
  368. );
  369. }
  370. // Once the CV term is selected then provide the other fields.
  371. if ($cvterm) {
  372. $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  373. $form['cv_id'] = array(
  374. '#type' => 'hidden',
  375. '#value' => $cv_id,
  376. );
  377. $form['term_name'] = array(
  378. '#type' => 'hidden',
  379. '#value' => $term_name,
  380. );
  381. $form['cvterm_id'] = array(
  382. '#type' => 'hidden',
  383. '#value' => $cvterm->cvterm_id,
  384. );
  385. $form['type'] = array(
  386. '#type' => 'hidden',
  387. '#value' => $bundle_id,
  388. );
  389. $form['cv_name_shown'] = array(
  390. '#type' => 'item',
  391. '#title' => 'Vocabulary',
  392. '#markup' => $cvterm->cv_id->name,
  393. '#weight' => -100,
  394. );
  395. $form['term_name_shown'] = array(
  396. '#type' => 'item',
  397. '#title' => 'Record Type',
  398. '#markup' => $cvterm->name,
  399. '#weight' => -100,
  400. );
  401. /*
  402. // Drupal field types and settings:
  403. // https://www.drupal.org/node/1879542
  404. $field = array(
  405. 'field_name' => 'feature__name',
  406. 'type' => 'text',
  407. 'cardinality' => 1,
  408. 'storage' => array(
  409. 'type' => 'tripal_entities_storage'
  410. ),
  411. );
  412. field_create_field($field);
  413. $field_instance = array(
  414. 'field_name' => 'feature__name',
  415. 'label' => 'Name',
  416. 'widget' => array(
  417. 'type' => 'text_textfield'
  418. ),
  419. 'entity_type' => 'chado_data',
  420. 'required' => 'true',
  421. 'settings' => array(
  422. 'max_length' => 255
  423. ),
  424. 'bundle' => $bundle_id,
  425. );
  426. field_create_instance($field_instance);
  427. $field = array(
  428. 'field_name' => 'feature__uniquename',
  429. 'type' => 'text',
  430. 'cardinality' => 1,
  431. 'storage' => array(
  432. 'type' => 'tripal_entities_storage'
  433. ),
  434. );
  435. field_create_field($field);
  436. $field_instance = array(
  437. 'field_name' => 'feature__uniquename',
  438. 'label' => 'Unique Name',
  439. 'widget' => array(
  440. 'type' => 'text_textfield'
  441. ),
  442. 'entity_type' => 'chado_data',
  443. 'required' => 'true',
  444. 'settings' => array(
  445. 'max_length' => 255
  446. ),
  447. 'bundle' => $bundle_id,
  448. );
  449. field_create_instance($field_instance);
  450. $field = array(
  451. 'field_name' => 'feature__organism_id',
  452. 'type' => 'organism_id',
  453. 'cardinality' => 1,
  454. 'storage' => array(
  455. 'type' => 'tripal_entities_storage'
  456. ),
  457. );
  458. field_create_field($field);
  459. $field_instance = array(
  460. 'field_name' => 'feature__organism_id',
  461. 'label' => 'Organism',
  462. 'entity_type' => 'chado_data',
  463. 'required' => 'true',
  464. 'settings' => array(),
  465. 'bundle' => $bundle_id,
  466. );
  467. field_create_instance($field_instance);
  468. */
  469. // Create the object for this entity instance. The entity instance type
  470. // is always the name of the term.
  471. if (!$entity) {
  472. $entity = new stdClass();
  473. $entity->entity_id = NULL;
  474. $entity->type = $bundle_id;
  475. }
  476. $form['#parents'] = array('attached');
  477. field_attach_form('chado_data', $entity, $form, $form_state);
  478. $form['submit'] = array(
  479. '#type' => 'submit',
  480. '#value' => t('Add a new ' . $cvterm->name),
  481. '#name' => 'add_data',
  482. '#weight' => 1000
  483. );
  484. }
  485. $form['#prefix'] = '<div id="chado_data_form">';
  486. $form['#suffix'] = '</div>';
  487. return $form;
  488. }
  489. /**
  490. * Implements hook_field_info().
  491. */
  492. function tripal_entities_field_info() {
  493. $fields = array(
  494. 'organism_id' => array(
  495. 'label' => t('Organism'),
  496. 'description' => t('A field for specifying an organism.'),
  497. 'default_widget' => 'tripal_entities_organism_select_widget',
  498. 'default_formatter' => 'tripal_entities_organism_formatter',
  499. 'settings' => array(),
  500. 'storage' => array(
  501. 'type' => 'tripal_entities_storage',
  502. 'module' => 'tripal_entities',
  503. 'active' => TRUE
  504. ),
  505. ),
  506. );
  507. return $fields;
  508. }
  509. /**
  510. * Implements hook_field_widget_info().
  511. */
  512. function tripal_entities_field_widget_info() {
  513. return array(
  514. 'tripal_entities_organism_select_widget' => array(
  515. 'label' => t('Organism Select'),
  516. 'field types' => array('organism_id')
  517. ),
  518. );
  519. }
  520. /**
  521. * Implements hook_field_formatter_info().
  522. */
  523. function tripal_entities_field_formatter_info() {
  524. return array(
  525. 'tripal_entities_organism_formatter' => array(
  526. 'label' => t('Organism'),
  527. 'field types' => array('organism_id')
  528. ),
  529. );
  530. }
  531. /**
  532. * Implements hook_field_formatter_view().
  533. *
  534. * Two formatters are implemented.
  535. * - field_example_simple_text just outputs markup indicating the color that
  536. * was entered and uses an inline style to set the text color to that value.
  537. * - field_example_color_background does the same but also changes the
  538. * background color of div.region-content.
  539. *
  540. * @see field_example_field_formatter_info()
  541. */
  542. function tripal_entities_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  543. $element = array();
  544. switch ($display['type']) {
  545. // This formatter simply outputs the field as text and with a color.
  546. case 'tripal_entities_organism_formatter':
  547. foreach ($items as $delta => $item) {
  548. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  549. $element[$delta] = array(
  550. // We create a render array to produce the desired markup,
  551. // "<p>Genus Species</p>".
  552. // See theme_html_tag().
  553. '#type' => 'html_tag',
  554. '#tag' => 'p',
  555. '#value' => '<i>' . $organism[0]->genus .' ' . $organism[0]->species . '</i>',
  556. );
  557. }
  558. break;
  559. }
  560. return $element;
  561. }
  562. /**
  563. * Implements hook_field_widget_form().
  564. */
  565. function tripal_entities_field_widget_form(&$form, &$form_state, $field,
  566. $instance, $langcode, $items, $delta, $element) {
  567. $widget = $element;
  568. $widget['#delta'] = $delta;
  569. switch ($instance['widget']['type']) {
  570. case 'tripal_entities_organism_select_widget':
  571. $options = tripal_get_organism_select_options();
  572. $widget += array(
  573. '#type' => 'select',
  574. '#title' => $element['#title'],
  575. '#description' => $element['#description'],
  576. '#options' => $options,
  577. '#default_value' => $items[0]['value'],
  578. '#required' => $element['#required'],
  579. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  580. '#delta' => $delta,
  581. '#element_validate' => array('tripal_entities_organism_select_widget_validate'),
  582. );
  583. $element['value'] = $widget;
  584. break;
  585. }
  586. return $element;
  587. }
  588. /**
  589. * Callback function for validating the tripal_entities_organism_select_widget.
  590. */
  591. function tripal_entities_organism_select_widget_validate($element, &$form_state) {
  592. $field_name = $element['#field_name'];
  593. // Make sure we have a valid organism
  594. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  595. foreach ($items as $delta => $value) {
  596. $organism_id = chado_select_record('organism', array('organism_id'),
  597. array('organism_id' => $value['value']), array('has_record' => TRUE));
  598. if (!$organism_id) {
  599. form_error($element, t("Please specify an organism that already exists in the database."));
  600. }
  601. }
  602. }
  603. }
  604. /**
  605. * Implements hook_field_is_empty().
  606. */
  607. function tripal_entities_field_is_empty($item, $field) {
  608. if (empty($item['tripal_entities_organism_select_widget'])) {
  609. return TRUE;
  610. }
  611. }
  612. /**
  613. * Implements hook_field_storage_info().
  614. */
  615. function tripal_entities_field_storage_info() {
  616. return array(
  617. 'tripal_entities_storage' => array(
  618. 'label' => t('Chado storage'),
  619. 'description' => t('Stores fields in the local Chado database.'),
  620. 'settings' => array(),
  621. ),
  622. );
  623. }
  624. /**
  625. * Implements hook_field_storage_write().
  626. */
  627. function tripal_entities_field_storage_write($entity_type, $entity, $op, $fields) {
  628. // Get the IDs for this entity.
  629. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  630. // Find out which table should receive the insert.
  631. $tablename = 'feature';
  632. $type_field = 'type_id';
  633. $schema = chado_get_schema($tablename);
  634. $pkey_field = $schema['primary key'][0];
  635. // Construct the values array that will be used to insert into the table.
  636. $values = array();
  637. foreach ($fields as $field_id) {
  638. $field = field_info_field_by_id($field_id);
  639. $field_name = $field['field_name'];
  640. $matches = array();
  641. if (preg_match('/^' . $tablename . '__(.*)/', $field_name, $matches)) {
  642. $chado_field = $matches[1];
  643. // Currently, we only support one language, but for for the sake of
  644. // thoroughness we'll iterate through all possible languages.
  645. $all_languages = field_available_languages($entity_type, $field);
  646. $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
  647. foreach ($field_languages as $langcode) {
  648. $items = (array) $entity->{$field_name}[$langcode];
  649. // The number of items is related to the cardinatily of the field.
  650. foreach ($items as $delta => $item) {
  651. $values[$chado_field] = $item['value'];
  652. }
  653. }
  654. }
  655. }
  656. // Add in the type_id field.
  657. $values[$type_field] = $entity->cvterm_id;
  658. switch ($op) {
  659. case FIELD_STORAGE_INSERT:
  660. $record = chado_insert_record($tablename, $values);
  661. if ($record === FALSE) {
  662. drupal_set_message('Could not insert Chado record.', 'error');
  663. }
  664. $entity->record_id = $record[$pkey_field];
  665. break;
  666. case FIELD_STORAGE_UPDATE:
  667. $match[$pkey_field] = $entity->record_id;
  668. chado_update_record($tablename, $match, $values);
  669. break;
  670. }
  671. }
  672. /**
  673. * Implements hook_field_storage_load().
  674. *
  675. * Responsible for loading the fields from the Chado database and adding
  676. * their values to the entity.
  677. */
  678. function tripal_entities_field_storage_load($entity_type, $entities, $age, $fields, $options) {
  679. $load_current = $age == FIELD_LOAD_CURRENT;
  680. global $language;
  681. $langcode = $language->language;
  682. foreach ($entities as $entity_id => $entity) {
  683. // Find out which table should receive the insert.
  684. $tablename = 'feature';
  685. $type_field = 'type_id';
  686. $schema = chado_get_schema($tablename);
  687. $pkey_field = $schema['primary key'][0];
  688. $record_id = $entity->record_id;
  689. // Iterate through the field names to get the list of tables and fields
  690. // that should be queried.
  691. $columns = array();
  692. foreach ($fields as $field_id => $ids) {
  693. // By the time this hook runs, the relevant field definitions have been
  694. // populated and cached in FieldInfo, so calling field_info_field_by_id()
  695. // on each field individually is more efficient than loading all fields in
  696. // memory upfront with field_info_field_by_ids().
  697. $field = field_info_field_by_id($field_id);
  698. $field_name = $field['field_name'];
  699. $matches = array();
  700. if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
  701. $table = $matches[1];
  702. $field = $matches[2];
  703. $columns[$table][] = $field;
  704. }
  705. }
  706. // Get the record
  707. $record = chado_select_record($tablename, $columns[$tablename], array($pkey_field => $entity->record_id));
  708. // Now set the field values
  709. foreach ($fields as $field_id => $ids) {
  710. $field = field_info_field_by_id($field_id);
  711. $field_name = $field['field_name'];
  712. $matches = array();
  713. if (preg_match('/^(.*?)__(.*?)$/', $field_name, $matches)) {
  714. $table = $matches[1];
  715. $field = $matches[2];
  716. $entity->{$field_name}['und'][] = array('value' => $record[0]->$field);
  717. }
  718. }
  719. }
  720. }
  721. /**
  722. * An Ajax callback for the chado_data_form.
  723. */
  724. function chado_data_form_ajax_callback($form, $form_state) {
  725. // return the form so Drupal can update the content on the page
  726. return $form;
  727. }
  728. /**
  729. * Implements hook_validate() for the chado_data_form.
  730. */
  731. function chado_data_form_validate($form, &$form_state) {
  732. if ($form_state['clicked_button']['#name'] == 'add_data') {
  733. $chado_data = (object) $form_state['values'];
  734. field_attach_form_validate('chado_data', $chado_data, $form, $form_state);
  735. }
  736. }
  737. /**
  738. * Implements hook_submit() for the chado_data_form.
  739. *
  740. */
  741. function chado_data_form_submit($form, &$form_state) {
  742. if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
  743. // don't do anything, we just need to know what the term name is.
  744. $form_state['rebuild'] = TRUE;
  745. }
  746. else {
  747. // Build and entity instance object.
  748. $entity = (object) $form_state['values'];
  749. // This is an update if the entity_id is in the form_state.
  750. if (array_key_exists('entity_id', $form_state['values'])) {
  751. $entity->entity_id = $form_state['values']['entity_id'];
  752. field_attach_update('chado_data', $entity);
  753. }
  754. else {
  755. field_attach_insert('chado_data', $entity);
  756. }
  757. // Now save the entity
  758. $entity = chado_data_save($entity);
  759. $form_state['redirect'] = "chado_data/$entity->entity_id";
  760. }
  761. }
  762. /**
  763. * Implements hook_theme().
  764. */
  765. function tripal_entities_theme($existing, $type, $theme, $path) {
  766. return array(
  767. );
  768. }
  769. /**
  770. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  771. */
  772. function tripal_entities_rdf_mapping() {
  773. return array();
  774. /* return array(
  775. 'type' => 'chado_data',
  776. 'bundle' => 'gene',
  777. 'mapping' => array(
  778. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  779. 'title' => array(
  780. 'predicates' => array('dc:title'),
  781. ),
  782. 'uid' => array(
  783. 'predicates' => array('sioc:has_creator'),
  784. 'type' => 'rel',
  785. ),
  786. 'name' => array(
  787. 'predicates' => array('foaf:name'),
  788. ),
  789. 'uniquename' => array(
  790. 'predicates' => array('foaf:name'),
  791. ),
  792. 'organism_id' => array(
  793. 'predicates' => array('sioc:has_parent'),
  794. 'type' => 'rel'
  795. )
  796. ),
  797. ); */
  798. }
  799. /**
  800. * ChadoDataControllerInterface definition.
  801. *
  802. * We create an interface here because anyone could come along and
  803. * use hook_entity_info_alter() to change our controller class.
  804. * We want to let them know what methods our class needs in order
  805. * to function with the rest of the module, so here's a handy list.
  806. *
  807. * @see hook_entity_info_alter()
  808. */
  809. interface ChadoDataControllerInterface
  810. extends DrupalEntityControllerInterface {
  811. /**
  812. * Create an entity.
  813. */
  814. public function create();
  815. /**
  816. * Save an entity.
  817. *
  818. * @param object $entity
  819. * The entity to save.
  820. */
  821. public function save($entity);
  822. /**
  823. * Delete an entity.
  824. *
  825. * @param object $entity
  826. * The entity to delete.
  827. */
  828. public function delete($entity);
  829. }
  830. /**
  831. * ChadoDataController extends DrupalDefaultEntityController.
  832. *
  833. * Our subclass of DrupalDefaultEntityController lets us add a few
  834. * important create, update, and delete methods.
  835. */
  836. class ChadoDataController
  837. extends DrupalDefaultEntityController
  838. implements ChadoDataControllerInterface {
  839. /**
  840. * Create and return a new tripal_entities entity.
  841. */
  842. public function create() {
  843. $entity = new stdClass();
  844. $entity->type = 'chado_data';
  845. $entity->entity_id = 0;
  846. return $entity;
  847. }
  848. /**
  849. * Delete a single entity.
  850. *
  851. * Really a convenience function for deleteMultiple().
  852. */
  853. public function delete($entity) {
  854. $this->deleteMultiple(array($entity));
  855. }
  856. /**
  857. * Delete one or more tripal_entities entities.
  858. *
  859. * Deletion is unfortunately not supported in the base
  860. * DrupalDefaultEntityController class.
  861. *
  862. * @param array $entities
  863. * An array of entity IDs or a single numeric ID.
  864. */
  865. public function deleteMultiple($entities) {
  866. $entity_ids = array();
  867. if (!empty($entities)) {
  868. $transaction = db_transaction();
  869. try {
  870. foreach ($entities as $entity) {
  871. // Invoke hook_entity_delete().
  872. module_invoke_all('entity_delete', $entity, 'chado_data');
  873. field_attach_delete('chado_data', $entity);
  874. $entity_ids[] = $entity->entity_id;
  875. }
  876. db_delete('chado_data')
  877. ->condition('entity_id', $entity_ids, 'IN')
  878. ->execute();
  879. }
  880. catch (Exception $e) {
  881. $transaction->rollback();
  882. watchdog_exception('entity_example', $e);
  883. throw $e;
  884. }
  885. }
  886. }
  887. /**
  888. * Saves the custom fields using drupal_write_record().
  889. */
  890. public function save($entity) {
  891. global $user;
  892. // If our entity has no entity_id, then we need to give it a
  893. // time of creation.
  894. if (empty($entity->entity_id)) {
  895. $entity->created = time();
  896. $invocation = 'entity_insert';
  897. }
  898. else {
  899. $invocation = 'entity_update';
  900. }
  901. // Invoke hook_entity_presave().
  902. module_invoke_all('entity_presave', $entity, 'chado_data');
  903. // Write out the entity record.
  904. $tablename = 'feature';
  905. $type_field = 'type_id';
  906. $schema = chado_get_schema($tablename);
  907. $pkey_field = $schema['primary key'][0];
  908. $record = array(
  909. 'cvterm_id' => $entity->cvterm_id,
  910. 'type' => $entity->type,
  911. 'tablename' => $tablename,
  912. 'record_id' => $entity->record_id,
  913. 'title' => 'title',
  914. 'uid' => $user->uid,
  915. 'created' => $entity->created,
  916. 'changed' => time(),
  917. );
  918. $success = drupal_write_record('chado_data', $record);
  919. if ($success == SAVED_NEW) {
  920. $entity->entity_id = $record['entity_id'];
  921. }
  922. // Now we need to either insert or update the fields which are
  923. // attached to this entity. We use the same primary_keys logic
  924. // to determine whether to update or insert, and which hook we
  925. // need to invoke.
  926. if ($invocation == 'entity_insert') {
  927. field_attach_insert('chado_data', $entity);
  928. }
  929. else {
  930. field_attach_update('chado_data', $entity);
  931. }
  932. // Invoke either hook_entity_update() or hook_entity_insert().
  933. module_invoke_all($invocation, $entity, 'chado_data');
  934. return $entity;
  935. }
  936. }