spatie / laravel-sitemap | Adding new url replacing the previous file completely - laravel

I am using spatie/laravel-sitemap package to generate sitemap. I am able to generate sitemap nicely but when i try to add some new url it is replacing the previous urls completely. I am using the code below. Any suggestion would be very much appreciated.
$keywords = Keyword::all();
$sitemap = SitemapGenerator::create(config('app.url'))
->getSitemap();
$location = Location::where('id', $location_id)->select('id', 'name', 'city_id')->with('city:id,name')->first();
foreach ($keywords as $keyword) {
$url = Url::create("/toptechnicians/{$keyword->keyword}/{$location->city->name}/{$location->name}");
$url2 = Url::create("/topbusinesses/{$keyword->keyword}/{$location->city->name}/{$location->name}");
$sitemap
->add($url
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1))
->add($url2
->setLastModificationDate(Carbon::yesterday())
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1))
->writeToFile(public_path('sitemap.xml'));
}

Related

laravel Backpack Original Image Name

I am using laravel backpack for my site and i need to upload some images, i copy the code from their website and works fine, but i need the original name on the images, i tried some things, but is not working.
public function setImageAttribute($value)
{
$attribute_name = "image";
$disk = "uploads";
$destination_path = 'storage/services/' .date('FY').DIRECTORY_SEPARATOR;
if ($value==null) {
Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
if (str_starts_with($value, 'data:image'))
{
$file = $value;
$filename = $this->generateFileName($file, $destination_path);
$image = InterventionImage::make($file)->orientate();
$fullPath = $destination_path.$filename.'.'.$file->getClientOriginalExtension();
$this->attributes[$attribute_name] = $fullPath;
}
protected function generateFileName($file, $destination_path)
{
$filename = basename($file->getClientOriginalName(), '.'.$file->getClientOriginalExtension());
$filename = Str::random(20);
while (Storage::disk('uploads')->exists($destination_path.$filename.'.'.$file->getClientOriginalExtension())) {
$filename = Str::random(20);
}
return $filename;
}
why value alwys take image base64
is there any way to git image original name?
As far as I know, you won't be able to retrieve the filename, as it's always a base64 file. But maybe this work around helps you:
Image fields blade file can be found at:
\vendor\backpack\crud\src\resources\views\crud\fields\image.blade.php
You can overwrite it by making a file on the same name at:
\resources\views\vendor\backpack\crud\fields\image.blade.php
You can change anything there, maybe you can add a hidden input with the file name.

Laravel : save a docx as PDF

I'm pretty new to laravel and I'm currently having some difficulties with documents. my problem as follows:
I have .docx document that I modify using \PhpOffice\PhpWord\TemplateProcessor, after that I need to convert the resulted document to a pdf file in order to display it on my view.
I tried Mpdf but I noticed that it doesnt work with docx :
public function topdf($bill){
$Mpdf = new Mpdf();
$sourceFile = storage_path( $bill);
$pageCount = $Mpdf->SetSourceFile($sourceFile);
for ($i = 1; $i <= ($pageCount); $i++) {
$Mpdf->AddPage();
$import_page = $Mpdf->ImportPage($i);
$Mpdf->UseTemplate($import_page);
$Mpdf->WriteText(75, 73.3, 'hello');
}
$file_name = 'app/public/' . Str::random(18) . '.pdf';
$Mpdf->Output(storage_path($file_name),Destination::DOWNLOAD);
}
If $bill is a file.docx this doesn't work, but if it's a file.pdf it imports it to another new pdf.
I'm stucking in this problem it's been a while. Any help please

Modify the existing canonical link in header

I am using Joomla 2.5 and I want to change the canonical link in the header.
I do this in category view (components/com_content/category/tmpl/default.php)
$url = JURI::root();
$sch = parse_url($url, PHP_URL_SCHEME);
$server = parse_url($url, PHP_URL_HOST);
$canonical = $this->escape($_SERVER['REQUEST_URI']);
$document->addCustomTag('<link rel="canonical" href="'.$sch.'://'.$server.$canonical.'"/>');
It prints the right canonical, but it also leaves the old canonical link there so that I have 2 canonical links in the header.
How can I change or delete the old canonical link?
I have found the following to work for me with Joomla! 3.2.1. You can directly modify the
$_links
variable in the JHtmlDocument object.
I'm doing a subset of the following in a particular view of my component because the URL that Joomla! is coming up with is not correct.
Hope this helps.
$document = JFactory::getDocument();
foreach($document->_links as $key=> $value)
{
if(is_array($value))
{
if(array_key_exists('relation', $value))
{
if($value['relation'] == 'canonical')
{
// we found the document link that contains the canonical url
// change it!
$canonicalUrl = 'http://www.something.com/index.php/component/my-component-name-here/?view=viewNameHere&parameterNameHere=parameterValueUsedInTheViewRightNow
$document->_links[$canonicalUrl] = $value;
unset($document->_links[$key]);
break;
}
}
}
}
What you probably want to do instead is something like the following:
$doc_data = $document->getHeadData();
$url = JURI::root();
$sch = parse_url($url, PHP_URL_SCHEME);
$server = parse_url($url, PHP_URL_HOST);
$canonical = $this->escape($_SERVER['REQUEST_URI']);
$newtag = '<link rel="canonical" href="'.$sch.'://'.$server.$canonical.'"/>'
$replaced = false;
foreach ($doc_data['custom'] as $key=>$c) {
if (strpos($c, 'rel="canonical"')!==FALSE) {
$doc_data['custom'][$key] = $newtag;
$replaced = true;
}
}
if (!$replaced) {
$doc_data['custom'][] = $newtag;
}
$document->setHeadData($doc_data);
This will grab all of the current head data from the document, including the canonical link that you want to replace. It will search through the custom set (where I'm guessing this will be) and if it finds it, replace it with yours. If it doesn't find it, then it tacks it on at the end. Just in case.
Potential problems with this that I can see right away:
If the tag contained rel='canonical' with single quotes it would not be found, so you may have to adjust that.
The tag may have been placed in a different section of what I've termed $doc_data. You may want to do a var_dump($doc_data}; to confirm the location of the variable in this array.

How to set Component parameters in J2.5?

I've created a J2.5 component with some config fields using config.xml in the admin folder of the component.
How can I set parameters in the config programatically?
I've tried the code bellow, but it obviously doesn't save the result to the DB:
$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
Could anyone please show some examples of how to achieve this?
The safest way to do this would be to include com_config/models/component.php and use it to validate and save the params. However, if you can somehow validate the data params yourself I would stick with the following (much more simple solution):
// Get the params and set the new values
$params = JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
// Get a new database query instance
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query
$query->update('#__extensions AS a');
$query->set('a.params = ' . $db->quote((string)$params));
$query->where('a.element = "com_mycomponent"');
// Execute the query
$db->setQuery($query);
$db->query();
Notice how I cast the params to a string (when building the query), it will convert the JRegistry object to a JSON formatted string.
If you get any caching problems, you might want to run the following after editing the params:
From a model:
$this->cleanCache('_system');
Or, else where:
$conf = JFactory::getConfig();
$options = array(
'defaultgroup' => '_system',
'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
);
$cache = JCache::getInstance('callback', $options);
$cache->clean();
The solution is here...
http://www.webtechriser.com/tutorials/82-joomla-3-0/86-how-to-save-component-parameters-to-database-programmatically
You can replace in Joomla 2.5+ the
// check for error
if (!$table->check()) {
$this->setError('lastcreatedate: check: ' . $table->getError());
return false;
}
if (!$table->store()) {
$this->setError('lastcreatedate: store: ' . $table->getError());
return false;
}
with
if (!$table->save()) {
$this->setError('Save Error: ' . $table->getError());
return false;
}

Get original image url Magento (1.6.1.0)

I have the following piece of code:
$cProduct = Mage::getModel("catalog/product");
foreach($products_id as $product_id) {
$product = $cProduct->load($product_id);
//$products_image[] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).str_replace(Mage::getBaseUrl('media'),"", $product);
//$products_image[] = $product->getMediaConfig()->getMediaUrl($_product->getData('image'));
$products_image[] = $product->getImageUrl(); //product's image url
}
As you can see, I've tried several ways to get the original image url. Currently I'm using getImageUrl(), but it retrieves the base image, which is a cropped version. How can I retrieve the original image??
Thanks in advance.
Edit:
Seriously appears there isn't a function for it, been Googling for hours straight (also before posting here). So I've written my own function.
function get_original_image_url($base_image_url) {
$exploded = explode("/", $base_image_url);
$image_name = $exploded[count($exploded) - 1];
$original_image_url = "http://yoursitehere.com/media/catalog/product/" . $image_name[0] . "/" .
$image_name[1] . "/" . $image_name;
return $original_image_url;
}
I call it with:
$original = get_original_image_url($product->getImageUrl());
Works for me, though it isn't a nice way to do it.
You should use the catalog product media config model for this purpose.
<?php
//your code ...
echo Mage::getModel('catalog/product_media_config')
->getMediaUrl( $product->getImage() ); //getSmallImage(), getThumbnail()
Hope this helps.
Other faster way:
$cProduct = Mage::getModel("catalog/product");
$baseUrl = Mage::getBaseUrl('media') . 'catalog/product/';
foreach($products_id as $product_id) {
$product = $cProduct->load($product_id);
$products_image[] = $baseUrl . $product->getImage();
}

Resources