Skip to content

Instantly share code, notes, and snippets.

@vaclavbohac
Created August 31, 2010 12:17
Show Gist options
  • Select an option

  • Save vaclavbohac/558944 to your computer and use it in GitHub Desktop.

Select an option

Save vaclavbohac/558944 to your computer and use it in GitHub Desktop.
Primitive Nette Panel for installing sample data
<h1>Example data</h1>
<style>
#example-data {
list-style: none;
padding-left: 5px;
}
</style>
<ul id="example-data">
<li>
<?php echo (string) $clear; ?>
</li>
<li>
<?php echo (string) $install; ?>
</li>
</ul>
<?php
class ExampleDataPanel extends Object implements IDebugPanel
{
protected static $registered;
public function getId()
{
return __CLASS__;
}
public function getPanel()
{
$presenter = Environment::getApplication()->getPresenter();
$clear = Html::el('a')
->href($presenter->link(':ExampleData:clear'))
->setText('Clear data');
$install = Html::el('a')
->href($presenter->link(':ExampleData:install'))
->setText('Install new data');
ob_start();
require_once __DIR__ . '/example.panel.phtml';
return ob_get_clean();
}
public function getTab()
{
return (string) Html::el('span')->setText(__CLASS__);
}
public static function register()
{
if (self::$registered) {
throw new LocicalException("Example data panel is already registered");
}
Debug::addPanel(new static());
self::$registered = TRUE;
}
}
<?php
class ExampleDataPresenter extends Presenter
{
private $tables = array (
'pt_cars' => array (
array (
'id' => 1,
'car_species_id' => 1,
'specification' => 'Tristranny sklapec',
'car_manufacturer_id' => 1,
'car_type_id' => 1,
'year' => 1990,
'kilometers' => 50000,
'price' => 16000,
'currency_id' => 2,
),
array (
'id' => 2,
'car_species_id' => 1,
'specification' => 'Tahac',
'car_manufacturer_id' => 2,
'car_type_id' => 2,
'year' => 2001,
'kilometers' => 150000,
'price' => 200000,
'currency_id' => 1,
),
),
'pt_car_manufacturers' => array (
array (
'id' => 1,
'name' => 'TATRA',
'permalink' => 'tatra',
'numVehicles' => 1,
),
array (
'id' => 2,
'name' => 'MAN',
'permalink' => 'man',
'numVehicles' => 1,
),
array (
'id' => 3,
'name' => 'IVECO',
'permalink' => 'iveco',
'numVehicles' => 1,
),
),
'pt_car_types' => array (
array (
'id' => 1,
'name' => 'T 815',
'permalink' => 't-815',
'car_manufacturer_id' => 1
),
array (
'id' => 2,
'name' => '360E 6x6',
'permalink' => '360e-6x6',
'car_manufacturer_id' => 2,
),
),
'pt_car_species' => array (
'id' => 1,
'name' => 'Nákladní vozidla',
),
'pt_currencies' => array (
array (
'id' => 1,
'name' => 'CZK',
'tag' => 'Kč',
),
array (
'id' => 2,
'name' => 'EURO',
'tag' => '&euro;',
)
),
'pt_images' => array (
'id' => 1,
'src' => 'images/tatra/',
'name' => 'tatra-min.jpg',
'alt' => 'TATRA zepredu',
'main' => 1,
),
'pt_news' => array (
'id' => 1,
'author' => 1,
'headline' => 'Lorem ipsum',
'permalink' => 'lorem-ipsum',
'body' => 'Lorem ipsum dolor sit amet consecteuer...',
'published' => 0,
'updated' => '2010-08-31',
'created' => '2010-08-31',
),
'pt_user_roles' => array (
'id' => 1,
'name' => 'admin',
),
'pt_users' => array (
'id' => 1,
'username' => 'wopice',
'password' => 'nove_heslo',
'user_role_id' => 1,
'last_logged' => '2010-08-31',
'added' => '2010-08-31',
),
);
public function startup()
{
parent::startup();
if (Environment::isProduction()) {
throw new ForbiddenRequestException('Example presenter cannot be accesible on production.');
}
}
public function actionClear()
{
foreach ($this->tables as $table => $data) {
dibi::query("DELETE FROM $table");
}
$source = '<script>history.go(-1)</script>';
$this->terminate(new RenderResponse($source));
}
public function actionInstall()
{
foreach ($this->tables as $table => $data) {
$this->save($table, $data);
}
$source = '<script>history.go(-1)</script>';
$this->terminate(new RenderResponse($source));
}
public function save($table, array $data)
{
$test = $data;
if (is_scalar(array_pop($test))) {
dibi::query("INSERT INTO $table", $data);
}
else {
foreach ($data as $row) {
$this->save($table, $row);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment