Skip to content

Instantly share code, notes, and snippets.

@branquito
Created February 10, 2013 23:47
Show Gist options
  • Save branquito/4751569 to your computer and use it in GitHub Desktop.
Save branquito/4751569 to your computer and use it in GitHub Desktop.
+event image move
<?php
/**
* @version $Id: view.html.php 11393 2009-01-05 02:11:06Z ian $
* @package Joomla
* @subpackage Contact
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant to the
* GNU General Public License, and as distributed it includes or is derivative
* of works licensed under the GNU General Public License or other free or open
* source software licenses. See COPYRIGHT.php for copyright notices and
* details.
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.application.component.view');
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
/**
* @package Joomla
* @subpackage Contacts
*/
class ContactViewSlike extends JView
{
function display($tpl = null)
{
global $mainframe;
// IMAGE-MOVE SCRIPT
//
//
// Get a db connection.
$tbl_passes = array(array('tbl_name'=>'#__content','tbl_field'=>'introtext'),
array('tbl_name'=>'#__content','tbl_field'=>'fulltext'),
array('tbl_name'=>'#__eventlist_events','tbl_field'=>'datdescription')
);
$db = JFactory::getDbo();
// Create a new query object.
// $query = $db->getQuery(true);
$delete_paths = array();
foreach ($tbl_passes as $a_pass) {
# code...
// ..doing query here
if ($a_pass['tbl_name'] == '#__content'){
$query = "select a.id, a.created, `a`.`" . $a_pass['tbl_field'] . "`, b.title from " . $a_pass['tbl_name'] . " AS a INNER JOIN #__categories AS b ON a.catid = b.id ORDER BY a.created ASC";
// add this prefix to all paths where you want to store your images..
$prefix_path = 'images/stories/';
} else {
$query = "select alias, created, " . $a_pass['tbl_field'] . " from " . $a_pass['tbl_name'];
$prefix_path = 'images/stories/events';
}
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$results = $db->loadObjectList();
foreach ($results as $result) {
// parse article as HTML into DOMDocument object
$doc = new DOMDocument();
$doc->loadHTML($result->$a_pass['tbl_field']);
$img_found = false;
// get IMG tags only
$image_tags = $doc->getElementsByTagName('img');
foreach ($image_tags as $one_tag) {
// echo "<b> $result->id </b>" . " " . "<b> $result->title </b>" . "<br>";
// echo "Kreirano: " . $result->created . "<br>";
// echo "OLD PATH : ";
// extract IMG src attr..
$img_path = $one_tag->getAttribute('src');
$filename = basename($img_path);
// echo $img_path . "<br>";
// extract FILENAME only, so we now have a file to be copied..
// echo $filename . "<br>";
// try regex..
//
// format is 2013-02-01.. more general yyyy-mm-dd..
// we will extract year and month
// regex which will remember first four digits [year], when the article was created..
$pattern = "/(\d{4})-\d{2}/";
$subject = $result->created;
// var_dump($subject);
// if image exists do copy and change src attr..
if (file_exists($img_path)) {
// var_dump($result);
echo "<span style=\"color: green;\">image exists<br></span>";
// .. change <img src=''> attrib
// then copy image before going to the next one ..
preg_match($pattern, $subject, $matches);
if ($a_pass['tbl_name'] == '#__content') {
$my_path = $prefix_path . $matches[1] . "/" . JFilterOutput::stringURLSafe($result->title) . "/";
} else {
$my_path = $prefix_path . $matches[1] . "/" . JFilterOutput::stringURLSafe($result->alias) . "/";
}
$new_intro = str_replace($img_path, $my_path . $filename, $result->$a_pass['tbl_field']);
$result->$a_pass['tbl_field'] = $new_intro;
// echo var_dump($new_intro);
// change OS specific delimiter so the COPY command will work as should
$img_path = str_replace("/", DS, $img_path);
$my_path = str_replace("/", DS, $my_path);
echo "Image copied from : " . $img_path . " to " . $my_path;
echo "<br />";
echo "<br />";
// Create Folder if Does Not Exist
if (!is_dir(JPATH_BASE . DS . $my_path)){
$created = JFolder::create(JPATH_BASE.DS.$my_path);
}
// COPY IMAGE
if (copy(JPATH_BASE.DS.$img_path, JPATH_BASE.DS.$my_path.$filename)){
$delete_paths[]=JPATH_BASE.DS.$img_path;
// unlink(JPATH_BASE.DS.$img_path);
}
// copy(JPATH_BASE.DS.$img_path, JPATH_BASE.DS.$my_path.$filename);
// TEST NEW PATH
// echo "<span style=\"font-size: 23px\">$my_path</span>";
// echo var_dump($matches);
// echo "<br>";
// set flag for update query to know when to update an article
$img_found = true;
}
// IMAGE DOES NOT EXIST, TELL USER!
else {
echo "<span style=\"color: red;\">image does not exist<br></span>";
}
}
// if article contains image/images do update content
if ($img_found){
// echo(JPATH_BASE . DS . $img_path . "," . JPATH_BASE . DS . $my_path.$filename);
// echo "<BR>";
// echo "<br>";
// Create a new update query object.
$query_update = $db->getQuery(true);
// ..doing query here
if ($a_pass['tbl_name'] == '#__content'){
$query_update = 'update #__content SET `' . $a_pass['tbl_field'] . '` = "' . mysql_real_escape_string($new_intro) . '" WHERE id = "' . $result->id . '"';
} else {
$query_update = 'update #__eventlist_events SET ' . $a_pass['tbl_field'] . ' = "' . mysql_real_escape_string($new_intro);
}
$db->setQuery($query_update);
$db->query();
}
}
}
foreach ($delete_paths as $path_to_delete) {
// var_dump($path_to_delete);
unlink($path_to_delete);
}
echo "Deleted " . count($delete_paths) . " images";
$this->assignRef('contact', $contact);
$this->assignRef('contacts', $contacts);
$this->assignRef('params', $pparams);
parent::display($tpl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment