Ok so I was able to get things almost working except for this small thing - when editing a note that exists, it reads the created_on date just fine but it doesn't save it if I change it to something else.
Hopefully you could find the fix for it as I wasn't able to.
The code looks like this now:
function edit_note():
public function edit_note($id=null) {
if(!$this->is_back()) {
$form = & $this->init_module('Utils/FileUpload',array(false));
if(isset($id))
$form->addElement('header', 'upload', $this->t('Edit note').': '.$this->add_header);
else
$form->addElement('header', 'upload', $this->t('Attach note').': '.$this->add_header);
$fck = $form->addElement('fckeditor', 'note', $this->t('Note'));
$fck->setFCKProps('800','300');
$form->set_upload_button_caption('Save');
if($form->getSubmitValue('note')=='' && !$form->is_file())
$form->addRule('note',$this->t('Please enter note or choose file'),'required');
$form->addElement('select','permission',$this->t('Permission'),array($this->ht('Public'),$this->ht('Protected'),$this->ht('Private')));
$form->addElement('checkbox','sticky',$this->t('Sticky'));
// added by Liran Tal
$form->addElement('date','created_on',$this->t('Date'),array('format'=>'Y-m-d H:i'));
$form->addRule('created_on',$this->t('Field required'),'required');
// end
if(isset($id))
$form->addElement('header',null,$this->t('Replace attachment with file'));
$form->add_upload_element();
if($this->max_file_size)
$form->set_max_file_size($this->max_file_size);
$form->setDefaults(array('created_on'=>time()));
if(isset($id)) {
/* replaced:
$row = DB::GetRow('SELECT l.sticky,x.text,l.permission FROM utils_attachment_note x INNER JOIN utils_attachment_link l ON l.id=x.attach_id WHERE x.attach_id=%d AND x.revision=(SELECT max(z.revision) FROM utils_attachment_note z WHERE z.attach_id=%d)',array($id,$id));
$form->setDefaults(array('note'=>$row['text'],'permission'=>$row['permission'],'sticky'=>$row['sticky']));
with: */
$row = DB::GetRow('SELECT l.sticky,x.text,l.permission,x.created_on FROM utils_attachment_note x INNER JOIN utils_attachment_link l ON l.id=x.attach_id WHERE x.attach_id=%d AND x.revision=(SELECT max(z.revision) FROM utils_attachment_note z WHERE z.attach_id=%d)',array($id,$id));
$form->setDefaults(array('note'=>$row['text'],'permission'=>$row['permission'],'sticky'=>$row['sticky'],'created_on'=>$row['created_on']));
//$form->setDefaults(array('created_on'=>time()));
}
if(!$this->inline) {
Base_ActionBarCommon::add('save','Save',$form->get_submit_form_href());
Base_ActionBarCommon::add('back','Back',$this->create_back_href());
} else {
$s = HTML_QuickForm::createElement('button',null,$this->t('Save'),$form->get_submit_form_href());
$c = HTML_QuickForm::createElement('button',null,$this->t('Cancel'),$this->create_back_href());
$form->addGroup(array($s,$c));
}
$this->ret_attach = true;
if(isset($id))
$this->display_module($form, array( array($this,'submit_edit'),$id,$row['text']));
else
$this->display_module($form, array( array($this,'submit_attach') ));
} else {
$this->ret_attach = false;
}
$this->caption = 'Edit note';
if($this->inline)
return $this->ret_attach;
elseif(!$this->ret_attach)
return $this->pop_box0();
}
function submit_attach():
public function submit_attach($file,$oryg,$data) {
DB::Execute('INSERT INTO utils_attachment_link(local,permission,permission_by,sticky,func,args) VALUES(%s,%d,%d,%b,%s,%s)',array($this->group,$data['permission'],Acl::get_user(),isset($data['sticky']) && $data['sticky'],serialize($this->func),serialize($this->args)));
$id = DB::Insert_ID('utils_attachment_link','id');
if($file && trim($data['note'])=='')
$data['note'] = $oryg;
/* replaced
DB::Execute('INSERT INTO utils_attachment_file(attach_id,original,created_by,revision) VALUES(%d,%s,%d,0)',array($id,$oryg,Acl::get_user()));
DB::Execute('INSERT INTO utils_attachment_note(attach_id,text,created_by,revision) VALUES(%d,%s,%d,0)',array($id,Utils_BBCodeCommon::optimize($data['note']),Acl::get_user()));
with: */
/*
$fh = fopen('/tmp/out.test', 'a+');
fwrite($fh, print_r($data, true));
fclose($fh);
*/
if (isset($data['created_on']['Y']))
$created_on_date = $data['created_on']['Y'].'-'.$data['created_on']['m'].'-'.$data['created_on']['d'].' '.$data['created_on']['H'].':'.$data['created_on']['i'].':00';
else
$created_on_date = date('Y-m-d H:i:s');
DB::Execute('INSERT INTO utils_attachment_file(attach_id,original,created_by,revision,created_on) VALUES(%d,%s,%d,0,%s)',array($id,$oryg,Acl::get_user(),$created_on_date));
DB::Execute('INSERT INTO utils_attachment_note(attach_id,text,created_by,revision,created_on) VALUES(%d,%s,%d,0,%s)',array($id,Utils_BBCodeCommon::optimize($data['note']),Acl::get_user(),$created_on_date));
if($file) {
$local = $this->get_data_dir().$this->group;
@mkdir($local,0777,true);
$dest_file = $local.'/'.$id.'_0';
rename($file,$dest_file);
if ($this->add_func) call_user_func($this->add_func,$id,0,$dest_file,$oryg,$this->add_args);
}
$this->ret_attach = false;
if (isset($this->watchdog_category)) Utils_WatchdogCommon::new_event($this->watchdog_category,$this->watchdog_id,'N_+_'.$id);
}
function submit_edit()
public function submit_edit($file,$oryg,$data,$id,$text) {
DB::Execute('UPDATE utils_attachment_link SET sticky=%b,permission=%d,permission_by=%d WHERE id=%d',array(isset($data['sticky']) && $data['sticky'],$data['permission'],Acl::get_user(),$id));
$fh = fopen('/tmp/out.test', 'w');
fwrite($fh, print_r($data, true));
fclose($fh);
$created_on_date = '';
/* added by Liran Tal */
if (isset($data['created_on']['Y']))
$created_on_date = $data['created_on']['Y'].'-'.$data['created_on']['m'].'-'.$data['created_on']['d'].' '.$data['created_on']['H'].':'.$data['created_on']['i'].':00';
else
$created_on_date = date('Y-m-d H:i:s');
/* end */
error_log($created_on_date);
if($data['note']!=$text) {
if($file && trim($data['note'])=='')
$data['note'] = $oryg;
DB::StartTrans();
$rev = DB::GetOne('SELECT max(x.revision) FROM utils_attachment_note x WHERE x.attach_id=%d',array($id));
DB::Execute('INSERT INTO utils_attachment_note(text,attach_id,revision,created_by,created_on) VALUES (%s,%d,%d,%d,%s)',array(Utils_BBCodeCommon::optimize($data['note']),$id,$rev+1,Acl::get_user(),$created_on_date));
DB::CompleteTrans();
}
if($file) {
DB::StartTrans();
$rev = DB::GetOne('SELECT max(x.revision) FROM utils_attachment_file x WHERE x.attach_id=%d',array($id));
$rev = $rev+1;
/* replaced
DB::Execute('INSERT INTO utils_attachment_file(attach_id,original,created_by,revision) VALUES(%d,%s,%d,%d)',array($id,$oryg,Acl::get_user(),$rev));
with:*/
DB::Execute('INSERT INTO utils_attachment_file(attach_id,original,created_by,revision,created_on) VALUES(%d,%s,%d,%d,%s)',array($id,$oryg,Acl::get_user(),$rev,$created_on_date));
DB::CompleteTrans();
$local = $this->get_data_dir().$this->group;
@mkdir($local,0777,true);
$dest_file = $local.'/'.$id.'_'.$rev;
rename($file,$dest_file);
if ($this->add_func) call_user_func($this->add_func,$id,$rev,$dest_file,$this->add_args);
}
$this->ret_attach = false;
if (isset($this->watchdog_category)) Utils_WatchdogCommon::new_event($this->watchdog_category,$this->watchdog_id,'N_~_'.$id);
}