Skip to content

Instantly share code, notes, and snippets.

@cole007
Last active March 16, 2017 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cole007/bfd550edf6a0dd540afeb48442fc2165 to your computer and use it in GitHub Desktop.
Save cole007/bfd550edf6a0dd540afeb48442fc2165 to your computer and use it in GitHub Desktop.
<?php
namespace Craft;
class MysteryShopper_ReportRecord extends BaseRecord
{
public function getTableName()
{
return 'mysteryshopper_Report';
}
protected function defineAttributes()
{
return array(
'report' => array(AttributeType::String, 'default' => '', 'column' => ColumnType::Text),
'stage' => array(AttributeType::Number),
'author' => array(AttributeType::Number),
'shop' => array(AttributeType::Number),
'instance' => array(AttributeType::Number),
'event' => array(AttributeType::Number),
'status' => array(AttributeType::String, 'default' => 'pending'),
);
}
/**
* @return array
*/
public function defineRelations()
{
return array(
);
}
}
<?php
namespace Craft;
class MysteryShopper_ReportService extends BaseApplicationComponent
{
public function saveReport(MysteryShopper_ReportModel $model,$id = null)
{
$pre = $model->report;
$id = (int)$id;
if ($id > 0) {
$tmp = [];
$record = MysteryShopper_ReportRecord::model()->findById($id);
$fields = json_decode($record->report);
// merge fields - new values take precedence
$merge = (object) array_merge((array) $fields, (array) json_decode($model->report));
$model->report = json_encode($merge);
$record->setAttribute('report',json_encode($merge));
} else {
$record = new MysteryShopper_ReportRecord;
}
foreach ($model AS $key => $value)
{
if ($value) $record->setAttribute($key,$value);
}
if ($record->save(false)) {
MysteryShopperPlugin::Log('Report saved via service');
} else {
MysteryShopperPlugin::Log('Report failed to save via service');
}
return $record->id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment