Skip to content

Instantly share code, notes, and snippets.

@ludofleury
Created December 18, 2010 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludofleury/746682 to your computer and use it in GitHub Desktop.
Save ludofleury/746682 to your computer and use it in GitHub Desktop.
The routing rules for the backend module file_relation
<?php
# Partial called by other admin-gen module like :
# include_partial('file_relation/ajax_list',array('object_type'=> 'product', 'object_id' => $product->getId(), 'file_type'=>'image'))
?>
<fieldset id="sf_fieldset_file_relation_<?php echo $file_type ?>">
<h2><?php echo ucfirst($file_type) ?></h2>
<div id="file_relation_<?php echo $file_type ?>" class="file_relation"></div>
</fieldset>
<script type="text/javascript">
<?php $sf_user->setAttribute('file_relation.filters', array('object_type'=>$object_type,
'object_id'=>array('text'=>$object_id),
'file_type'=>$file_type), 'admin_module'); ?>
$('#file_relation_<?php echo $file_type ?>').after('<ul class="sf_admin_actions"><li class="sf_admin_action_new"><a href="<?php echo url_for('@file_relation_add?file_type='.$file_type.'&object_type='.$object_type.'&object_id='.$object_id) ?>">Add a file</a></li></ul>')
$('#file_relation_<?php echo $file_type ?>').load('<?php echo url_for('@file_relation_list?file_type='.$file_type.'&object_type='.$object_type.'&object_id='.$object_id) ?>');
function position(tr,position)
{
var tbody = tr.parent();
var rows = $('tr',tbody).length;
var indicator = tr.find('td.sf_admin_list_td_position');
var oldPosition = Number(indicator.html());
if(position != oldPosition)
{
var ref = $('tr',tbody).eq(position-1);
//ref.css('background-color','green');
tr.css('background-color','green');
tr.remove();
// Promoted
if(position < oldPosition)
{
ref.before(tr);
}
// Demoted
else if(position > oldPosition)
{
ref.after(tr);
}
$('.sf_admin_list_td_position').each(function(index)
{
$(this).html(index+1);
})
tr.css('background-color','transparent');
}
}
$('.sf_admin_action_promote a').live('click',function(e)
{
e.preventDefault();
var link = $(this);
$.get(link.attr('href'),
function(data){ if(data.result=='success'){ position(link.parents('tr'),data.position) ;} notify(data.notice); },
'json');
});
$('.sf_admin_action_demote a').live('click',function(e)
{
e.preventDefault();
var link = $(this);
$.get(link.attr('href'),
function(data){ if(data.result=='success'){ position(link.parents('tr'),data.position) ;} notify(data.notice); },
'json');
});
$('.sf_admin_action_delete a').live('click',function(e)
{
e.preventDefault();
if (confirm("Are you sure?"))
{
var link = $(this);
$.post(link.attr('href'),
link.parent('form').serialize(),
function(data){ if(data.result=='success'){ remove(link.parents('tr'));} notify(data.notice); },
'json');
}
});
</script>
<?php
require_once dirname(__FILE__).'/../lib/file_relationGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/file_relationGeneratorHelper.class.php';
/**
* file_relation actions.
*
* @package shop
* @subpackage file_relation
* @author Ludovic Fleury <ludovic.fleury@w3brothers.com>
* @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
*/
class file_relationActions extends autoFile_relationActions
{
public function executeIndex(sfWebRequest $request)
{
$this->forward('default','disabled','Index action disabled');
}
public function executeList(sfWebRequest $request)
{
$this->forward404Unless($request->hasParameter('file_type')
&& $request->hasParameter('object_type')
&& $request->hasParameter('object_id'),'Missing parameters');
$this->getUser()->setAttribute('file_relation.filters', array('file_type'=>$request->getParameter('file_type'),
'object_type'=>$request->getParameter('object_type'),
'object_id'=>array('text'=>$request->getParameter('object_id'))), 'admin_module');
parent::executeIndex($request);
if($request->isXmlHttpRequest())
{
return $this->renderPartial('file_relation/list', array('pager' => $this->pager, 'sort' => $this->sort, 'helper' => $this->helper));
}
$this->setTemplate('index');
}
public function executeDelete(sfWebRequest $request)
{
$request->checkCSRFProtection();
$this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));
$deleted = $this->getRoute()->getObject()->delete();
if($request->isXmlHttpRequest())
{
$this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
$output = array("result" => $deleted ? "success" : "error",
"notice" => $deleted ? 'The item was deleted successfully' : 'An error occured');
return $this->renderText(json_encode($output));
}
if($deleted)
{
$this->getUser()->setFlash('notice', 'The item was deleted successfully.');
}
$this->redirect('@file_relation_list?object_type='.$request->getParameter('object_type').'&object_id='.$request->getParameter('object_id').'&file_type='.$request->getParameter('file_type'));
}
public function executePromote(sfWebRequest $request)
{
$object = $this->getRoute()->getObject();
$object->promote();
if($request->isXmlHttpRequest())
{
$this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
$output = array("result" => 'success',
"notice" => 'The item was promoted successfully',
"position" => $object->getPosition());
return $this->renderText(json_encode($output));
}
$this->redirect('@file_relation_list?object_type='.$request->getParameter('object_type').'&object_id='.$request->getParameter('object_id').'&file_type='.$request->getParameter('file_type'));
}
public function executeDemote(sfWebRequest $request)
{
$object = $this->getRoute()->getObject();
$object->demote();
if($request->isXmlHttpRequest())
{
$this->getResponse()->setHttpHeader('Content-Type','application/json; charset=utf-8');
$output = array("result" => 'success',
"notice" => 'The item was demoted successfully',
"position" => $object->getPosition());
return $this->renderText(json_encode($output));
}
$this->redirect('@file_relation_list?object_type='.$request->getParameter('object_type').'&object_id='.$request->getParameter('object_id').'&file_type='.$request->getParameter('file_type'));
}
public function executeAdd(sfWebRequest $request)
{
if($request->hasParameter('object_type') || $request->hasParameter('object_id'))
{
$this->forward404Unless(($request->hasParameter('object_type') && $request->hasParameter('object_id')), 'You must specify Type and Identifier for the related object');
$this->forward404Unless(Doctrine::getTable($request->getParameter('object_type'))->findOneBy('id',$request->getParameter('object_id')),'Unknown related object');
}
$this->forward($request->getParameter('file_type'),$request->isMethod('post') ? 'create' : 'new');
}
public function executeEdit(sfWebRequest $request)
{
if($request->hasParameter('object_type') || $request->hasParameter('object_id'))
{
$this->forward404Unless(($request->hasParameter('object_type') && $request->hasParameter('object_id')), 'You must specify Type and Identifier for the related object');
$this->forward404Unless(Doctrine::getTable($request->getParameter('object_type'))->findOneBy('id',$request->getParameter('object_id')),'Unknown related object');
}
$request->setParameter('id',$request->getParameter('file_id'));
$this->forward($request->getParameter('file_type'),$request->isMethod('post') ? 'update' : 'edit');
}
}
<?php
/**
* file_relation module helper.
*
* @package shop
* @subpackage file_relation
* @author Ludovic Fleury <ludovic.fleury@w3brothers.com>
* @version SVN: $Id: helper.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
*/
class file_relationGeneratorHelper extends BaseFile_relationGeneratorHelper
{
/**
* Returns url rule
* Support custom List rule
*
* @param string $action
* @return string
*/
public function getUrlForAction($action)
{
return 'file_relation_'.$action;
}
/**
* Render delete link tag
* compliant with jQuery ajax support.
*
* @param Doctrine_record $object
* @param array $params
* @param boolean $ajax
* @return string
*/
public function linkToDelete($object, $params, $request = null)
{
if ($object->isNew())
{
return '';
}
$url = url_for($this->getUrlForAction('delete') ,array('id' =>$object->getId(),
'object_type' => $request->getParameter('object_type'),
'object_id' => $request->getParameter('object_id'),
'file_type' => $request->getParameter('file_type')));
if($request->isXmlHttpRequest())
{
$tag = '<li class="sf_admin_action_delete">';
$tag .= '<form action="'.$url.'" method="post" style="display:inline">';
$tag .= '<input type="hidden" name="sf_method" value="delete" />';
// CSRF protection
$form = new BaseForm();
if ($form->isCSRFProtected())
{
$tag .= sprintf('<input type="hidden" name="%s" value="%s" />',$form->getCSRFFieldName(), $form->getCSRFToken());
}
$tag .= '<a href="'.$url.'">'.$params['label'].'</a>';
$tag .= '</form>';
$tag .= '</li>';
}
else
{
$tag = '<li class="sf_admin_action_delete">'.link_to(__($params['label'], array(), 'sf_admin'), $this->getUrlForAction('delete'),
array('id' =>$object->getId(),
'object_type' => $request->getParameter('object_type'),
'object_id' => $request->getParameter('object_id'),
'file_type' => $request->getParameter('file_type')),
array('method' => 'delete', 'confirm' => !empty($params['confirm']) ? __($params['confirm'], array(), 'sf_admin') : $params['confirm'])).'</li>';
}
return $tag;
}
/**
* Render Promote link tag
*
* @param Doctrine_record $object
* @param array $params
* @return string
*/
public function linkToPromote($object, $params, $request)
{
return '<li class="sf_admin_action_'.$params['class_suffix'].'"><a href="'.url_for($this->getUrlForAction('promote'),array('id' =>$object->getId(),
'object_type' => $request->getParameter('object_type'),
'object_id' => $request->getParameter('object_id'),
'file_type' => $request->getParameter('file_type'), )).'">'.$params['label'].'</a></li>';
}
/**
* Render Promote link tag
*
* @param Doctrine_record $object
* @param array $params
* @return string
*/
public function linkToDemote($object, $params, $request)
{
return '<li class="sf_admin_action_'.$params['class_suffix'].'"><a href="'.url_for($this->getUrlForAction('demote'),array('id' =>$object->getId(),
'object_type' => $request->getParameter('object_type'),
'object_id' => $request->getParameter('object_id'),
'file_type' => $request->getParameter('file_type'), )).'">'.$params['label'].'</a></li>';
}
}
file_relation:
class: sfDoctrineRouteCollection
options:
model: FileRelation
module: file_relation
prefix_path: /file
column: id
with_wildcard_routes: true
actions: [list,new,delete]
file_relation_list:
url: /files/:object_type/:object_id/:file_type.:sf_format
params:
module: file_relation
action: list
sf_format: html
file_relation_add:
url: /file/add/:object_type/:object_id/:file_type.:sf_format
param:
module: file_relation
action: add
sf_format: html
file_relation_edit:
url: /file/edit/:object_type/:object_id/:file_type/:file_id.:sf_format
param:
module: file_relation
action: edit
sf_format: html
file_relation_promote:
url: /file/:id/promote.:sf_format
param:
module: file_relation
action: promote
sf_format: html
file_relation_demote:
url: /file/:id/demote.:sf_format
param:
module: file_relation
action: demote
sf_format: html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment