How to iterate over list of terms within a taxonomy - orchardcms

Provided a taxonomy name or ID, how do I iterate over a list of terms within the said taxonomy?
E.g. I want to do something like this:
var taxonomyId = 15;
foreach(dynamic item in get_terms(taxonomyId)) {
#Display(item.text)
}

You need to inject ITaxonomyService in your controller/driver, and ITaxonomyService has a method which returns the list of terms for a taxonomyId.
IEnumerable<TermPart> GetTerms(int taxonomyId);
you can pass these terms to the view using model/viewmodel.
you can look into ITaxonomyService implementation in taxonomy module for further details.

Related

How to get source list types of particular list/record field?

Here is I have two entity custom fields with list/record type,
custom_dev_j15 entity field has a custom source list (eg: one, two, three, four, etc)
custom_qa_v93 entity field has a standard source list as an object (eg: customer )
I've two vendor entity custom fields as stated in screenshots of question,
custentity473 --> customer is selected as list source
custentity474 --> custom_dev_j15_m_list as selected as list source ( which is custom list)
Here is snippet that i used to get the options of these fields,
// Snippet
var fieldDetails = {};
var record = nlapiCreateRecord("Vendor");
var field = record.getField("custentity473");
var selectoptions = field.getSelectOptions();
for ( var i in selectOptions) {
var Option = {
id : selectOptions[i].getId(),
label : selectOptions[i].getText()
}
Options.push(Option);
}
fieldDetail["options"] = Options;
But my need is to get source list information like name of the list source (customer or custom_dev_j15_m_list) via suitescript
any idea on how to get this information?
Thanks in advance
I'm not sure I understand this question for what you're trying to do.
In NetSuite almost always, you accommodate to the source list types because you know that's the type, and if you need something else (e.g. a selection which is a combination/or custom selection you'll use a scripted field)
Perhaps you can expand on your use case, and then we can help you further?

How to add Count() method on my content type in orchard?

I have a content type named "News" with Title, Body, Autoroute and a TextField. I have 2 problems:
To Count value of content items by text field named Author (can't use taxonomy terms).
To Count total sum of content items by Date (from 2017-01-01 to 2017-12-31)
Now, I want to add a Count() method on my content type, so I can filter, sort and use Projection.
How can I do this?
Or do you have a better method for it?
Thanks!
I'd use Orchards ISearchService for this. Just add your ContentType to a search index (you can have multiple indices) and check the fields you want to include, like created and author.
Then you can search like this and use the TotalItemCount property of the ISearchService:
var pager = new Pager(this.OrchardServices.WorkContext.CurrentSite, page, pageSize);
var searchSettingsPart = this.OrchardServices.WorkContext.CurrentSite.As<SearchSettingsPart>();
// Orchard.Search.Services.ISearchService
var searchHits = this.searchService.Search.Query(
searchText,
pager.Page,
pager.PageSize,
searchSettingsPart.FilterCulture,
searchSettingsPart.SearchIndex,
searchSettingsPart.GetSearchFields(searchSettingsPart.SearchIndex),
searchHit => searchHit);
var count = searchHits.TotalItemCount;
As far as the description of your ContentType goes, I think Orchard already provides all the necessary filters for a projection.
If you need something special you'd need to implement your own FilterProvider. The ContentTypesFilter is a good example on how to do this.

Orchard - how to access taxonomy field of a custom content type programmatically

I have a custom content type called Store, which has a Brands taxonomy field. A Store can have multiple Brands associated with it.
I have been tasked with building an import/export routine that allows the user to upload a CSV file containing new Stores and their associated Brands.
I can create the Stores other fields OK, but can't work out how to set the taxonomy field?
Can anyone tell me how I access the Taxonomy field for my custom content type?
Thanks in advance.
OK so (as Bertrand suggested), using the Import/Export feature might be a better way to go, but as a relative noob on Orchard I don't have the time to spend looking at it and couldn't find a good tutorial.
Below is an alternative approach, using the TaxonomyService to programatically assign Terms to a ContentItem.
First of all, inject the ContentManager and TaxonomyService into the constructor...
private ITaxonomyService _taxonomyService;
private IContentManager _contentManager;
public MyAdminController(IContentManager contentManager, ITaxonomyService taxonomyService)
{
_contentManager = contentManager;
_taxonomyService = taxonomyService;
}
Create your ContentItem & set the title
var item = _contentManager.New("MyContentType");
item.As<TitlePart>().Title = "My New Item";
_contentManager.Create(item);
Now we have a ContentItem to work with. Time to get your taxonomy & find your term(s)...
var taxonomy = _taxonomyService.GetTaxonomyByName("Taxonomy Name");
var termPart = _taxonomyService.GetTermByName(taxonomy.Id, "Term Name");
Add the terms to a List of type TermPart...
List<TermPart> terms = new List<TermPart>();
terms.Add(termPart);
Finally, call UpdateTerms, passing in the ContentItem, terms to assign and the name of the field on the ContentItem you want to update...
_taxonomyService.UpdateTerms(item, terms.AsEnumerable<TermPart>(), "My Field");
Hope this helps someone. Probably me next time round! : )

