creating an new row for edit/delete links in the datatable

working on creating/editing records.
This commit is contained in:
Naomi 2013-07-23 18:01:43 -04:00
parent 69afc715d1
commit ecb2f81211
5 changed files with 215 additions and 72 deletions

View File

@ -11,6 +11,7 @@ class PlayouthistoryController extends Zend_Controller_Action
->addActionContext('edit-aggregate-item', 'json')
->addActionContext('create-list-item', 'json')
->addActionContext('edit-list-item', 'json')
->addActionContext('delete-list-item', 'json')
->addActionContext('update-list-item', 'json')
->addActionContext('update-aggregate-item', 'json')
->addActionContext('create-template', 'json')
@ -137,10 +138,10 @@ class PlayouthistoryController extends Zend_Controller_Action
public function editListItemAction()
{
$file_id = $this->_getParam('id');
$history_id = $this->_getParam('id', null);
$historyService = new Application_Service_HistoryService();
$form = $historyService->makeHistoryItemForm($file_id);
$form = $historyService->makeHistoryItemForm($history_id);
$this->view->form = $form;
$this->view->dialog = $this->view->render('form/edit-history-item.phtml');
@ -148,6 +149,14 @@ class PlayouthistoryController extends Zend_Controller_Action
unset($this->view->form);
}
public function deleteListItemAction()
{
$history_id = $this->_getParam('id');
$historyService = new Application_Service_HistoryService();
$historyService->deletePlayedItem($history_id);
}
public function updateListItemAction()
{
$request = $this->getRequest();

View File

@ -21,7 +21,7 @@ class Application_Service_HistoryService
public function __construct()
{
$this->con = isset($con) ? $con : Propel::getConnection(CcPlayoutHistoryPeer::DATABASE_NAME);
$this->timezone = date_default_timezone_get();
$this->timezone = Application_Model_Preference::GetTimezone();
}
/*
@ -61,6 +61,21 @@ class Application_Service_HistoryService
left join cc_files as file on (file.id = playout.file_id)";
$results = Application_Model_Datatables::findEntries($this->con, $select, $historyTable, $opts, "history");
$timezoneUTC = new DateTimeZone("UTC");
$timezoneLocal = new DateTimeZone($this->timezone);
//need to display the results in the station's timezone.
foreach ($results["history"] as $index => &$result) {
$dateTime = new DateTime($result["starts"], $timezoneUTC);
$dateTime->setTimezone($timezoneLocal);
$result["starts"] = $dateTime->format("Y-m-d H:i:s");
$dateTime = new DateTime($result["ends"], $timezoneUTC);
$dateTime->setTimezone($timezoneLocal);
$result["ends"] = $dateTime->format("Y-m-d H:i:s");
}
return $results;
}
@ -153,7 +168,7 @@ class Application_Service_HistoryService
}
/* id is an id in cc_playout_history */
public function makeHistoryItemForm() {
public function makeHistoryItemForm($id) {
try {
$form = new Application_Form_EditHistoryItem();
@ -197,18 +212,23 @@ class Application_Service_HistoryService
public function createPlayedItem($data) {
try {
$form = $this->makeHistoryItemForm();
$form = $this->makeHistoryItemForm(null);
$history_id = $form->getElement("his_item_id");
$json = $form->processAjax($data);
Logging::info($json);
if ($form->isValid($data)) {
$values = $form->getElementsAndSubFormsOrdered();
$history_id->setIgnore(true);
$values = $form->getValues();
Logging::info("created list item");
Logging::info($values);
}
else {
Logging::info("created list item NOT VALID");
}
Logging::info($form->getMessages());
return $json;
//return $json;
}
catch (Exception $e) {
Logging::info($e);
@ -216,21 +236,25 @@ class Application_Service_HistoryService
}
/* id is an id in cc_playout_history */
public function editPlayedItem($id) {
public function editPlayedItem($data) {
try {
$form = $this->makeHistoryItemForm();
$id = $data["his_item_id"];
$form = $this->makeHistoryItemForm($id);
$history_id = $form->getElement("his_item_id");
$history_id->setRequired(true);
$json = $form->processAjax($data);
Logging::info($json);
if ($form->isValid($data)) {
$values = $form->getElementsAndSubFormsOrdered();
Logging::info("edited list item");
Logging::info($values);
}
return $json;
$values = $form->getValues();
Logging::info("edited list item");
Logging::info($values);
}
else {
Logging::info("edited list item NOT VALID");
}
Logging::info($form->getMessages());
}
catch (Exception $e) {
Logging::info($e);
@ -240,29 +264,59 @@ class Application_Service_HistoryService
/* id is an id in cc_files */
public function editPlayedFile($data) {
$form = new Application_Form_EditHistoryFile();
$json = $form->processAjax($data);
Logging::info($json);
if ($form->isValid($data)) {
$id = $data["his_file_id"];
$file = Application_Model_StoredFile::RecallById($id, $this->con);
$md = array(
MDATA_KEY_TITLE => $data['his_file_title'],
MDATA_KEY_CREATOR => $data['his_file_creator'],
MDATA_KEY_COMPOSER => $data['his_file_composer'],
MDATA_KEY_COPYRIGHT => $data['his_file_copyright']
);
$file->setDbColMetadata($md);
}
$this->con->beginTransaction();
try {
$form = new Application_Form_EditHistoryFile();
$json = $form->processAjax($data);
Logging::info($json);
if ($form->isValid($data)) {
$id = $data["his_file_id"];
$file = Application_Model_StoredFile::RecallById($id, $this->con);
$md = array(
MDATA_KEY_TITLE => $data['his_file_title'],
MDATA_KEY_CREATOR => $data['his_file_creator'],
MDATA_KEY_COMPOSER => $data['his_file_composer'],
MDATA_KEY_COPYRIGHT => $data['his_file_copyright']
);
$file->setDbColMetadata($md);
}
$this->con->commit();
}
catch (Exception $e) {
$this->con->rollback();
Logging::info($e);
throw $e;
}
return $json;
}
/* id is an id in cc_playout_history */
public function deletePlayedItem($id) {
$this->con->beginTransaction();
try {
$record = CcPlayoutHistoryQuery::create()->findPk($id, $this->con);
$record->delete($this->con);
$this->con->commit();
}
catch (Exception $e) {
$this->con->rollback();
Logging::info($e);
throw $e;
}
}
//---------------- Following code is for History Templates --------------------------//

View File

@ -7,6 +7,7 @@
<li><a href="#his-tabs-2"><?php echo _("Aggregate"); ?></a></li>
</ul>
<div id="his-tabs-1">
<div id="his_create"><?php echo _("Create Entry"); ?></div>
<table id="history_table_list" cellpadding="0" cellspacing="0" class="datatable"></table>
</div>
<div id="his-tabs-2">

View File

@ -0,0 +1,3 @@
<div id="history_template" class="ui-widget ui-widget-content block-shadow alpha-block padded">
</div>

View File

@ -35,9 +35,9 @@ var AIRTIME = (function(AIRTIME) {
]
};
var lengthMenu = [[50, 100, 500, -1], [50, 100, 500, $.i18n._("All")]];
var lengthMenu = [[10, 25, 50, 100, 500, -1], [10, 25, 50, 100, 500, $.i18n._("All")]];
var sDom = 'lf<"dt-process-rel"r><"H"T><"dataTables_scrolling"t><"F"ip>';
var sDom = 'l<"dt-process-rel"r><"H"T><"dataTables_scrolling"t><"F"ip>';
function getFileName(ext){
var filename = $("#his_date_start").val()+"_"+$("#his_time_start").val()+"m--"+$("#his_date_end").val()+"_"+$("#his_time_end").val()+"m";
@ -98,13 +98,9 @@ var AIRTIME = (function(AIRTIME) {
fnRowCallback;
fnRowCallback = function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var url = baseUrl+"Playouthistory/edit-aggregate-item/format/json/id/"+aData.file_id,
$link = $("<a/>", {
"href": url,
"text": $.i18n._("Edit")
});
$('td.his_edit', nRow).html($link);
var editUrl = baseUrl+"Playouthistory/edit-aggregate-item/format/json/id/"+aData.file_id;
nRow.setAttribute('url-edit', editUrl);
};
oTable = $historyTableDiv.dataTable( {
@ -115,8 +111,7 @@ var AIRTIME = (function(AIRTIME) {
{"sTitle": $.i18n._("Played"), "mDataProp": "played", "sClass": "his_artist"}, /* times played */
{"sTitle": $.i18n._("Length"), "mDataProp": "length", "sClass": "his_length library_length"}, /* Length */
{"sTitle": $.i18n._("Composer"), "mDataProp": "composer", "sClass": "his_composer"}, /* Composer */
{"sTitle": $.i18n._("Copyright"), "mDataProp": "copyright", "sClass": "his_copyright"}, /* Copyright */
{"sTitle" : $.i18n._("Admin"), "mDataProp": "file_id", "bSearchable" : false, "sClass": "his_edit"}, /* id of history item */
{"sTitle": $.i18n._("Copyright"), "mDataProp": "copyright", "sClass": "his_copyright"} /* Copyright */
],
"bProcessing": true,
@ -145,13 +140,11 @@ var AIRTIME = (function(AIRTIME) {
fnRowCallback;
fnRowCallback = function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var url = baseUrl+"playouthistory/edit-list-item/format/json/id/"+aData.history_id,
$link = $("<a/>", {
"href": url,
"text": $.i18n._("Edit")
});
$('td.his_edit', nRow).html($link);
var editUrl = baseUrl+"playouthistory/edit-list-item/format/json/id/"+aData.history_id,
deleteUrl = baseUrl+"playouthistory/delete-list-item/format/json/id/"+aData.history_id;
nRow.setAttribute('url-edit', editUrl);
nRow.setAttribute('url-delete', deleteUrl);
};
oTable = $historyTableDiv.dataTable( {
@ -160,8 +153,7 @@ var AIRTIME = (function(AIRTIME) {
{"sTitle": $.i18n._("Start"), "mDataProp": "starts", "sClass": "his_starts"}, /* Starts */
{"sTitle": $.i18n._("End"), "mDataProp": "ends", "sClass": "his_ends"}, /* Ends */
{"sTitle": $.i18n._("Title"), "mDataProp": "title", "sClass": "his_title"}, /* Title */
{"sTitle": $.i18n._("Creator"), "mDataProp": "artist", "sClass": "his_artist"}, /* Creator */
{"sTitle" : $.i18n._("Admin"), "mDataProp": "history_id", "bSearchable" : false, "sClass": "his_edit"}, /* id of history item */
{"sTitle": $.i18n._("Creator"), "mDataProp": "artist", "sClass": "his_artist"} /* Creator */
],
"bProcessing": true,
@ -191,7 +183,8 @@ var AIRTIME = (function(AIRTIME) {
screenWidth = Math.floor(viewport.width - 110),
oBaseDatePickerSettings,
oBaseTimePickerSettings,
oTable,
oTableAgg,
oTableItem,
dateStartId = "#his_date_start",
timeStartId = "#his_time_start",
dateEndId = "#his_date_end",
@ -251,15 +244,72 @@ var AIRTIME = (function(AIRTIME) {
minuteText: $.i18n._("Minute")
};
oTable = aggregateHistoryTable();
itemHistoryTable();
oTableItem = itemHistoryTable();
oTableAgg = aggregateHistoryTable();
$historyContentDiv.find(dateStartId).datepicker(oBaseDatePickerSettings);
$historyContentDiv.find(timeStartId).timepicker(oBaseTimePickerSettings);
$historyContentDiv.find(dateEndId).datepicker(oBaseDatePickerSettings);
$historyContentDiv.find(timeEndId).timepicker(oBaseTimePickerSettings);
$historyContentDiv.on("click", "td.his_edit", function(e) {
// 'open' an information row when a row is clicked on
//for create/edit/delete
function openRow(oTable, tr) {
var links = ['url-edit', 'url-delete'],
i, len,
attr,
name,
$link,
$div;
$div = $("<div/>");
for (i = 0, len = links.length; i < len; i++) {
attr = links[i];
if (tr.hasAttribute(attr)) {
name = attr.split("-")[1];
$link = $("<a/>", {
"href": tr.getAttribute(attr),
"text": $.i18n._(name),
"class": "his_"+name
});
$div.append($link);
}
}
if (oTable.fnIsOpen(tr)) {
oTable.fnClose(tr);
}
else {
oTable.fnOpen(tr, $div, "his_update");
}
}
$historyContentDiv.on("click", "#history_table_list tr", function(ev) {
openRow(oTableItem, this);
});
$historyContentDiv.on("click", "#history_table_aggregate tr", function(ev) {
openRow(oTableAgg, this);
});
$("#his_create").click(function(e) {
var url = baseUrl+"playouthistory/edit-list-item/format/json" ;
e.preventDefault();
$.get(url, function(json) {
makeHistoryDialog(json.dialog);
}, "json");
});
$historyContentDiv.on("click", "a.his_edit", function(e) {
var url = e.target.href;
e.preventDefault();
@ -271,6 +321,27 @@ var AIRTIME = (function(AIRTIME) {
}, "json");
});
$historyContentDiv.on("click", "a.his_delete", function(e) {
var url = e.target.href,
doDelete;
e.preventDefault();
doDelete = confirm($.i18n._("Delete this history record?"));
if (doDelete) {
$.post(url, function(json) {
oTableAgg.fnDraw();
oTableItem.fnDraw();
}, "json");
}
});
$('body').on("click", ".his_file_cancel, .his_item_cancel", function(e) {
removeHistoryDialog();
});
$('body').on("click", ".his_file_save", function(e) {
e.preventDefault();
@ -288,7 +359,7 @@ var AIRTIME = (function(AIRTIME) {
}
else {
removeHistoryDialog();
oTable.fnDraw();
oTableAgg.fnDraw();
}
}, "json");
@ -299,11 +370,15 @@ var AIRTIME = (function(AIRTIME) {
e.preventDefault();
var $form = $(this).parents("form");
var data = $form.serializeArray();
var url = baseUrl+"Playouthistory/update-list-item/format/json";
var $form = $(this).parents("form"),
data = $form.serializeArray(),
id = data[0].value,
createUrl = baseUrl+"Playouthistory/create-list-item/format/json",
updateUrl = baseUrl+"Playouthistory/update-list-item/format/json",
url;
url = (id === "") ? createUrl : updateUrl;
$.post(url, data, function(json) {
//TODO put errors on form.
@ -312,7 +387,7 @@ var AIRTIME = (function(AIRTIME) {
}
else {
removeHistoryDialog();
oTable.fnDraw();
oTableItem.fnDraw();
}
}, "json");
@ -325,11 +400,12 @@ var AIRTIME = (function(AIRTIME) {
oRange = AIRTIME.utilities.fnGetScheduleRange(dateStartId, timeStartId, dateEndId, timeEndId);
fn = oTable.fnSettings().fnServerData;
fn = fnServerData;
fn.start = oRange.start;
fn.end = oRange.end;
oTable.fnDraw();
oTableAgg.fnDraw();
oTableItem.fnDraw();
});
$historyContentDiv.find("#his-tabs").tabs();