So what I want to obtain is to add from time to time articles to the database from those RSS Feeds already added in the Database.
There is a need for a script which should be scheduled after the component installation (using crontab or Windows scheduler depending on the OS)
I will place this php script under /administrator/components/com_rssagregator/ in the Joomla installation folder, and call it "cron.rssagregator.php"
At the beginning I am defining the correct JPATH_BASE from the current file location and include standard Joomla files:
//basic to make J! happy
define('_JEXEC', 1); //make j! happy
define('JPATH_BASE', realpath(substr(dirname(__FILE__),0,strpos(dirname(__FILE__),'administrator'))));;
define('DS', DIRECTORY_SEPARATOR);
// Load up the standard stuff for testing
require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
require_once JPATH_BASE.DS.'includes'.DS.'framework.php';
I found this information on this link: https://docs.joomla.org/Framework_Compatibility
Further an instance of JTable class is created, not before specifying the path to the specific JTable child class:
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_rssagregator/tables');
$article = JTable::getInstance('content','RssagregatorTable',array());
At the specified path: '/components/com_rssagregator/tables' there is a file called content.php containing a class inheriting JTable:
class RssagregatorTablecontent extends JTable
{
public function __construct($db)
{
parent::__construct( '#__content', 'id', $db );
}
}
#__content is the name of the table where the Joomla built in component "com_content" is storing the articles.
You may want to have look on the JTable class documentation here: https://api.joomla.org/cms-3/classes/JTable.html#method_getInstance
For example the JTable::getInstance method:
getInstance
Static method to get an instance of a JTable class if it can be found in the table include paths. To add include paths for searching for JTable classes see JTable::addIncludePath().
getInstance(string $type, string $prefix = 'JTable', array $config = array()) : \JTable|boolean
You can have a look using phpMyAdmin on the #__content table to see the columns names. My component will add values to some of them:
//values to be saved in the database. this hardcoded values should be replaced with data from RSS feeds
$tobind = array(
"title" => "articol de test-3",
"alias" => "articol de test",
"introtext" => "BlueBerry Pie afadf afadfadfa faf a fadfa fad fafafdafa",
"state"=>'1',
"featured"=>'1',
"created_by"=>'453',
"language"=>'*',
"catid" => "2",
"metadata"=>'{"page_title":"","author":"","robots":""}',
);
Using the JTable::save method the information is saved on the database:
//if new article successfully added to database echo success message
if ($article->save($tobind)) {
echo "The article has been added to database. ";
} else {
echo "There was an error. ";
}
Testing the script is done by directly accessing it from browser: http://joomla1/administrator/components/com_rssagregator/cron.rssagregator.php
"joomla1" is the name of my virtual host. If not using virtual host feature the link looks like:
http://127.0.0.1/joomla1/administrator/components/com_rssagregator/cron.rssagregator.php
From your Joomla administrator, under Content menu, Article manager you can see all the articles including the one newly added.
There is an issue, that even the articles are featured ("featured"=>'1') they do not appear in the list of Featured articles (menu Content -> Featured Articles ).
To solve this we need to add the ID of the new added article in the table #__content_frontpage. A new JTable child class is added in the '/components/com_rssagregator/tables' folder to handle the connection:
class RssagregatorTablefeatureditems extends JTable
{
public function __construct($db)
{
parent::__construct( '#__content_frontpage', 'id', $db );
}
}
And in the cron.rssagregator.php I will add:
$article_featured = JTable::getInstance('featureditems','RssagregatorTable',array());
$tobind = array(
"content_id" => $last_id,
);
if ($article_featured->save($tobind)) {
echo "new featured article added to database. ";
} else {
echo "There was an error on getting a new featured article. ";
}
Of course these error/successes messages are just for testing, later should be changed with some code writing on log files.
The 3 files presented in this blog post: cron.rssagregator.php, content.php and featureditems.php are available for downloading here. They should be copied in the correct path presented previously.
No comments:
Post a Comment