Split a string and search a domain for each individual word with Grails and Groovy

I'm creating a search function to browse through one of my domain classes.
I have a string which i would like to split into individual words, so that I can search through my domain class for each word.
For example, my User class has a name attribute and a biography attribute.
If I searched "Tom Chicago" in my system, it would return all the users with a name like %tom% or with a name like %chicago% AND all the users with a biography containing either of those words to.
I have started my search function like this:
def userCriteria = User.createCriteria()
userResults = userCriteria.list(){
like("name", "%${q}%")
like("biography", "%${q}%")
}
Obviously this is not doing quite what I'd like it to do. How can I adjust it so that it does?
While this is going to create some horrible SQL statements it will do what you want. Not sure I could in good faith use this in a production system when such things as Searchable exist.
def userCriteria = User.createCriteria()
def userResults = userCriteria.list() {
or {
q.split(" ").each { t ->
ilike("name", "%$t%")
ilike("biography", "%$t%")
}
}
}

Access detail element of a collection inside a section in openxava

How can I access the details element of a collection entity which is inside one section of another entity with openxava? For example, in the view of entity A, we have section {S1,S2,S3} and inside section S3 view, we have {collection of entity B}. Now I want to access the detail element of entity B, so that i can fill the element in an action controller. How do I do that?
Get the collection directly from the view, in this way:
Collection myCollection = getView().getSubview("myCollection").getCollectionObjects();
It must work even with oldest OpenXava versions
Obtain the entity associated to the view and get the collection from it. Since OpenXava 4.3 you can do it in this way:
MyEntity myEntity = (MyEntity) getView().getEntity();
Collection myCollection = myEntity.getMyCollection();
If you're using an OX previous to 4.3 do it in this way:
Map keyValues = getView().getKeyValuesWithValue();
if (!keyValues.isEmpty()) {
MyEntity myEntity = (MyEntity)
MapFacade.findEntity(getView().getModelName(), keyValues);
Collection myCollection = myEntity.getMyCollection();
}
You can do it in several ways. Here you have one, I have used it with some references that I want to modify from inside of an action called by the base module (which should work with your collection):
Query q = XPersistence.getManager().createQuery("JPQL QUERY TO RETRIVE THE COLLECTION WITH :parameterIfNeeded");
q.setParameter("parameterIfNeeded", "value");
List entityBList = q.getResultList();
if (getView().getModelName().equalsIgnoreCase("yourBaseModelViewName")) {
getView().getSubview("yourSubViewName").setModel(entityBList);
getView().getSubview("yourSubViewName").refresh();
}
You must to be using OX 4.6 to be able to use setModel(). And remember that the "yourSubViewName" is the name of the property for your collection into the base model.
I have not tested that code with a collection, so make the adjustments according to your needs, maybe you will need to CAST the query result list or something.

Resources