Salve, mi sono messo da poco allo sviluppo di un componente. Vorrei mettere un image uploader dentro il mio componente com_reviews. Quello che fa già è creare delle recensioni su un ristorante. vorrei associare un immagine alla recensione con
jfile::upload. In rete ho trovato un pò di esempi ma non sono comunque riuscito a risolvere il problema. Il codice è il seguente:admin.reviews.html.php defined( '_JEXEC' ) or die( 'Restricted access' );
class HTML_reviews
{
function editReview( $row, $lists, $option )
{
$editor =& JFactory::getEditor();
JHTML::_('behavior.calendar');
?>
<form action="index.php" method="post"
name="adminForm" id="adminForm">
<fieldset class="adminform">
<legend>Details</legend>
<table class="admintable">
<tr>
<td width="100" align="right" class="key">
Name:
</td>
<td>
<input class="text_area" type="text" name="name"
id="name" size="50" maxlength="250"
value="<?php echo $row->name;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Address:
</td>
<td>
<input class="text_area" type="text" name="address"
id="address" size="50" maxlength="250"
value="<?php echo $row->address;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Reservations:
</td>
<td>
<?php
echo $lists['reservations'];
?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Quicktake:
</td>
<td>
<?php
echo $editor->display( 'quicktake', $row->quicktake ,
'100%', '150', '40', '5' ) ;
?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Review:
</td>
<td>
<?php
echo $editor->display( 'review', $row->review ,
'100%', '250', '40', '10' ) ;
?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Notes:
</td>
<td>
<textarea class="text_area" cols="20" rows="4"
name="notes" id="notes" style="width:500px"><?php echo
$row->notes; ?></textarea>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Smoking:
</td>
<td>
<?php
echo $lists['smoking'];
?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Credit Cards:
</td>
<td>
<input class="text_area" type="text" name="credit_cards"
id="credit_cards" size="50" maxlength="250"
value="<?php echo $row->credit_cards;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Cuisine:
</td>
<td>
<input class="text_area" type="text" name="cuisine"
id="cuisine" size="31" maxlength="31"
value="<?php echo $row->cuisine;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Average Dinner Price:
</td>
<td>
$<input class="text_area" type="text"
name="avg_dinner_price"
id="avg_dinner_price" size="5" maxlength="3"
value="<?php echo $row->avg_dinner_price;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Review Date:
</td>
<td>
<?php echo JHTML::calendar($row->review_date, 'review_date', 'review_date'); ?>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Published:
</td>
<td>
<?php
echo $lists['published'];
?>
</td>
</tr>
</table>
</fieldset>
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<input type="hidden" name="option" value="<?php echo $option;?>" />
<input type="hidden" name="task" value="" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
<?php
}
function showReviews( $option, &$rows, &$pageNav )
{
?>
<form action="index.php" method="post" name="adminForm">
<table class="adminlist">
<thead>
<tr>
<th width="20">
<input type="checkbox" name="toggle"
value="" onclick="checkAll(<?php echo
count( $rows ); ?>);" />
</th>
<th class="title">Name</th>
<th width="15%">Address</th>
<th width="10%">Reservations</th>
<th width="10%">Cuisine</th>
<th width="10%">Credit Cards</th>
<th width="5%" nowrap="nowrap">Published</th>
</tr>
</thead>
<?php
jimport('joomla.filter.output');
$k = 0;
for ($i=0, $n=count( $rows ); $i < $n; $i++)
{
$row = &$rows[$i];
$checked = JHTML::_('grid.id', $i, $row->id );
$published = JHTML::_('grid.published', $row, $i );
$link = JFilterOutput::ampReplace( 'index.php?option=' . $option . '&task=edit&cid[]='. $row->id );
?>
<tr class="<?php echo "row$k"; ?>">
<td>
<?php echo $checked; ?>
</td>
<td>
<a href="<?php echo $link; ?>">
<?php echo $row->name; ?>[/url]
</td>
<td>
<?php echo $row->address; ?>
</td>
<td>
<?php echo $row->reservations; ?>
</td>
<td>
<?php echo $row->cuisine; ?>
</td>
<td>
<?php echo $row->credit_cards; ?>
</td>
<td align="center">
<?php echo $published;?>
</td>
</tr>
<?php
$k = 1 - $k;
}
?>
<tfoot>
<td colspan="7"><?php echo $pageNav->getListFooter(); ?></td>
</tfoot>
</table>
<input type="hidden" name="option" value="<?php echo $option;?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
<?php
}
function showComments( $option, &$rows, &$pageNav )
{
?>
<form action="index.php" method="post" name="adminForm">
<table class="adminlist">
<thead>
<tr>
<th width="20">
<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $rows ); ?>);" />
</th>
<th class="title">Review Name</th>
<th width="15%">Commenter</th>
<th width="20%">Comment Date</th>
<th width="30%">Comment</th>
</tr>
</thead>
<?php
jimport('joomla.filter.output');
$k = 0;
for ($i=0, $n=count( $rows ); $i < $n; $i++) {
$row = &$rows[$i];
$checked = JHTML::_('grid.id', $i, $row->id );
$link = JFilterOutput::ampReplace( 'index.php?option=' . $option . '&task=editComment&cid[]='. $row->id );
?>
<tr class="<?php echo "row$k"; ?>">
<td><?php echo $checked; ?></td>
<td><a href="<?php echo $link; ?>"><?php echo $row->name; ?>[/url]</td>
<td><?php echo $row->full_name; ?></td>
<td><?php echo JHTML::Date($row->comment_date); ?></td>
<td><?php echo substr($row->comment_text, 0, 149); ?></td>
</tr>
<?php
$k = 1 - $k;
}
?>
<tfoot>
<td colspan="5"><?php echo $pageNav->getListFooter(); ?></td>
</tfoot>
</table>
<input type="hidden" name="option"
value="<?php echo $option;?>" />
<input type="hidden" name="task" value="comments" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
<?php
}
function editComment ($row, $option)
{
JHTML::_('behavior.calendar');
?>
<form action="index.php" method="post" name="adminForm" id="adminForm">
<fieldset class="adminform">
<legend>Comment</legend>
<table>
<tr>
<td width="100" align="right" class="key">
Name:
</td>
<td>
<input class="text_area" type="text" name="full_name" id="full_name" size="50" maxlength="250" value="<?php echo $row->full_name;?>" />
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Comment:
</td>
<td>
<textarea class="text_area" cols="20" rows="4" name="comment_text" id="comment_text" style="width:500px"><?php echo $row->comment_text; ?></textarea>
</td>
</tr>
<tr>
<td width="100" align="right" class="key">
Comment Date:
</td>
<td>
<?php echo JHTML::calendar($row->comment_date, 'comment_date', 'comment_date'); ?>
</td>
</tr>
</table>
</fieldset>
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="task" value="" />
<?php echo JHTML::_( 'form.token' ); ?>
</form>
<?php
}
}?>
admin.reviews.php
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( JApplicationHelper::getPath( 'admin_html' ) );
require_once( JPATH_COMPONENT.DS.'controller.php' );
JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
$controller = new ReviewController( array('default_task' => 'showReviews') );
$controller->execute( JRequest::getVar( 'task' ) );
$controller->redirect();
?>
controller.php
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
class ReviewController extends JController
{
function __construct( $default = array() )
{
parent::__construct( $default );
$this->registerTask( 'add' , 'edit' );
$this->registerTask( 'apply', 'save' );
$this->registerTask( 'unpublish', 'publish' );
}
function edit()
{
global $option;
$row =& JTable::getInstance('review', 'Table');
$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
$id = $cid[0];
$row->load($id);
$lists = array();
$reservations = array(
'0' => array('value' => 'None Taken', 'text' => 'None Taken'),
'1' => array('value' => 'Accepted', 'text' => 'Accepted'),
'2' => array('value' => 'Suggested', 'text' => 'Suggested'),
'3' => array('value' => 'Required', 'text' => 'Required'),
);
$lists['reservations'] = JHTML::_('select.genericlist', $reservations, 'reservations', 'class="inputbox" '. '', 'value', 'text', $row->reservations );
$lists['smoking'] = JHTML::_('select.booleanlist', 'smoking', 'class="inputbox"', $row->smoking);
$lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $row->published);
HTML_reviews::editReview($row, $lists, $option);
}
function save()
{
global $option;
JRequest::checkToken() or die( 'Invalid Token' );
$row =& JTable::getInstance('review', 'Table');
if (!$row->bind(JRequest::get('post'))) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
exit();
}
$row->quicktake = JRequest::getVar( 'quicktake', '', 'post', 'string', JREQUEST_ALLOWRAW );
$row->review = JRequest::getVar( 'review', '', 'post', 'string', JREQUEST_ALLOWRAW );
$row->id = (int) $row->id;
$row->smoking = (int) $row->smoking;
$row->avg_dinner_price = (int) $row->avg_dinner_price;
$date =& JFactory::getDate($row->review_date);
$row->review_date = $date->toMySQL();
if (!$row->store()) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
exit();
}
switch ($this->_task)
{
case 'apply':
$msg = 'Changes to Review saved';
$link = 'index.php?option=' . $option . '&task=edit&cid[]='. $row->id;
break;
case 'save':
default:
$msg = 'Review Saved';
$link = 'index.php?option=' . $option;
break;
}
$this->setRedirect($link, $msg);
}
function showReviews()
{
global $option, $mainframe;
$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
$limitstart = JRequest::getVar('limitstart', 0);
$db =& JFactory::getDBO();
$query = "SELECT count(*) FROM #__reviews";
$db->setQuery( $query );
$total = $db->loadResult();
$query = "SELECT * FROM #__reviews";
$db->setQuery( $query, $limitstart, $limit );
$rows = $db->loadObjectList();
if ($db->getErrorNum()) {
echo $db->stderr();
return false;
}
jimport('joomla.html.pagination');
$pageNav = new JPagination($total, $limitstart, $limit);
HTML_reviews::showReviews( $option, $rows, $pageNav );
}
function remove()
{
global $option;
$cid = JRequest::getVar( 'cid', array(), '', 'array' );
$db =& JFactory::getDBO();
if(count($cid))
{
JArrayHelper::toInteger($cid);
$cids = implode( ',', $cid );
$query = "DELETE FROM #__reviews WHERE id IN ( $cids )";
$db->setQuery( $query );
if (!$db->query()) {
echo "<script> alert('".$db->getErrorMsg()."'); window. history.go(-1); </script>\n";
}
}
$this->setRedirect( 'index.php?option=' . $option );
}
function publish()
{
global $option;
$cid = JRequest::getVar( 'cid', array(), '', 'array' );
if( $this->_task == 'publish')
{
$publish = 1;
}
else
{
$publish = 0;
}
$reviewTable =& JTable::getInstance('review', 'Table');
$reviewTable->publish($cid, $publish);
$this->setRedirect( 'index.php?option=' . $option );
}
function comments()
{
global $option, $mainframe;
$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
$limitstart = JRequest::getVar('limitstart', 0);
$db =& JFactory::getDBO();
$query = "SELECT count(*) FROM #__reviews_comments";
$db->setQuery( $query );
$total = $db->loadResult();
$query = "SELECT c.*, r.name FROM #__reviews_comments AS c LEFT JOIN #__reviews AS r ON r.id = c.review_id ";
$db->setQuery( $query, $limitstart, $limit );
$rows = $db->loadObjectList();
if ($db->getErrorNum())
{
echo $db->stderr();
return false;
}
jimport('joomla.html.pagination');
$pageNav = new JPagination($total, $limitstart, $limit);
HTML_reviews::showComments( $option, $rows, $pageNav );
}
function editComment()
{
global $option;
$row =& JTable::getInstance('comment', 'Table');
$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
$id = $cid[0];
$row->load($id);
HTML_reviews::editComment($row, $option);
}
function saveComment()
{
global $option;
$row =& JTable::getInstance('comment', 'Table');
if (!$row->bind(JRequest::get('post'))) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
exit();
}
if (!$row->store()) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
exit();
}
$this->setRedirect('index.php?option=' . $option . '&task=comments', 'Comment changes saved');
}
function removeComment()
{
global $option;
$cid = JRequest::getVar( 'cid', array(), '', 'array' );
$db =& JFactory::getDBO();
if(count($cid))
{
JArrayHelper::toInteger($cid);
$cids = implode( ',', $cid );
$query = "DELETE FROM #__reviews_comments WHERE id IN ( $cids )";
$db->setQuery( $query );
if (!$db->query()) {
echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n";
}
}
$this->setRedirect( 'index.php?option=' . $option . '&task=comments' );
}
}
?>
vi ringrazio anticipatamente per le risposte