A co myślicie o takim kodzie?
Jest to wycinek modulu panelu adminitracyjnego, ktory zwraca kod HTML do szablonu. Layer do bazy obrabia odpowiednio dane w zaleznosci od typu.
Zależy mi na porównaniu dwóch tych wycinków (news powyżej i tego poniżej)
pod względem jakości kodu.
<?php
class system_settings
{
private $lastSub='';
public function run($action)
{
$html='';
switch ($action)
{
default:
case 'groups':
$html=$this->groups();
break;
case 'newgroup':
$html=$this->newGroup();
break;
case 'delete':
$html=$this->delete();
break;
}
return $html;
}
public function getName()
{
return "System settings";
}
public function getSubName()
{
return $this->lastSub;
}
private function newGroup()
{
$request=httpContext::instance()->request;
if($request->contains('group_name','group_key','group_description'))
{
$group_name=$request->map(POST,'group_name','',new stringCheck(3));
$group_key=$request->map(POST,'group_key','alphanumeric',new stringCheck(3));
$group_description=$request->map(POST,'group_description','string');
if($request->noErrors())
{
$db=db::instance();
$db->insert('settings_groups',array('gkey'=>$group_key,'name'=>$group_name,'description'=>$group_description));
if($db->affectedRows())
{
acpMessages::add(M_INFO,'New group has been successfully added.');
return $this->groups();
}
}
}
$form=new acpForm('Add new settings group');
$form->add(new textField('Group name*','group_name',''));
$form->add(new textarea('Group description','group_description',''));
$form->add(new textField('Group key*','group_key','','Needed when you want to access variable from specified group'));
$this->lastSub="New group";
return $form->fetch();
}
private function groups()
{
$db=db::instance();
$groups=$db->getIterator("SELECT * FROM settings_groups");
$table=new acpTable('Groups',$groups);
$table->setActions('system/settings','sg_id',array('edit.gif'=>'edit/%ID%','delete.gif'=>'delete/%ID%'));
$table->setFields('name','description');
$table->setHeader('Name','Description',' ');
$this->lastSub="Groups";
return $table->fetch();
}
private function delete()
{
$id=httpContext::instance()->request->map(GET,'3','int');
if($id>0)
{
$db=db::instance();
$count=$db->count('settings',array('sg'=>$id));
if($count>0)
{
acpMessages::add(M_WARNING,'Requested group must be empty before removing');
}else
{
if($db->delete('settings_groups','sg_id='.$id))
{
acpMessages::add(M_INFO,'Group has been successfully removed.');
}else
{
acpMessages::add(M_ERROR,'Failed to remove specified group.');
}
}
}
return $this->groups();
}
}
?>