Hello,
Sorry for this very long post but, I want to get what information I
can out for these questions. Sorry for my "noobness" to cake. I
guess the questions are simple really but, just want to make
everything clear.
I have an xml file that contains a lot of data that is extracted from
a front-end system. The data is nothing more than classified ads with
editions, categories and text. Currently, I have a controller called
admin tools which house various tools I can use for day to day site
maintenance tasks. One of these tools parses any XML files I upload
to it and replace the system specific formatting codes with HTML
formatting code (ie-\B = <strong>).
So, with that bit of explanation on what I am doing, here are my
questions.
To parse this XML file I am using SimpleXML (from PHP5) which works
like a charm and returns an object instead of an array. This takes
place in a Component instead of the Model. After writing all of this
code, I realized that, data handling should go into the Model, not the
Controller. Is this a correct assumption or would it be better fit
into a component?
My second dilemma is that since all of this is taking place in the
admin tools controller, the data ultimately must get to the classified
controller some way and be able to write each classified ad into the
database. So, regardless of my first question, how do I pass data
from one controller to another? Any links to guide and what not would
be very appreciated.
Third - (Last one I promise). Say I do get the data to the classified
controller and I want to save the data. Since the data is an Object
output by SimpleXML, is there a way to save all this info into the
MySQL database quickly? The table has all the correct fields I need,
I just need to get it in there in one go. Is there any Cake way of
doing this or am I stuck to writing a custom iteration and SQL
commands to INSERT the data? Until now, the only way I am familiar
with getting info into the table is by being on the correct controller
and using a form. But, the form can only hold one set of information
at a time.
So, in retrospect, yes, I am still learning Cake. I guess I could jut
do all the parsing in the Classifieds controller if need be but, I was
going to limit the admin controller as a whole to admins. Instead of
limiting parts of controllers.
To make matters worse, when I save the data into the table, there are
also JOIN tables that I need to take into consideration.
Here is some sample code:
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<ad>
<itemno>00004</itemno>
<acctcode>000000-00000</acctcode>
<class>0010</class>
<table>08</table>
<start>02/22/07</start>
<end>03/01/07</end>
<editions>N</editions>
<specials>Q</specials>
<dispname></dispname>
<dispnote></dispnote>
<disptag></disptag>
<dispcols>0</dispcols>
<dispdepth>0</dispdepth>
<text>
ANTIQUE DOUBLE dresser, ma
hogany, brass knobs, large glass
mirror. $150. 555-555-5555
</text>
</ad>
The component parser:
<?php
uses('sanitize');
class XmlFileParserComponent extends Object {
private $xmlobject = NULL;
private $MrClean = NULL;
private $tmpText = NULL;
private $center;
private $bold;
private $boldx2;
private $editionsArray = array();
private $keepers = array('<', '>', '/', '?', '"', "'", '\\',
'-', '(', ')', ' ',
'.', ',', '!',
'@', '#', '$', '%',
'*', '_', '+',
':', '¤', '¥', '',
'«', '¬', '=',
'[', ']', '{', '}',
';', "\n", "\r");
function startup(&$controller){
$this->controller =& $controller;
}
function parse_classified($file, $size){
if (file_exists($file)){
$this->MrClean = new Sanitize();
$this->clean_data = $this->MrClean-
>paranoid(fread(fopen($file, "r"), $size), $this->keepers);
$this->xmlobject =
simplexml_load_string($this->clean_data);
return $this->xmlobject;
}else{
echo "Cannot find $file\n";
}
}
function fix_text($xml_data){
foreach($xml_data->ad as $this->tmpText){
$this->center = 0;
$this->bold = 0;
$this->boldx2 = 0;
//DO SOME PARSING ETC
$this->tmpText->text = str_replace('¤', 'ñ', $this-
>tmpText->text);
$this->tmpText->text = str_replace('¥', 'Ñ', $this-
>tmpText->text);
$this->tmpText->text = str_replace('', '¢', $this-
>tmpText->text);
$this->tmpText->text = str_replace('«', '½', $this-
>tmpText->text);
$this->tmpText->text = str_replace('¬', '¼', $this-
>tmpText->text);
$this->tmpText->text = str_replace("\n", "<br />\n", $this-
>tmpText->text);
$this->tmpText->text = str_replace("\r", "<br />\n", $this-
>tmpText->text);
}
return $xml_data;
}
}
?>
The controller code:
<?php
class AdminToolsController extends AppController {
public $xml_data = NULL;
public $components = array('XmlFileParser');
function index(){
}
function classified_xml(){
if (!empty($this->params['form']) &&
is_uploaded_file($this->params['form']['File']['tmp_name'])){
move_uploaded_file($this->params['form']['File']
['tmp_name'],
"/var/www/localhost/htdocs/app/tmp/".
$this->params['form']['File']['name']);
$this->xml_data = $this->XmlFileParser-
>parse_classified("/var/www/localhost/htdocs/app/tmp/".
$this->params['form']['File']['name'], $this-
>params['form']['File']['size']);
$this->xml_data = $this->XmlFileParser->fix_text($this-
>xml_data);
$this->set('xmldata', $this->xml_data);
}
}
}
?>
The table i need to get the data into
CREATE TABLE `classifieds` (
`id` int(255) NOT NULL auto_increment,
`user_id` int(255) NOT NULL,
`itemno` int(5) NOT NULL,
`acctcode` varchar(13) NOT NULL,
`category_id`
enum('0010','0050','0150','1100','0400','0450','0700','0650','0635','0800','0850','1000','0950','1050','1125')
NOT NULL default '0650',
`start` date NOT NULL,
`end` date NOT NULL,
`editions` set('B','E','I','N','P','Y') NOT NULL,
`specials` char(1) default NULL,
`dispname` varchar(12) default NULL,
`disptag` text,
`dispcols` enum('0','1') NOT NULL default '0',
`dispdepth` int(3) default NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `itemno` (`itemno`),
KEY `class` (`category_id`),
KEY `start` (`start`),
KEY `end` (`end`),
KEY `dispcols` (`dispcols`),
KEY `dispname` (`dispname`),
KEY `users_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---