tripal.entities.api.inc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for working with
  5. * TripalEntity content types (bundles) and their entities.
  6. */
  7. /**
  8. * @defgroup tripal_entities_api Tripal Entities
  9. * @ingroup tripal_api
  10. * @{
  11. * Provides an application programming interface (API) for working with
  12. * TripalEntity content types (bundles) and their entities.
  13. * @}
  14. *
  15. */
  16. /**
  17. * Allows a module to make changes to an entity object after creation.
  18. *
  19. * This function is added by Tripal to allow datastore backends to add
  20. * addition properties to the entity that they themselves will use later.
  21. *
  22. * @param $entity
  23. * @param $entity_type
  24. *
  25. * @ingroup tripal_entities_api
  26. */
  27. function hook_entity_create(&$entity, $entity_type) {
  28. }
  29. /**
  30. * Allows a module to perform tasks after a TripalBundle object is created.
  31. *
  32. * @param $bundle
  33. * The newly created TripalBundle object.
  34. * @param $storage_args
  35. * Arguments passed to the storage backend for this bundle. These arguments
  36. * typically provide details for how to associate this bundle with records
  37. * in the storage system. Each storage system will have its own set of
  38. * arguments that it will expect.
  39. *
  40. * @ingroup tripal_entities_api
  41. */
  42. function hook_bundle_create(&$bundle, $storage_args) {
  43. }
  44. /**
  45. * Allows a module to perform tasks after fields are added to a TripalBundle.
  46. *
  47. * @param $bundle
  48. * The newly created TripalBundle object.
  49. *
  50. * @ingroup tripal_entities_api
  51. */
  52. function hook_bundle_postcreate(&$bundle) {
  53. }
  54. /**
  55. * Allows a module to add admin notifications to the associated tripal table
  56. * during the cron run.
  57. *
  58. * @ingroup tripal_entities_api
  59. */
  60. function hook_tripal_cron_notification() {
  61. }
  62. /**
  63. * Allows a module to perform tasks before a TripalBundle object is deleted.
  64. *
  65. * @param $bundle
  66. * The newly created TripalBundle object.
  67. *
  68. * @ingroup tripal_entities_api
  69. */
  70. function hook_bundle_delete($bundle) {
  71. }
  72. /**
  73. * Implement this hook to define default formats for Tripal Content Types.
  74. *
  75. * @param TripalBundle $bundle
  76. * A tripal content type entity with information to be used for determining the default title format.
  77. * @param array $available_tokens
  78. * An array of available tokens for this particular tripal content type.
  79. *
  80. * @return array
  81. * An array of potential formats. The lightest weighted format suggested by all modules will be chosen.
  82. * Each array item should consist of a 'weight' and 'format'. See the hook implementation below
  83. * for examples.
  84. * - weight: an integer used to determine priority of suggestions.
  85. * The smaller/lighter the number the higher the priority.
  86. * Best practice is to use a weight less than 0 for extension modules.
  87. * specifically, -2 is a good weight for calculated formats and -5 is a
  88. * good weight for hard-coded formats specific to a given type.
  89. * - format: a string including approved tokens used to determine the title
  90. * on Tripal content pages.
  91. *
  92. * @ingroup tripal_entities_api
  93. */
  94. function hook_tripal_default_title_format($bundle, $available_tokens) {
  95. $format = array();
  96. // If you want to suggest a default format for a particular vocabulary term:
  97. //---------------------------------------------------------------------------
  98. // Load the term associated with this Tripal Content type.
  99. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  100. $term = reset($term);
  101. // If it's the term you are interested in then suggest a format.
  102. if ($term->name == 'organism') {
  103. // To suggest a format, add an element to the array with a format & weight key.
  104. $format[] = array(
  105. // This is the format/pattern you suggest be used to determine the title of organism pages.
  106. 'format' => '[organism__genus] [organism__species]',
  107. // The weight/priority of your suggestion.
  108. 'weight' => -5
  109. );
  110. }
  111. // Say you know that in your particular site, all 'names' are required
  112. // and you want to only use the human-readable name:
  113. //---------------------------------------------------------------------------
  114. $name_field = preg_grep('/__name]$/', array_keys($available_tokens));
  115. $name_field = reset($name_field);
  116. if (is_string($name_field)) {
  117. $format[] = array(
  118. 'format' => $name_field,
  119. 'weight' => -2,
  120. );
  121. }
  122. return $format;
  123. }
  124. /**
  125. * A replacement for the entity_load function of Drupal.
  126. *
  127. * This function should be used for loading of Tripal Entities. It provides
  128. * greater control to limit which fields are loaded with the entity. The
  129. * entity_load() function of Drupal will automatically attach all fields at
  130. * once but this may not be desired as some fields can be comples and large and
  131. * the site developer may desire loading of fields via AJAX or the user of
  132. * web services may wish to specify the fields they want to include.
  133. *
  134. * @param $entity_type:
  135. * The entity type to load, e.g. node or user.
  136. * @param $ids
  137. * An array of entity IDs, or FALSE to load all entities.
  138. * @param $reset: Whether to reset the internal cache for the requested entity
  139. * type. Defaults to FALSE.
  140. * @param $field_ids
  141. * A list of numeric field IDs that should be loaded. The
  142. * TripalField named 'content_type' is always automatically added.
  143. *
  144. * @return
  145. * An array of entity objects indexed by their ids. When no results are
  146. * found, an empty array is returned.
  147. *
  148. * @ingroup tripal_entities_api
  149. */
  150. function tripal_load_entity($entity_type, $ids = FALSE, $reset = FALSE,
  151. $field_ids = array()) {
  152. // The $conditions is deprecated in the funtion arguments of entity_load
  153. // so it was removed from the parameters of this function as well. But
  154. // the load() function of the entity controller still expects it so set it
  155. // to an empty array.
  156. $conditions = array();
  157. // If this isn't a TripalEntity then just load it the old fashioned way
  158. // although caching will not be used if it not specifically set to FALSE.
  159. if ($entity_type != 'TripalEntity') {
  160. return entity_load($entity_type, $ids, $conditions, $reset);
  161. }
  162. // Get the entity controller and clear the cache if requested (default).
  163. $ec = entity_get_controller($entity_type);
  164. if ($reset) {
  165. $ec->resetCache();
  166. }
  167. return $ec->load($ids, $conditions, $field_ids);
  168. }
  169. /**
  170. * Retrieves a TripalTerm entity that matches the given arguments.
  171. *
  172. * @param $values
  173. * An associative array used to match a term. Valid keys may be 'vocabulary',
  174. * 'accession, or 'term_id'. The keys 'vocabulary' and 'accession' must
  175. * always be used together to uniquely identify a term. The key 'term_id'
  176. * can be used alone to uniquely identify a term.
  177. *
  178. * @return
  179. * A TripalTerm entity object or NULL if not found.
  180. *
  181. * @ingroup tripal_entities_api
  182. */
  183. function tripal_load_term_entity($values) {
  184. $vocabulary = array_key_exists('vocabulary', $values) ? $values['vocabulary'] : '';
  185. $accession = array_key_exists('accession', $values) ? $values['accession'] : '';
  186. $term_id = array_key_exists('term_id', $values) ? $values['term_id'] : '';
  187. $term = NULL;
  188. if ($vocabulary and $accession) {
  189. $query = db_select('tripal_term', 'tt');
  190. $query->join('tripal_vocab', 'tv', 'tv.id = tt.vocab_id');
  191. $query->fields('tt', array('id'))
  192. ->fields('tv', array('vocabulary'))
  193. ->condition('tv.vocabulary', $vocabulary)
  194. ->condition('tt.accession', $accession);
  195. $term = $query->execute()->fetchObject();
  196. }
  197. else if ($term_id) {
  198. $query = db_select('tripal_term', 'tt');
  199. $query->fields('tt', array('id'))
  200. ->condition('tt.id', $term_id);
  201. $term = $query->execute()->fetchObject();
  202. }
  203. if ($term) {
  204. $entity = entity_load('TripalTerm', array($term->id));
  205. return reset($entity);
  206. }
  207. return NULL;
  208. }
  209. /**
  210. * Retrieves a TripalVocab entity that maches the given arguments.
  211. *
  212. * @param $values
  213. * An associative array used to match a vocabulary. The valid keys are
  214. * 'vocab_id' and 'vocabulary'.
  215. *
  216. * @return
  217. * A TripalVocab entity object or NULL if not found.
  218. *
  219. * @ingroup tripal_entities_api
  220. */
  221. function tripal_load_vocab_entity($values) {
  222. $vocabulary = array_key_exists('vocabulary', $values) ? $values['vocabulary'] : '';
  223. $vocab_id = array_key_exists('vocab_id', $values) ? $values['vocab_id'] : '';
  224. $vocab = NULL;
  225. $query= db_select('tripal_vocab', 'tv')
  226. ->fields('tv');
  227. if ($vocabulary) {
  228. $query->condition('tv.vocabulary', $vocabulary);
  229. }
  230. if ($vocab_id) {
  231. $query->condition('tv.id', $vocab_id);
  232. }
  233. $vocab = $query->execute()->fetchObject();
  234. if ($vocab) {
  235. $entity = entity_load('TripalVocab', array($vocab->id));
  236. return reset($entity);
  237. }
  238. return NULL;
  239. }
  240. /**
  241. * Retrieves a TripalBundle entity that matches the given arguments.
  242. *
  243. * @param $values
  244. * An associative array used to match a bundle. Valid keys may:
  245. * - id: the numeric id of the bundle.
  246. * - name: the bundle name (e.g. 'bio_data_234')
  247. * - label: the bundle label (e.g. 'Organism')
  248. * - term_id: the term ID to which the bundle belongs
  249. *
  250. * @return
  251. * A TripalBundle entity object or NULL if not found.
  252. *
  253. * @ingroup tripal_entities_api
  254. */
  255. function tripal_load_bundle_entity($values) {
  256. $query = db_select('tripal_bundle', 'tb');
  257. $query->fields('tb');
  258. if (array_key_exists('id', $values)) {
  259. $query->condition('tb.id', $values['id']);
  260. }
  261. if (array_key_exists('name', $values)) {
  262. $query->condition('tb.name', $values['name']);
  263. }
  264. if (array_key_exists('label', $values)) {
  265. $query->condition('tb.label', $values['label']);
  266. }
  267. if (array_key_exists('term_id', $values)) {
  268. $query->condition('tb.term_id', $values['term_id']);
  269. }
  270. $bundle = $query->execute()->fetchObject();
  271. if ($bundle) {
  272. $entity = entity_load_unchanged('TripalBundle', $bundle->id);
  273. return $entity;
  274. }
  275. return NULL;
  276. }
  277. /**
  278. * Allows a module to write to the admin notification table
  279. * during the cron run.
  280. *
  281. * @param $title
  282. * A generic phrase indicating what the notification is for.
  283. * @param $details
  284. * A human-readable sentence or two describing the issue.
  285. * @param $type
  286. * A one word type indicating the type of notification. Tripal types include: Jobs, Fields.
  287. * If not type is required please pass NULL.
  288. * @param $actions
  289. * A serialized PHP associative array containing the link and URL for each action.
  290. * If not type is required please pass NULL.
  291. * @param $submitter_id
  292. * A unique ID provided by the submitter for checking to make sure that the notification is not added more than once.
  293. *
  294. * @ingroup tripal_entities_api
  295. */
  296. function tripal_add_notification($title, $details, $type, $actions, $submitter_id) {
  297. $transaction = db_transaction();
  298. // Check the notification isn't already in the admin notification table.
  299. $dedup = db_select('tripal_admin_notfications', 'tan')
  300. ->fields('tan')
  301. ->condition('submitter_id', $submitter_id, '=')
  302. ->execute()->fetchAll();
  303. if (empty($dedup)) {
  304. try {
  305. $record = new stdClass;
  306. $record->details = $details;
  307. $record->title = $title;
  308. $record->submitter_id = $submitter_id;
  309. $record->actions = serialize($actions);
  310. $record->enabled = 1;
  311. $record->type = $type;
  312. $success = drupal_write_record('tripal_admin_notfications', $record);
  313. }
  314. catch (Exception $e) {
  315. $transaction->rollback();
  316. watchdog('tripal_cron', 'Could not write notification to database.');
  317. }
  318. }
  319. }
  320. /**
  321. * Creates a new Tripal Entity type (i.e. bundle).
  322. *
  323. * @param $args
  324. * An array of arguments that must include the following keys:
  325. * - vocabulary: The abbreviated vocabulary for the vocabulary
  326. * (e.g. RO, SO, PATO).
  327. * - accession: The unique term ID in the vocabulary $vocabulary
  328. * (i.e. an accession).
  329. * - term_name: A human-readable name for this term. This will became
  330. * the name that appears for the content type. In practice, this
  331. * should be the name of the term. (E.g. the name for SO:0000704 is gene).
  332. * @param $error
  333. * A string, passed by reference, that is filled with the error message
  334. * if the function fails.
  335. *
  336. * @return
  337. * The bundle object or FALSE if failure.
  338. *
  339. * @ingroup tripal_entities_api
  340. */
  341. function tripal_create_bundle($args, &$error = '') {
  342. $vocabulary = $args['vocabulary'];
  343. $accession = $args['accession'];
  344. $term_name = $args['term_name'];
  345. $storage_args = $args['storage_args'];
  346. $transaction = db_transaction();
  347. try {
  348. // First create the TripalVocab if it doesn't already exist.
  349. $vocab = tripal_load_vocab_entity(array('vocabulary' => $vocabulary));
  350. if (!$vocab) {
  351. $vocab = entity_get_controller('TripalVocab')->create(array('vocabulary' => $vocabulary));
  352. $vocab->save();
  353. }
  354. // Next create the TripalTerm if it doesn't already exist.
  355. $term = tripal_load_term_entity(array(
  356. 'vocabulary' => $vocabulary,
  357. 'accession' => $accession
  358. ));
  359. if (!$term) {
  360. $targs = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
  361. $term = entity_get_controller('TripalTerm')->create($targs);
  362. $term = $term->save();
  363. }
  364. // If the bundle doesn't already exist, then add it.
  365. $bundle_name = 'bio_data_' . $term->id;
  366. $einfo = entity_get_info('TripalEntity');
  367. if (!in_array($bundle_name, array_keys($einfo['bundles']))) {
  368. // Make the label for the content type have capitalized words. The
  369. // exception is 'mRNA' which we know should not be uppercased.
  370. $label = ucwords(preg_replace('/_/', ' ', $term_name));
  371. if ($term_name == 'mRNA') {
  372. $label = $term_name;
  373. }
  374. // Insert the bundle.
  375. db_insert('tripal_bundle')
  376. ->fields(array(
  377. 'label' => $label,
  378. 'type' => 'TripalEntity',
  379. 'name' => $bundle_name,
  380. 'term_id' => $term->id,
  381. ))
  382. ->execute();
  383. }
  384. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  385. $modules = module_implements('bundle_create');
  386. foreach ($modules as $module) {
  387. $function = $module . '_bundle_create';
  388. $function($bundle, $storage_args);
  389. }
  390. // Clear the entity cache so that Drupal will read our
  391. // hook_entity_info() implementation.
  392. global $language;
  393. $langcode = $language->language;
  394. cache_clear_all("entity_info:$langcode", 'cache');
  395. variable_set('menu_rebuild_needed', TRUE);
  396. // Get the bundle object.
  397. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  398. tripal_create_bundle_fields($bundle, $term);
  399. $modules = module_implements('bundle_postcreate');
  400. foreach ($modules as $module) {
  401. $function = $module . '_bundle_postcreate';
  402. $function($bundle);
  403. }
  404. }
  405. catch (Exception $e) {
  406. $transaction->rollback();
  407. drupal_set_message(t("Failed to create content type: %message.",
  408. array('%message' => $e->getMessage())), 'error');
  409. return FALSE;
  410. }
  411. return $bundle;
  412. }
  413. /*
  414. * Checks access permissions for a given entity.
  415. *
  416. * This function is set for TripalEntity access checking in the
  417. * tripal_entity_info() under the 'access callback' element.
  418. *
  419. *
  420. * @param $entity
  421. * The entity to check access for.
  422. function tripal_entity_permissions($entity) {
  423. if ($entity) {
  424. $bundle_name = $entity->bundle;
  425. }
  426. else {
  427. return FALSE;
  428. }
  429. // Get the bundle object.
  430. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  431. // Identify the administrative user roles.
  432. $admin_role = user_role_load_by_name('administrator');
  433. $roles = array($admin_role->rid => $admin_role->name);
  434. // Define the permissions.
  435. $permission_for_role = array(
  436. 'create ' . $bundle->label => TRUE,
  437. 'view ' . $bundle->label => TRUE,
  438. 'edit ' . $bundle->label => TRUE,
  439. 'delete ' . $bundle->label => TRUE,
  440. );
  441. // Assign the permissions
  442. foreach($roles as $role => $value){
  443. user_role_grant_permissions($role, $permission_for_role);
  444. watchdog('debug', '<pre>tripal_entity_permissions $role: '. print_r($role, TRUE) .'</pre>');
  445. }
  446. return TRUE;
  447. }
  448. */
  449. /**
  450. * Retrieves a list of the content types.
  451. *
  452. * @return
  453. * An array of bundles. Each bundle is an object containing information
  454. * about that bundle.
  455. *
  456. * @ingroup tripal_entities_api
  457. */
  458. function tripal_get_content_types() {
  459. return db_select('tripal_bundle', 'tb')
  460. ->fields('tb')
  461. ->execute()
  462. ->fetchAll();
  463. }
  464. /**
  465. * Refreshes the bundle such that new fields added by modules will be found during cron.
  466. *
  467. * @param $bundle_name
  468. * The name of the bundle to refresh (e.g. bio_data_4).
  469. *
  470. * @ingroup tripal_entities_api
  471. */
  472. function tripal_tripal_cron_notification() {
  473. $num_created = 0;
  474. // Get all bundle names to cycle through.
  475. $all_bundles = db_select('tripal_bundle', 'tb')
  476. ->fields('tb', array('name'))
  477. ->execute()->fetchAll();
  478. foreach ($all_bundles as $bundle_name){
  479. // Get the bundle object.
  480. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name->name));
  481. if (!$bundle) {
  482. tripal_report_error('tripal', TRIPAL_ERROR, "Unrecognized bundle name '%bundle'.",
  483. array('%bundle' => $bundle_name));
  484. return FALSE;
  485. }
  486. // Allow modules to add fields to the new bundle.
  487. $modules = module_implements('bundle_fields_info');
  488. foreach ($modules as $module) {
  489. $function = $module . '_bundle_fields_info';
  490. $info = $function('TripalEntity', $bundle);
  491. foreach ($info as $field_name => $details) {
  492. // If the field already exists then skip it.
  493. $field = field_info_field($details['field_name']);
  494. if ($field) {
  495. continue;
  496. }
  497. // Create notification that new fields exist.
  498. $detail_info = ' Tripal has detected a new field ' . $details['field_name'] .' for ' . $bundle->label. ' content type is available for import.';
  499. $title = 'New field available for import';
  500. $actions['Import'] = 'admin/import/field/' . $details['field_name'] . '/' . $bundle_name->name . '/' . $module . '/field';
  501. $type = 'Field';
  502. $submitter_id = $details['field_name'] . '-' . $bundle_name->name . '-' . $module;
  503. tripal_add_notification($title, $detail_info, $type, $actions, $submitter_id);
  504. $num_created++;
  505. }
  506. }
  507. // Allow modules to add instances to the new bundle.
  508. $modules = module_implements('bundle_instances_info');
  509. foreach ($modules as $module) {
  510. $function = $module . '_bundle_instances_info';
  511. $info = $function('TripalEntity', $bundle);
  512. foreach ($info as $field_name => $details) {
  513. // If the field is already attached to this bundle then skip it.
  514. $field = field_info_field($details['field_name']);
  515. if ($field and array_key_exists('bundles', $field) and
  516. array_key_exists('TripalEntity', $field['bundles']) and
  517. in_array($bundle->name, $field['bundles']['TripalEntity'])) {
  518. continue;
  519. }
  520. // Create notification that new fields exist.
  521. $detail_info = ' Tripal has detected a new field ' . $details['field_name'] .' for ' . $bundle->label. ' content type is available for import.';
  522. $title = 'New field available for import';
  523. $actions['Import'] = 'admin/import/field/' . $details['field_name'] . '/' . $bundle->name . '/' . $module . '/instance';
  524. $type = 'Field';
  525. $submitter_id = $details['field_name'] . '-' . $bundle_name->name . '-' . $module;
  526. tripal_add_notification($title, $detail_info, $type, $actions, $submitter_id);
  527. $num_created++;
  528. }
  529. }
  530. }
  531. }
  532. /**
  533. * Retrieves information about a given content type.
  534. *
  535. * @param $bundle_name
  536. * The name of a bundle.
  537. *
  538. * @return
  539. * An object containing information about the bundle.
  540. *
  541. * @ingroup tripal_entities_api
  542. */
  543. function tripal_get_content_type($bundle_name) {
  544. return db_select('tripal_bundle', 'tb')
  545. ->fields('tb')
  546. ->condition('tb.name', $bundle_name)
  547. ->execute()
  548. ->fetchAll();
  549. }
  550. /**
  551. * Refreshes the bundle such that new fields added by modules will be found.
  552. *
  553. * @param $bundle_name
  554. * The name of the bundle to refresh (e.g. bio_data_4).
  555. *
  556. * @return
  557. * The array of field instance names that were added.
  558. *
  559. * @ingroup tripal_entities_api
  560. */
  561. function tripal_create_bundle_fields($bundle, $term) {
  562. $added = array();
  563. // Allow modules to add fields to the new bundle.
  564. $modules = module_implements('bundle_fields_info');
  565. $info = array();
  566. foreach ($modules as $module) {
  567. $function = $module . '_bundle_fields_info';
  568. $temp = $function('TripalEntity', $bundle);
  569. $info = array_merge($info, $temp);
  570. }
  571. // Allow modules to alter which fields should be attached to content
  572. // types they create.
  573. drupal_alter('bundle_fields_info', $info, $bundle, $term);
  574. // Iterate through all of the fields and create them.
  575. foreach ($info as $field_name => $details) {
  576. $field_type = $details['type'];
  577. // TODO: make sure the field term exits. If not then
  578. // skip it.
  579. // If the field already exists then skip it.
  580. $field = field_info_field($details['field_name']);
  581. if ($field) {
  582. continue;
  583. }
  584. // Create the field.
  585. $field = field_create_field($details);
  586. if (!$field) {
  587. tripal_set_message(t("Could not create new field: %field.",
  588. array('%field' => $details['field_name'])), TRIPAL_ERROR);
  589. }
  590. }
  591. // Allow modules to add instances to the new bundle.
  592. $modules = module_implements('bundle_instances_info');
  593. $info = array();
  594. foreach ($modules as $module) {
  595. $function = $module . '_bundle_instances_info';
  596. $temp = $function('TripalEntity', $bundle);
  597. $info = array_merge($info, $temp);
  598. }
  599. // Allow modules to alter which fields should be attached to content
  600. // types they create.
  601. drupal_alter('bundle_instances_info', $info, $bundle, $term);
  602. // Iterate through all of the field instances and create them.
  603. foreach ($info as $field_name => $details) {
  604. // If the field is already attached to this bundle then skip it.
  605. $field = field_info_field($details['field_name']);
  606. if ($field and array_key_exists('bundles', $field) and
  607. array_key_exists('TripalEntity', $field['bundles']) and
  608. in_array($bundle->name, $field['bundles']['TripalEntity'])) {
  609. continue;
  610. }
  611. // Create the field instance.
  612. $instance = field_create_instance($details);
  613. $added[] = $field_name;
  614. }
  615. return $added;
  616. }
  617. /**
  618. * Updates an existing field and its attached instance to a bundle.
  619. *
  620. *
  621. * @param $field_name
  622. * The name of the field.
  623. * @param $field_info
  624. * An associative array containing the field information. The following
  625. * key/value pairs are supported:
  626. * 'field_type' : a valid field type. May be any of the Drupal default
  627. * fields, one created by the tripal_chado module or another custom module.
  628. * 'widget_type' : a valid widget type. May be any of the Drupal default
  629. * fields, one created by the tripal_chado module or another custom module.
  630. * 'field_settings' : an array of settings that are appropriate for the
  631. * selected field type.
  632. * 'widget_settings' : an array of settings that are appropriate for the
  633. * selected widget type.
  634. * 'description' : a default description for this field.
  635. * 'label' : a label used as a header for this field.
  636. * 'is_required' : indicates if the field is required in the edit form.
  637. * 'cardinality' : indicates the number of values this field can support.
  638. * the default is 1 (meaning only one value). Use a value of
  639. * FIELD_CARDINALITY_UNLIMITED for unlimited number of values.
  640. * 'default_value' : A default value for the field.
  641. * 'format' : A string indicating the format for the field. Must be
  642. * specific to the field.
  643. * @param $entity_type_name
  644. * The entity type name.
  645. * @param $bundle_name
  646. * The bundle name.
  647. *
  648. * @return
  649. * FALSE if the field could not be updated
  650. *
  651. * TODO: this function really shouldn't try to create an instance and
  652. * attach to a bundle at the same time.
  653. *
  654. * @ingroup tripal_entities_api
  655. */
  656. function tripal_update_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name) {
  657. $field = field_info_field($field_name);
  658. // If the field doesn't exists or is not attached to this bundle then
  659. // just return, there is nothing left to do.
  660. if (!$field or !array_key_exists('bundles', $field) or
  661. !array_key_exists($entity_type_name, $field['bundles']) or
  662. !in_array($bundle_name, $field['bundles'][$entity_type_name])) {
  663. return FALSE;
  664. }
  665. $field['field_name'] = $field_name;
  666. if (array_key_exists('field_type', $field_info)) {
  667. $field['cardinality'] = $field_info['cardinality'];
  668. }
  669. if (array_key_exists('locked', $field_info)) {
  670. $field['locked'] = $field_info['locked'];
  671. }
  672. if (array_key_exists('storage', $field_info)) {
  673. $field['storage']['type'] = $field_info['storage'];
  674. }
  675. if (array_key_exists('field_settings', $field_info)) {
  676. $field['settings'] = $field_info['field_settings'];
  677. }
  678. field_update_field($field);
  679. $field_instance['field_name'] = $field_name;
  680. $field_instance['entity_type'] = $entity_type_name;
  681. $field_instance['bundle'] = $bundle_name;
  682. if (array_key_exists('label', $field_info)) {
  683. $field['label'] = $field_info['label'];
  684. }
  685. if (array_key_exists('description', $field_info)) {
  686. $field['description'] = $field_info['description'];
  687. }
  688. if (array_key_exists('widget', $field_info)) {
  689. if (array_key_exists('widget_type', $field_info['widget'])) {
  690. $field['widget']['type'] = $field_info['widget_type'];
  691. }
  692. if (array_key_exists('widget_settings', $field_info['widget'])) {
  693. $field['widget']['settings'] = $field_info['widget_settings'];
  694. }
  695. }
  696. if (array_key_exists('required', $field_info)) {
  697. $field['required'] = $field_info['is_required'];
  698. }
  699. if (array_key_exists('settings', $field_info)) {
  700. $field['settings'] = $field_info['field_settings'];
  701. }
  702. if (array_key_exists('default_value', $field_info)) {
  703. $field['default_value'] = $field_info['default_value'];
  704. }
  705. if (array_key_exists('format', $field_info)) {
  706. $field['format'] = $field_info['format'];
  707. }
  708. field_update_instance($field_instance);
  709. }
  710. /**
  711. * @section
  712. * Bundle Variables.
  713. */
  714. /**
  715. * Fetch the value for a given tripal variable.
  716. *
  717. * @param string $variable_name
  718. * The name of the variable as in tripal_variables.name.
  719. * @param int $bundle_id
  720. * The unique identfier for the bundle you want the value for.
  721. * @return text
  722. * The value of the specified variable for the specified bundle.
  723. *
  724. * @ingroup tripal_entities_api
  725. */
  726. function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
  727. $variable = tripal_get_variable($variable_name);
  728. // Warn if we can't find the tripal_variable.
  729. if (!$variable) {
  730. return $default;
  731. }
  732. // Select the value for this variable.
  733. $value = db_select('tripal_bundle_variables', 'var')
  734. ->fields('var', array('value'))
  735. ->condition('var.bundle_id', $bundle_id)
  736. ->condition('var.variable_id', $variable->variable_id)
  737. ->execute()
  738. ->fetchField();
  739. // Warn if the value appears to be empty.
  740. if (!$value) {
  741. return $default;
  742. }
  743. return $value;
  744. }
  745. /**
  746. * Save the value of a tripal variable for a given bundle.
  747. *
  748. * @param string $variable_name
  749. * The name of the variable as in tripal_variables.name.
  750. * @param int $bundle_id
  751. * The unique identfier for the bundle you want the value for.
  752. * @param $text $value
  753. * The value of the variable for the given bundle.
  754. *
  755. * @ingroup tripal_entities_api
  756. */
  757. function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
  758. $variable = tripal_get_variable($variable_name);
  759. if (!$variable) {
  760. if($variable_name === 'hide_empty_field'){
  761. tripal_insert_variable(
  762. 'hide_empty_field',
  763. 'Structure->Tripal Content Type->edit checkbox to hide empty fields for that bundle.'
  764. );
  765. $variable = tripal_get_variable($variable_name);
  766. if (!$variable) {
  767. return FALSE;
  768. }
  769. }
  770. else {
  771. return FALSE;
  772. }
  773. }
  774. // And then we need to write the new format to the tripal_bundle_variables table.
  775. $record = array(
  776. 'bundle_id' => $bundle_id,
  777. 'variable_id' => $variable->variable_id,
  778. 'value' => $value,
  779. );
  780. // Check whether there is already a format saved.
  781. $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
  782. ->fields('var', array('bundle_variable_id'))
  783. ->condition('var.bundle_id', $record['bundle_id'])
  784. ->condition('var.variable_id', $record['variable_id'])
  785. ->execute()
  786. ->fetchField();
  787. if ($bundle_variable_id) {
  788. $record['bundle_variable_id'] = $bundle_variable_id;
  789. return drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
  790. }
  791. else {
  792. return drupal_write_record('tripal_bundle_variables', $record);
  793. }
  794. }
  795. /**
  796. * @section
  797. * Title & URL Formats.
  798. */
  799. /**
  800. * Get Page Title Format for a given Tripal Entity Type.
  801. *
  802. * @param TripalBundle $bundle
  803. * The Entity object for the Tripal Bundle the title format is for.
  804. *
  805. * @ingroup tripal_entities_api
  806. */
  807. function tripal_get_title_format($bundle) {
  808. // Get the existing title format if it exists.
  809. $title_format = tripal_get_bundle_variable('title_format', $bundle->id);
  810. // If there isn't yet a title format for this bundle/type then we should
  811. // determine the default.
  812. if (!$title_format) {
  813. $title_format = tripal_get_default_title_format($bundle);
  814. tripal_save_title_format($bundle, $title_format);
  815. }
  816. return $title_format;
  817. }
  818. /**
  819. * Save Page Title Format for a given Tripal Entity Type.
  820. *
  821. * @param TripalBundle $entity
  822. * The Entity object for the Tripal Bundle the title format is for.
  823. * @param string $format
  824. * The pattern to be used when generating entity titles for the above type.
  825. *
  826. * @ingroup tripal_entities_api
  827. */
  828. function tripal_save_title_format($entity, $format) {
  829. return tripal_set_bundle_variable('title_format', $entity->id, $format);
  830. }
  831. /**
  832. * Determine the default title format to use for an entity.
  833. *
  834. * @param TripalBundle $bundle
  835. * The Entity object for the Tripal Bundle that the title format is for.
  836. *
  837. * @return string
  838. * A default title format.
  839. *
  840. * @ingroup tripal_entities_api
  841. */
  842. function tripal_get_default_title_format($bundle) {
  843. $format = '';
  844. // Retrieve all available tokens.
  845. $tokens = tripal_get_entity_tokens($bundle);
  846. // A) Check to see if more informed modules have suggested a title for this
  847. // type. Invoke hook_tripal_default_title_format() to get all suggestions
  848. // from other modules.
  849. $suggestions = module_invoke_all('tripal_default_title_format', $bundle, $tokens);
  850. if ($suggestions) {
  851. // Use the suggestion with the lightest weight.
  852. $lightest_key = NULL;
  853. foreach ($suggestions as $k => $s) {
  854. if ($lightest_key === NULL) $lightest_key = $k;
  855. if ($s['weight'] < $lightest_key) $lightest_key = $k;
  856. }
  857. $format = $suggestions[$lightest_key]['format'];
  858. return $format;
  859. }
  860. // B) Generate our own ugly title by simply comma-separating all the
  861. // required fields.
  862. if (!$format) {
  863. $tmp = array();
  864. // Check which tokens are required fields and join them into a default format.
  865. foreach($tokens as $token) {
  866. if ($token['required']) {
  867. $tmp[] = $token['token'];
  868. }
  869. }
  870. $format = implode(', ', $tmp);
  871. return $format;
  872. }
  873. return $format;
  874. }
  875. /**
  876. * Returns an array of tokens based on Tripal Entity Fields.
  877. *
  878. * @param TripalBundle $entity
  879. * The bundle entity for which you want tokens.
  880. * @return
  881. * An array of tokens where the key is the machine_name of the token.
  882. *
  883. * @ingroup tripal_entities_api
  884. */
  885. function tripal_get_entity_tokens($entity, $options = array()) {
  886. $tokens = array();
  887. // Set default options.
  888. $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
  889. $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
  890. if ($options['include id']) {
  891. $token = '[TripalBundle__bundle_id]';
  892. $tokens[$token] = array(
  893. 'label' => 'Bundle ID',
  894. 'description' => 'The unique identifier for this Tripal Content Type.',
  895. 'token' => $token,
  896. 'field_name' => NULL,
  897. 'required' => TRUE
  898. );
  899. $token = '[TripalEntity__entity_id]';
  900. $tokens[$token] = array(
  901. 'label' => 'Content/Entity ID',
  902. 'description' => 'The unique identifier for an individual piece of Tripal Content.',
  903. 'token' => $token,
  904. 'field_name' => NULL,
  905. 'required' => TRUE
  906. );
  907. }
  908. $fields = field_info_instances('TripalEntity', $entity->name);
  909. foreach ($fields as $f) {
  910. // Build the token from the field information.
  911. $token = '[' . $f['field_name'] . ']';
  912. $current_token = array(
  913. 'label' => $f['label'],
  914. 'description' => $f['description'],
  915. 'token' => $token,
  916. 'field_name' => $f['field_name'],
  917. 'required' => $f['required']
  918. );
  919. // If the required only option is set then we only want to add
  920. // required fields to the token list.
  921. if ($options['required only'] AND $current_token['required']) {
  922. $tokens[$token] = $current_token;
  923. }
  924. // If the required only option is not set then add everything.
  925. elseif (!$options['required only']) {
  926. $tokens[$token] = $current_token;
  927. }
  928. }
  929. return $tokens;
  930. }
  931. /**
  932. * Replace all Tripal Tokens in a given string.
  933. *
  934. * NOTE: If there is no value for a token then the token is removed.
  935. *
  936. * @param string $string
  937. * The string containing tokens.
  938. * @param TripalEntity $entity
  939. * The entity with field values used to find values of tokens.
  940. * @param TripalBundle $bundle_entity
  941. * The bundle enitity containing special values sometimes needed for token replacement.
  942. *
  943. * @return
  944. * The string will all tokens replaced with values.
  945. *
  946. * @ingroup tripal_entities_api
  947. */
  948. function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL) {
  949. // Determine which tokens were used in the format string
  950. $used_tokens = array();
  951. if (preg_match_all('/\[\w+\]/', $string, $matches)) {
  952. $used_tokens = $matches[0];
  953. }
  954. // If there are no tokens then just return the string.
  955. if (count($used_tokens) == 0) {
  956. return $string;
  957. }
  958. // If the field are not loaded for the entity then we want to load them
  959. // but we won't do a field_attach_load() as that will load all of the
  960. // fields. For syncing (publishing) of content loading all fields for
  961. // all synced entities causes extreme slowness, so we'll only attach
  962. // the necessary fields for replacing tokens.
  963. $attach_fields = array();
  964. foreach($used_tokens as $token) {
  965. $field_name = str_replace(array('.','[',']'), array('__','',''), $token);
  966. if (!property_exists($entity, $field_name)) {
  967. $field = field_info_field($field_name);
  968. $storage = $field['storage'];
  969. $attach_fields[$storage['type']]['storage'] = $storage;
  970. $attach_fields[$storage['type']]['fields'][] = $field;
  971. }
  972. }
  973. // If we have any fields that need attaching, then do so now.
  974. if (count(array_keys($attach_fields)) > 0) {
  975. foreach ($attach_fields as $storage_type => $details) {
  976. $storage = $details['storage'];
  977. $fields = $details['fields'];
  978. $field_ids = array();
  979. foreach ($fields as $field) {
  980. $field_ids[$field['id']] = array($entity->id);
  981. }
  982. $entities = array($entity->id => $entity);
  983. }
  984. module_invoke($storage['module'], 'field_storage_load', 'TripalEntity',
  985. $entities, FIELD_LOAD_CURRENT, $field_ids, array());
  986. }
  987. // Now that all necessary fields are attached process the tokens.
  988. foreach($used_tokens as $token) {
  989. $field_name = str_replace(array('.','[',']'), array('__','',''), $token);
  990. $value = '';
  991. if (property_exists($entity, $field_name)) {
  992. // Note: there is a memory leak in field_get_items() so we can't use it
  993. // here or bulk publishing will slowly erode memory.
  994. //$field_value = field_get_items('TripalEntity', $entity, $field_name);
  995. if (array_key_exists(0, $entity->{$field_name}['und'])) {
  996. $value = $entity->{$field_name}['und'][0]['value'];
  997. }
  998. // TODO: deal with the value when it is not a scalar.
  999. }
  1000. // The TripalBundle__bundle_id is a special token for substituting the
  1001. // bundle id.
  1002. elseif ($field_name === 'TripalBundle__bundle_id') {
  1003. // Load the bundle entity if we weren't given it.
  1004. if (!$bundle_entity) {
  1005. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  1006. }
  1007. // This token should be the id of the TripalBundle.
  1008. $value = $bundle_entity->id;
  1009. }
  1010. // The TripalBundle__bundle_id is a special token for substituting the
  1011. // entty id.
  1012. elseif ($field_name === 'TripalEntity__entity_id') {
  1013. // This token should be the id of the TripalEntity.
  1014. $value = $entity->id;
  1015. }
  1016. // We can't support tokens that have multiple elements (i.e. in an array).
  1017. if (is_array($value)) {
  1018. $string = str_replace($token, '', $string);
  1019. }
  1020. else {
  1021. $string = str_replace($token, $value, $string);
  1022. }
  1023. }
  1024. return $string;
  1025. }
  1026. /**
  1027. * Formats the tokens for display.
  1028. *
  1029. * @param array $tokens
  1030. * A list of tokens generated via tripal_get_entity_tokens().
  1031. * @return
  1032. * Rendered output describing the available tokens.
  1033. *
  1034. * @ingroup tripal_entities_api
  1035. */
  1036. function theme_token_list($tokens) {
  1037. $header = array('Token', 'Name', 'Description');
  1038. $rows = array();
  1039. foreach ($tokens as $details) {
  1040. $rows[] = array(
  1041. $details['token'],
  1042. $details['label'],
  1043. $details['description'],
  1044. );
  1045. }
  1046. return theme('table', array('header' => $header, 'rows' => $rows));
  1047. }
  1048. /**
  1049. * Define the entity label callback. This will return the title.
  1050. *
  1051. * @param $entity
  1052. *
  1053. * @return mixed
  1054. *
  1055. * @ingroup tripal_entities_api
  1056. */
  1057. function tripal_entity_label($entity) {
  1058. if (property_exists($entity, 'title')) {
  1059. return $entity->title;
  1060. }
  1061. return NULL;
  1062. }
  1063. /**
  1064. *
  1065. * @param $bundle_name
  1066. *
  1067. * @ingroup tripal_entities_api
  1068. */
  1069. function tripal_get_bundle_details($bundle_name) {
  1070. global $user;
  1071. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  1072. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  1073. $vocab = $term->vocab;
  1074. $instances = field_info_instances('TripalEntity', $bundle->name);
  1075. $details = array(
  1076. 'name' => $bundle->name,
  1077. 'label' => $bundle->label,
  1078. 'term' => array(
  1079. 'accession' => $vocab->vocabulary . ':' . $term->accession,
  1080. 'name' => $term->name,
  1081. 'definition' => $term->definition,
  1082. 'url' => $term->url
  1083. ),
  1084. 'fields' => array(),
  1085. );
  1086. // Iterate through each feild and provide a discription of it and
  1087. // it's sub elements.
  1088. foreach ($instances as $instance) {
  1089. // Skip deleted fields.
  1090. if ($instance['deleted']) {
  1091. continue;
  1092. }
  1093. $field_name = $instance['field_name'];
  1094. $field = field_info_field($field_name);
  1095. $field_class = $field['type'];
  1096. $term_vocab = $instance['settings']['term_vocabulary'];
  1097. $term_accession = $instance['settings']['term_accession'];
  1098. $field_term = tripal_get_term_details($term_vocab, $term_accession);
  1099. $field_details = array(
  1100. 'name' => $field_name,
  1101. 'label' => $instance['label'],
  1102. 'term' => array(
  1103. 'accession' => $term_vocab . ":" . $term_accession,
  1104. 'name' => $field_term['name'],
  1105. 'definition' => $field_term['definition'],
  1106. 'url' => $field_term['url'],
  1107. ),
  1108. // These items can be overridden by the element_info array that
  1109. // is present in a TripalField instance. Here we set defaults.
  1110. 'required' => $instance['required'] ? TRUE : FALSE,
  1111. 'type' => 'xs:string',
  1112. 'readonly' => TRUE,
  1113. // The cardinatlity value always comes from the field.
  1114. 'cardinality' => $field['cardinality'],
  1115. );
  1116. if (tripal_load_include_field_class($field_class)) {
  1117. $field_obj = new $field_class($field, $instance);
  1118. $element_info = $field_obj->elementInfo();
  1119. $element_info = $element_info[$term_vocab . ':' . $term_accession];
  1120. // If the element info for this field sets required, type and readonly
  1121. // attributes then set those.
  1122. $field_details['required'] = array_key_exists('required', $element_info) ? $element_info['required'] : FALSE;
  1123. $field_details['type'] = array_key_exists('type', $element_info) ? $element_info['type'] : 'xs:string';
  1124. $field_details['readonly'] = array_key_exists('readonly', $element_info) ? $element_info['readonly'] : TRUE;
  1125. $field_details['label'] = array_key_exists('label', $element_info) ? $element_info['label'] : $field_details['label'];
  1126. $field_details['help'] = array_key_exists('help', $element_info) ? $element_info['help'] : '';
  1127. // If this field is an 'xs:complexType' then it will have sub elements.
  1128. // we need to add those as well.
  1129. if (array_key_exists('elements', $element_info) and is_array($element_info['elements'])) {
  1130. _tripal_get_bundle_field_element_details($element_info['elements'], $field_details);
  1131. }
  1132. $details['fields'][] = $field_details;
  1133. }
  1134. }
  1135. return $details;
  1136. }
  1137. /**
  1138. * A recursive helper function for the tripal_get_bundle_details.
  1139. *
  1140. * @param $elementInfo
  1141. *
  1142. * @ingroup tripal_entities_api
  1143. */
  1144. function _tripal_get_bundle_field_element_details($elements, &$field_details) {
  1145. $field_details['elements'] = array();
  1146. foreach ($elements as $element_key => $element_info) {
  1147. // Handle the entity element differnetly.
  1148. if ($element_key == 'entity') {
  1149. continue;
  1150. }
  1151. list($term_vocab, $term_accession) = explode(':', $element_key);
  1152. $term = tripal_get_term_details($term_vocab, $term_accession);
  1153. $element_details = array(
  1154. 'name' => $element_info['name'],
  1155. 'label' => array_key_exists('label', $element_info) ? $element_info['label'] : ucfirst(preg_replace('/_/', ' ', $term['name'])),
  1156. 'help' => array_key_exists('help', $element_info) ? $element_info['help'] : '',
  1157. 'term' => array(
  1158. 'accession' => $term_vocab . ':' . $term_accession,
  1159. 'name' => $term['name'],
  1160. 'definition' => $term['definition'],
  1161. 'url' => $term['url'],
  1162. ),
  1163. 'required' => array_key_exists('required', $element_info) ? $element_info['required'] : FALSE,
  1164. 'type' => array_key_exists('type', $element_info) ? $element_info['type'] : 'xs:string',
  1165. 'readonly' => array_key_exists('readonly', $element_info) ? $element_info['readonly'] : TRUE,
  1166. );
  1167. if (array_key_exists('elements', $element_info) and is_array($element_info['elements'])) {
  1168. _tripal_get_bundle_field_element_details($element_info['elements'], $element_details);
  1169. }
  1170. $field_details['elements'][] = $element_details;
  1171. }
  1172. }
  1173. /**
  1174. *
  1175. * @param unknown $bundle_name
  1176. * @param unknown $values
  1177. *
  1178. * @throws Exception
  1179. *
  1180. * @ingroup tripal_entities_api
  1181. */
  1182. function tripal_insert_entity($bundle_name, $values){
  1183. global $user;
  1184. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  1185. // Get the fields associated with this content type.
  1186. $instances = field_info_instances('TripalEntity', $bundle->name);
  1187. foreach ($instances as $instance) {
  1188. $field_name = $instance['field_name'];
  1189. $field = field_info_field($field_name);
  1190. $field_type = $field['type'];
  1191. $field_settings = $field['settings'];
  1192. $instance_settings = $instance['settings'];
  1193. $field_name = $field['field_name'];
  1194. $vocabulary = $instance['settings']['term_vocabulary'];
  1195. $accession = $instance['settings']['term_accession'];
  1196. $field_accession = $vocabulary . ':' . $accession;
  1197. $field_term = tripal_get_term_details($vocabulary, $accession);
  1198. $field_key = $field_term['name'];
  1199. $field_key = strtolower(preg_replace('/ /', '_', $key));
  1200. // There are three ways that a field value can be specified. Those
  1201. // are as the controlled vocabulary accession (e.g. GO:0000134), sa
  1202. // the field name or as the field key which is the term name with
  1203. // spaces replaced with underscores.
  1204. // First make sure that required fields are present.
  1205. if ($instance['required'] == TRUE) {
  1206. if (!array_key_exists($field_key, $values) and
  1207. !array_key_exists($field_accession, $values) and
  1208. !array_key_exists($field_name, $values)) {
  1209. throw new Exception(t('Cannot insert the record. Missing the required field "%missing".',
  1210. array('%missing' => $field_name)));
  1211. }
  1212. }
  1213. }
  1214. // Make sure that all required fields are presnet
  1215. // TODO: make sure the user has permission to do this.
  1216. $ec = entity_get_controller('TripalEntity');
  1217. $entity = $ec->create(array(
  1218. 'bundle' => $bundle_name,
  1219. 'term_id' => $bundle->term_id,
  1220. ));
  1221. $entity = $entity->save();
  1222. }
  1223. /**
  1224. * @param $bundle_name
  1225. * @param $values
  1226. *
  1227. * @ingroup tripal_entities_api
  1228. */
  1229. function tripal_update_entity($bundle_name, $values) {
  1230. }