Joomla and dynamic content/pages from mysql - php

i'm using Joomla 1.6 + mysql
What I want to do at this point is to create dynamic links using content that exists on the database.
At this point i'm calling a function in php that creates an ordered list with the contents of a certain table, but right now I want that those items contain not only the text on the database but also a link associated with that item, so that i can display inside my joomla website the content on the database.
In other words I have 'N' itens that will be changed over time, and i need to display those 'N' items using an ordered list (this part is already working for 'N' items) and then each item contains a link to a joomla page/article/... dynamically generated in some sort of way in order to show contents on the database associated with that Item.
Please note that this has to work dynamically because content on the database will change over time and the number of links can contain 0 to N items.
Any ideas or pointers to do this?
Thank you

The best way to do this is to create a small content plugin.
function onPrepareContent( &$article, &$params, $limitstart ) {
$plugin =& JPluginHelper::getPlugin('content', 'myplugg');
$pluginParams = new JParameter( $plugin->params );
// get your data from the db
$rows = ...
// assemble the html for insertion
$content2insert = ...
// append the content into the joomla article
$article->text = $article->text . $content2insert;
return true;
}
Here is a full tutorial on content plugins:
http://docs.joomla.org/Creating_a_content_plugin

Related

Remove duplicate records from view result with pager using search api

My project is setup in Drupal 7 and i am using search API for display my custom search result. Actually i want to show search result based on two content types content so i have configure the search api for those content type and select the fields which i want for search and then indexing the content.
For display the search result i am using drupal View of Index node type. But the problem is that in my search result i am getting duplicate result because of some contents have same titles but having different body text.
I want to remove search result which have duplicate title. (Custom field created by myself)
I have tried some custom solution by View hook but its not working properly its give pager issue
function custom_views_pre_render(&$view) {
if ($view->name == 'search_books') {
$view_ISBN = array();
foreach ($view->result AS $key => $res_view) {
$ISBN = $res_view->entity->field_isbn[und][0]['value'];
if (!in_array($ISBN,$view_ISBN)) {
$view_ISBN[] = $ISBN;
unset($view->result[$key]);
}
}
$view->query->pager->total_items = count($view->result);
$view->query->pager->update_page_info();
}
}
This remove the duplicate result but it gives wrong pager result.
I have also tried the Query alter hook but its not working for me.
function custom_query_alter($query) {
if (isset($query->alterMetaData)) {
if (isset($query->alterMetaData['view'])) {
if($query->alterMetaData['view']->name == 'search_books') {
$fields =& $query->getGroupBy();
// Tried various fields to check which was the field creating the problem.
$query->groupBy('field_isbn');
$query->distinct = TRUE;
}
}
}
}
I have also tried to install "views_distinct" module and distinct the field from view but its also gives me pager issue.
Any Idea how to solve this issue?
You can use Grouping field to use Grouping field from you views Format or you can Use
Use aggregation from view Advanced section

Getting an array with all positions names from a Joomla template

I want to get an array with all current template positions names as listed in the respective templateDetails.xml.
I tried:
$this->params->get('positions')
But it didn't work.
The code will be called or inserted into /templates/mytemplate/index.php file.
I've seen in the module parameters page a select that displays all available positions from all installed templates, maybe it gives a clue on how to achieve it.
Recently I edited a post in joomla docs here https://docs.joomla.org/JFactory/getXMLParser. Hardly 1 week back as it was not updated for a long time. You can get template position this way after giving templateDetails.xml location.
$xmlfile = 'templates/protostar/templateDetails.xml';//Change to your template
$xml = JFactory::getXML( $xmlfile );
//The position array is this `$xml->positions->position`
foreach ($xml->positions->position as $position) {
echo $position . "<br />";
}

Force Joomla JRoute to use the menu item

I'm building a component for Joomla! 2.5 and inside I'm using JRoute::_('index.php?option=com_myapp&view=cpanel') to build all my links. This works, BUT it produces links which look like that:
/component/myapp/cpanel.html
In the menu however I have defined that index.php?option=com_myapp&view=cpanel has an alias of "myapp" so the link should rather be
/myapp/cpanel.html
The component is accessible via this path. If I do that, also all links generated inside will have the /myapp prefix. But for use in a template (special login link) and if the user stumbles upon /component/myapp.... I still want all links to go to /myapp prefix.
How can I force JRoute to use this menu item entry by itself?
Because I struggled a lot with this issue, here's a quick function I did that will try to find the link based on a menu item. It's mainly based on other answers.
This has only been tested on Joomla 3.5. JRequest is deprecated, so $app->input is used instead. This might be done better, but at least it works for the use I have of it. Hopes it can help other people too !
public static function getRouteMenu($default_url) {
$db = JFactory::getDbo();
$app = JFactory::getApplication();
$jinput = $app->input;
$db->setQuery('SELECT `id` FROM #__menu WHERE `link` LIKE '. $db->Quote($default_url) .' AND `published` = 1 LIMIT 1' );
$itemId = $db->loadResult();
if($itemId) {
$route = $jinput->getString('return', JRoute::_('index.php?Itemid='.$itemId));
$route = str_replace('index.php/', '', $route);
} else {
$route = $jinput->getString('return', JRoute::_($default_url));
}
return $route;
}
The parameter requires a not rewrited link such as : index.php?option=com_content&view=article&id=10. Check your database if you need to be sure about how to build up this link.
//look if there is a menu item set for myapp. if yes, we use that one to redirect
$db = JFactory::getDBO();
$defaultRedirect = 'index.php?option=com_myapp&view=cpanel';
$db->setQuery('SELECT `id` FROM #__menu WHERE `link` LIKE '. $db->Quote($defaultRedirect) .' LIMIT 1' );
$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
if($itemId){
$rpath = JRequest::getString('return', base64_encode(JRoute::_('index.php?Itemid='.$itemId)));
}else{
$rpath = JRequest::getString('return', base64_encode(JRoute::_('index.php?option=com_myapp&view=cpanel')));
}
Beware: This is NOT language-safe. It takes the first menu entry found in the db. If you setup different menu aliases for different languages you have to check for the right language in the SQL query, too.
You ideally need to set a router as part of your component. The menu manager will override the component/myapp bit, but what comes after that will be determined by what is in the router.php file.
http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
If, however, you simply need to create a link to a specific menu item, then you can always just add an Itemid to your parameter
JRoute::_('index.php?Itemid=12')
will point to any menu item with the id 12, so if that is your menu item ID for your com_myapp component, it will create the menu version of the link to that page.
edit:
You could technically assign the same component to two menu items, so I don't believe that handling the beginning of the URL is default behaviour when you link to a component outside of a menu item without giving the link an itemid. Joomla say (on creating a link for JRoute)...
"Notice that it is possible to leave out the parameters option and
Itemid. option defaults to the name of the component currently being
executed, and Itemid defaults to the current menu item's ID."
ie, call it from your homepage (or any page outside of one set to use your component) without an itemid or option and it is likely to think you are in com_content and with the homepage menu item.
If you want to link to the component outside of a menu item but without hard-coding an itemid, then one way would be to add a Menuitem field type to the config options of the module or template where you are linking from, and then add the itemid it generates to your link.
You could also use JFactory::getApplication()->getMenu() to find which menu item is linking to your component. Not had chance to test this - but using that could also mean that you can set an itemid parameter in your router.

How can I get an id from a simplepie post which can be used to look it up later?

I've recently started developing a portfolio website which I would like to link to my wordpress blog using simplepie. It's been quite a smooth process so far - loading names and descriptions of posts, and linking them to the full post was quite easy. However, I would like the option to render the posts in my own website as well. Getting the full content of a given post is simple, but what I would like to do is provide a list of recent posts which link to a php page on my portfolio website that takes a GET variable of some sort to identify the post, so that I can render the full content there.
That's where I've run into problems - there doesn't seem to be any way to look up a post according to a specific id or name or similar. Is there any way I can pull some unique identifier from a post object on one page, then pass the identifier to another page and look up the specific post there? If that's impossible, is there any way for me to simply pass the entire post object, or temporarily store it somewhere so it can be used by the other page?
Thank you for your time.
I stumbled across your question looking for something else about simplepie. But I do work with an identifier while using simplepie. So this seems to be the answer to your question:
My getFeedPosts-function in PHP looks like this:
public function getFeedPosts($numberPosts = null) {
$feed = new SimplePie(); // default options
$feed->set_feed_url('http://yourname.blogspot.com'); // Set the feed
$feed->enable_cache(true); /* Enable caching */
$feed->set_cache_duration(1800); /* seconds to cache the feed */
$feed->init(); // Run SimplePie.
$feed->handle_content_type();
$allFeeds = array();
$number = $numberPosts>0 ? $numberPosts : 0;
foreach ($feed->get_items(0, $number) as $item) {
$singleFeed = array(
'author'=>$item->get_author(),
'categories'=>$item->get_categories(),
'copyright'=>$item->get_copyright(),
'content'=>$item->get_content(),
'date'=>$item->get_date("d.m.Y H:i"),
'description'=>$item->get_description(),
'id'=>$item->get_id(),
'latitude'=>$item->get_latitude(),
'longitude'=>$item->get_longitude(),
'permalink'=>$item->get_permalink(),
'title'=>$item->get_title()
);
array_push($allFeeds, $singleFeed);
}
$feed = null;
return json_encode($allFeeds);
}
As you can see, I build a associative array and return it as JSON what makes it really easy using jQuery and ajax (in my case) on the client side.
The 'id' is a unique identifier of every post in my blog. So this is the key to identify the same post also in another function/on another page. You just have to iterate the posts and compare this id. As far as I can see, there is no get_item($ID)-function. There is an get_item($key)-function but it is also just taking out a specific post from the list of all posts by the array-position (which is nearly the same way I suggest).

Zend Framework Lucene Boolean / "Google"-like search

I'm working on the application at http://demos.zatechcorp.com/codeigniter/
In its current incarnation running on my machine, I loaded the ZendFramework inside Codeigniter, and generated an index, like this:
// ... Some code that loads all the markets
foreach ($markets as $market)
{
$doc = new Zend_Search_Lucene_Document();
// Id for retrieval
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('id', $market->id));
// Store document URL to identify it in search result.
$doc->addField(Zend_Search_Lucene_Field::Text('url', $market->permalink));
// Index document content
$doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $market->description));
// Title
$doc->addField(Zend_Search_Lucene_Field::Text('title', $market->title));
// Phone
$doc->addField(Zend_Search_Lucene_Field::Keyword('phone', $market->phone));
// Fax
$doc->addField(Zend_Search_Lucene_Field::Keyword('fax', $market->fax));
// Street
$doc->addField(Zend_Search_Lucene_Field::Keyword('street', $market->street));
// City
$doc->addField(Zend_Search_Lucene_Field::Keyword('city', $market->city));
// State
$doc->addField(Zend_Search_Lucene_Field::Keyword('state', $market->state));
// Zip
$doc->addField(Zend_Search_Lucene_Field::Keyword('zip', $market->zip));
// Type
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('type', 'market'));
// Store Document
$index->addDocument($doc);
}
In my search, I do this:
$hits = $index->find($q);
This works with simple words, but when I want to do a search like "Sheba Foods" (quotes included), it returns one result, but the wrong one, which doesn't even have the word "Sheba".
I moved away from MySQL full-text search because of its obvious problems, and can't make any headway with this.
I've been looking at the Zend_Search_Lucene_Search_QueryParser::parse() method. Does the answer lie in this method?
I figured it out. With Lucene, you can add a field with the name 'id', but retrieving id from a hit gives you something different -- I'll guess this is the id of the search term within the entire search results.
What I had to do in this case was use a different field name like this:
// Id for retrieval
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('item_id', $market->id));
I have used MySQL full-text search in the past, but it's really CPU intensive.
You could always rely on a SELECT * FROM table WHERE column = '%query%'

Categories