Using Views module (Drupal 7) to display php array - php

I have a php array (let's call it $people) that I specifically created from LinkedIn API. The fact is I don't want to record it in my own database due to the huge amount of data and I don't want to use a Cron to update results.
So, the problem is I want to display results from my php array in a filterable/sortable table. Notice: I can parameter this array to catch results from a START parameter with a COUNT parameter; this is mandatory for the pagination.
How to use Views module of Drupal 7 to do that? Any idea?
Thanks in advance.

I didn't get the exact question but assuming you want to display some data in a paginated table, I can give you some hints. You can try the theme_table() to render a paginated table. Here is rough sample code https://drupal.org/node/156863
Integration with views in a relatively complex task. Easiest way is to go with https://drupal.org/project/data. But if you want integrate more, then start with the hook_views_data()

Related

How do I add/save records in the same row to a database? - Symfony 3

Im working with Symfony 3.4 and I would like to let the user add/edit/delete records from index (where there are displayed all records), instead of clicking on add/edit/delete buttons and opening a new page. I have been researching about this, but I cant find anything useful. I know we have to use javascript to add rows to the table, but how can I use a form into the row to perform crud options, and also modify the database. Could you please give me a clue?
you have a few questions here ;-)
Recap:
You have a List of items (I'll use tasks in my examples)
You want to add/edit/delete items directly on your listing-page
You want to map the items to your database
There are multiple possible solutions, depending on what you use.
JS-Frontend
You can build your list in React/VueJS/Whatever.
When you add/remove/edit a task you trigger an XHR/AJAX-Request to your API.
You have to implement the API endpoints and use them.
e.g.
GET /api/tasks => return JSON wir all tasks
DELETE /api/tasks/7 => deletes Task with ID 7
POST /api/tasks=> created a new tasks
PUT /api/tasks/{id} => updates task with ID. Attach JSON-Body with name, etc.
Symfony Forms
You can also use symfony forms. You're looking for the Collection Field-Type.
The allow_add and allow_delete Options will be your friends.
You can then use the objects to store the objects to the database.
e.g. using DoctrineORM or whatever you want :-)
See Handling Form Submissions
But be careful. This is not usable for large lists since it will delete all elements from the database which are not on this page.
If you want to paginate your list, this is not what you're looking for.
My Opinion
You want to allow adding/deleting items. My guess is that you're building some Backend-Application (?).
I'd recommend a JS-Solution for the sake of simplicity.
Your API-Endpoints will be quiet simple and easy to customize.
Symfony Forms can be complex. If you want to go this direction I'd recommend you reading this Blog-Post about symfony forms.

elasticsearch returning all found aggregations

I'm using the example application from github.com/searchly/searchly-php-sample with Searchly service.
I've came a simple where I want the search results to return all the aggregations(continued as 'aggs') from the search results, not only the ones I specified.
Currently the code for the aggs is:
$searchParams['body']['aggs']['resolution']['terms']['field'] = 'resolution';
this returns the resolution agg but I can not find the way for it to return all of the possible aggs from the search results.
Is it possible or does it require me to save the aggs some where and then just list them when I do the actual search request?
Thank you!
As far as I know there is no way to do this directly - you have to specify each field you are interested in.
However if you can build up a list of all the fields in the index then you could generate the required aggregations fairly easily.
So, how to build up that list? I can think of three ways that might work
A) build it up by doing some pre-processing before you index each document into ElasticSearch
B) Use the GET MAPPING api to see what fields have been created by dynamic mapping (http://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html)
C) Use a Scripted Metric Aggregation and write scripts that build up a de-duped list of fields in the documents (http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)

apigility - What alternatives I can use for Zend\Paginator\Paginator in Collections?

In several endpoints I'm doing with Apigility, I want retrieve all the possible results without pagination. Is there a class to replace Paginator in Collection?
To receive a response in Hal without pagination you should simply return an array from your queries instead of an instance of Zend\Paginator\Paginator.
For those who happen to find this question just as I did. You can edit the number of items per page in the module.config.php file. It's set as the value "page_size". Changing it to a number larger than the total amount in your collection will let you receive all the results in one page.
Admittedly not the cleanest of solutions, but it worked for me.

how to pass a list of values to a method, Codeigniter style

What's a good way to pass a list of items 2-3 items to a method in my controller?
I was thinking of just using the URL.... like so:
http://myserver/myapp/mycontroller/mymethod/parm1/parm2/listitem1/listitem2/listitem3
Is there a better way to do this? This data is not coming from a form, but rather from a database query and I'm building a hyperlink with it.
I guess the only part that bothers me is that I won't know in advance how many items I have when i'm parsing this url.
Its possible that I'll get none, or all 3 or some value in between. So the method that then has to parse this url will just keep looping until uri->segment() returns false, indicating that it's hit the first empty uri segment.
Any suggestions would be appreciated.
Thanks
EDIT 1:
Just in case it wasn't clear, my model is getting the data from the database and will also build the list.
The question is really about parsing an undetermined number of uri segments.
Just wondering if there's a better way to do this.
Thanks!
EDIT 2
Here's some more information to help you understand my MVC app. I don't think my issue is the way I've organized my code as far as who is doing what.. But just in case it helps...
I have methodA in my model that queries database and passes back to my controller listitem1, listitem2 and listitem2.
The controller then builds a string that represents a URL like:
http://myserver/myapp/mycontroller/methodB/parm1/parm2/listitem1/listitem2/listitem3
Then the view display a hyperlink using the url above.
When the user clicks on this hyperlink, it calls methodB.
In methodB, I since I don't know the number of items, I will just loop through all segments until I hit my first false.
As far as why I need to do this / what I'm doing... here's some background info:
I'm query a database for a list of ports on a switch that are considered trunks - ones that should not be modified.
this is what method A does.
methodB run a command against a switch and it returns a bunch of data back. the view that displays the data from methodB will allow the end user to make further changes to the switch. before I display the data from methodB, i want to filter out the list of ports I got from methodA so they cannot be tampered with.
Hope this helps.
Edit 3
I need both methodA and methodB because they serve two different purposes. methodA displays summary data about ports from my database. Think of methodA as a function that shows documentation about the switch. The view for methodA in turn, provides "live" links to communicate with the actual switch - this is where methodB comes in. methodB is triggered by one of those live links and it goes and gets a list of ports - similar to methodA - except that it represents what actual, and it doesn't include user defined information about the port.
I guess I can have methodB communicate with my database and filter its data before it displays, but if i want to treat these two functions as separate APIs... aka - one set of functions get data out of the database, the other set is a tool to communicate with switches... - then i don't think i want one talking directly to the other. I would like the GUI to tie them together. In fact, i have created two separate models and controllers for what I'll call the database interface, and then the switch interface.
So far, i think the forms idea is the most elegant solution.
Thanks everyone, for reading.
place number of listitems as parametr 3
../mymethod/parm1/parm2/numberofitems/listitem1/listitem2/listitem3
and put 1, 2, or 3 as needed. In case when 0 put nothing - null, however make sure that controller would know what to do if null happend - do not expect items.
If the data is coming from a query it should be within a model in CodeIgniter if you wish for your application to truly MVC compliant. This might mean a restructuring of your application, which may be difficult but it would really benefit you in the future to create a model for all your database queries.
You can read up on codeigniter models here:
http://codeigniter.com/user_guide/general/models.html
And you can read up on the database class here: http://codeigniter.com/user_guide/database/index.html
I really suggest you do this.
If your data is already coming from a model you can call it by including the model:
$this->load->model('model_name');
$response = $this->model_name->model_function(parameters);
Edit: This would also solve the issue of an unknown number of list items as you can simply parse the response returned from the model function instead of trying to figure out a uri hack.
After reading all of the other answers + edits over, that's definitely not the way you want to do it.
Unless I'm misunderstanding your comments, here's the issue: The list of ports is domain data stored on your server. So why then, are you going to pull that data out, send it to the presentation layer, and show it to the user who will send it right back to the application? Skip the middle-man and have "MethodB" get that data.
Your "MethodB" should get this information itself before processing what it needs to do - domain data stays in the domain layer, and the view never sees any of that information directly (the user would see a link directly to "MethodB")
Alternatively, you could do this all in one query if your DB schema is conducive to such a join.

Magento - Retrieve Advanced Search results as a plain array

I am currently working on a Magento application and have a requirement to sort advanced search results based on the precedence of the categories in the store.
Basically, i have the algorithm prepared where i would loop through the advanced search results, run a query to retrieve the position of the products category and then sort the final result set before returning it back to the calling function.
But the issue I'm having is that i am unable to retrieve the search results as a plain array to work with. Could any one of the experts tell a way to retrieve this array please?
Regards,
Maximumus 69
assuming that you're working in list.phtml, this should work.
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->toArray($requiredFields)
where $requiredFields is null (if you want all fields) or an array containing the fields that you're interested in.
Note that your choice to convert to array and then sort is particularly inefficient. You should be using Magento's inbuilt Collection sorting mechanisms. Read the documentation and API then then give setOrder('position') a try.
Good luck,
JD

Categories