Joomla - display number of articles published each month - joomla

How do I create a module that would display the number of articles in one category that have been published each month. Something similar to WordPress, eg.
December 2016 (3)
November 2016 (5)
etc.
Thanks

You should follow joomla tutorial to create a new module, that's quite simple : https://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module/en
Once you have created the hellow world module as described in tutorial, you should modify the file helper.php and create a new function :
public static function getPublicationsByMonth(){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('DATE_FORMAT(created, "%M %Y") as date, count(*)')
->from('#__content')
->where('state = 1')
->group('date');
$db->setQuery($query);
return $db->loadObjectList();
}
Then in tpl/default.php you should use result of this method to display results as you want.

Related

Show list of registered users base on current month using laravel

Im new to laravel and im tryin to learn the fundamentals of it. My question is how can I display all the registered users that is registered only on the current month.
$from = now()->startOfMonth(); // first date of the current month
$to = now();
$usersRegisteredThisMonth = User::whereBetween('created_at', [$from, $to])->get();
There is a simple way of doing it.
Just use this code
User::whereMonth('created_at', now()->month) // checking if the month of created_at is current month
->whereYear('created_at', now()->year) // checking if the year of created_at is current year
->get();
This line will give you Users from current month.
Nobody explained that these queries might be put in user model with local scope scopeWith magic method. I assume you read docs from top to bottom.
Simple example:
public function scopeRegisteredLastMonth($query){ return $query->whereBetween... }
Laravel Local Scope
You can specyficy additional arguments for this method.
The final call in controller will look like this:
$lastMonthUsers = User::registeredLastMonth()->get();
This function should be set to something like 'public function withRegisteredBetween($query, $date_start, $date_end) and return query based on date range.
PS: Don't use DB:: when you can use Model::method() or Model::query()->method()
PS2: for date management I advise you to install carbon, it's an additional addon - sometimes it's easy, sometimes not, overall not bad.
You could use the users table with a whereBetween clause like this:
$from = date('2022-01-05 00:00:00');
$to = date('2022-31-05 00:00:00');
$usersRegisteredThisMonth = DB::table('users')->
whereBetween('created_at', [$from, $to])->get();

Editing product page with dimsav/laravel-translatable

im using a package called Laravel-Translatable
but is giving me more problemns that i expect, mainly with quite simple tasks. For example i have a list of all records (products), and each of them haves 2 languages translated (en,es). But now i need to edit the product information to put in the inputs fields, and for this i wish in my edit page get all the translated details (title, description), but for some reason, is returning me only one language, it doesnt return all the translated details from a specific product:
ex: return Product::where('id', '2')->get();
Somebody uses this package?
Just read the documentation. Therein is all that you need.
Some example:
$product = Product::where('id', 2)->get();
$product->translate('de')->title = "Germany title";
$product->translateOrNew('pl')->title = "Polish title";
//Shortcut
$product->{'title:pl'} = 'lorem ipsum';
$product->save(); //It Will save all translations and main model

joomla 2.5 get seo link by article id

let´s say I have following id´s from articles (#__content)
3,4,5 and I want to know the SEO URLs for these ID´s within my template.
pseudo code:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$db->setQuery($query);
$query
->select(array('a.seolink'))
->from('#__content AS a')
->where("a.id = '3'" );
Anyone knows a query or function which does the job?
with kind regards,
tony
If you have article slug ("id:alias") and category slug ("catid:catalias"), then you could do
$link = JRoute::_(ContentHelperRoute::getArticleRoute($slug, $catslug));
From #__content you can get id, alias and catid, so you just have to get category alias too (since I think it won't work without it, but you can try)
Offtopic: anyone interested in adding tag synonyms for Joomla, please give your opinion

Sanitize input in joomla

I have code like this in my Joomla plugin:
$some_id = $_GET["someid"];
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table WHERE id = '$some_id'");
$result = $db->loadRow();
Does Joomla sanitize this automatically, or i need to do something (and what) to sanitize this query ? Using Joomla 2.5.
There is no need to sanitize database queries when using Joomla. The information you are pulling down is the information that has put put there or already there, and thus you don't want to change. I would also recommend using Joomla 2.5 coding standards to make database queries, like so:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('*'))
->from($db->quoteName('#__table'))
->where($db->quoteName('id') . ' = ' . $db->quote($some_id));
$db->setQuery($query);
$rows = $db->loadRow(); //or loadResult()
The only time I have ever needed to sanitize (so to speak) something was when handling files, in which case I used JFile::makeSafe();.
Please take a look at you will get your answer
Secure coding guidelines
http://docs.joomla.org/Secure_coding_guidelines
Take a look at JInput and this corresponding documentation
Filter example:
$jinput = JFactory::getApplication()->input;
$some_id = $jinput->get('someid', '', 'string');

Migrating Wordpress Images to Drupal with Migrate 2.4

I'm having a time migrating images from Wordpress to Drupal with the Migrate 2.4 module. Here are my mappings:
$this->addFieldMapping('field_image','images');
$this->addFieldMapping('destination_file', 'images');
$this->addFieldMapping('field_image:source_dir')
->defaultValue('/Users/grafa/htdocs/wordpress/wp-content/uploads');
$this->addFieldMapping('field_image:file_class')
->defaultValue('MigrateFileUri');
The images come from a function that queries the wp_postmeta table then returns the result to the prepareRow() function.
function getImages($row) {
$post_id = $row->id;
$results = db_query("
SELECT pm.post_id, pm.meta_key, pm.meta_value FROM streetroots_wp.wp_postmeta AS pm LEFT JOIN streetroots_wp.wp_posts AS p ON pm.post_id=p.id WHERE p.post_parent = $post_id AND pm.meta_key='_wp_attached_file';");
$images = array();
foreach($results as $result) {
$images[] = $result->meta_value;
}
return !empty($images) ? $images : NULL;
}
This basically returns the image name and relative path from the wp_postmeta table something like '2012/05/figure1.jpg'. I then use prepareRow() like this:
function prepareRow($row) {
$row->images = $this->getImages($row);
}
I'm guessing there's something funky with how I'm using the new-ish migrate module that handles the file fields. The sql is outputs the file names correctly but it doesn't seem like the images are getting copied over. This is a Drupal 7 using Migrate 2.4. Any help is appreciated.
You might want to have a look at the beer.inc lines 377 - 383 within migrate_example folder and at the associated content type *migrate_example_beer* which has an image field defined not a file field which was my mistake and lead to my images not being populated.
Hope this helps!
Migrate 2.5 has largely simplified the handling of files, you should check it out!

Resources