Epesi ENS | Epesi Academy | epesi.cloud PaaS | GitHub

Help Me! | Download from SourceForge Download Epesi BIM Free & Open Source CRM


Hey,

The notes tab addon functionality is good for tracking history yet I'd like to tweak it a little bit - to add a date field which the user may specify which date the entry is for (instead of epesi filling it automatically when the entry is saved).

Is it possible to extend or tweak it relatively easily or should I re-create this addon altogether?

Hi
you can do it with something like this - Utils/Attachment/Attachment_0.php:
- add after line 590:


$form->addElement('date','created_on',$this->t('Date'),array('format'=>'Y-m-d H:i'));
$form->addRule('created_on',$this->t('Field required'),'required');

- replace lines 602-603 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']));

- add after line 604:

$form->setDefaults(array('created_on'=>time()));

- in submit_attach and submit_edit methods in INSERT into utils_attachment_note and utils_attachment_file queries add created_on column and fill it with value from created_on form field.

Please test this solution... There can be a bug with editing note with date older than previous entered one (newer revision with older date) but not sure 100%... If some bug occur you can add rule that will prevent adding older date than previous revision during note edition.

Hope it will be helpful...
Paul

9 days later

Hey Paul,

Are you sure about the line 590 changes/
Because after the if condition on line 590 I don't see any point in adding this since
there isn't seem to exist any variable of $form name.

I am on revision 159.

Regards.

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);
}

Hi,

As you can see in submit_edit method, INSERT is done only if text of note changed or when file is changed... You can add some condition to take into account date field.

Thanks to you I found another bug - there is wrong date in browse of attachments if there was a note without file, and next edition attachs file without note edition. To fix it replace lines 203-204:


$note_on = Base_RegionalSettingsCommon::time2reg($row['note_on'],0);
$note_on_time = Base_RegionalSettingsCommon::time2reg($row['note_on'],1);

with:

$created_on = $row['note_on']>$row['upload_on']?$row['note_on']:$row['upload_on'];
$note_on = Base_RegionalSettingsCommon::time2reg($created_on,0);
$note_on_time = Base_RegionalSettingsCommon::time2reg($created_on,1);

It will be fixed with next release.

ok, some more hints:
- %D in database query means argument is date, use it instead of %s, because someone can change select value to something what is not a number and query will fail triggering error. %D will replace something like that with 1970-01-01.
- %T in db qry means time - as above.
- you can debug with

Epesi::alert(print_r($variable,true));
you can also write log to file with
error_log(print_r($variable,true)."\n",3,'data/logfile.txt');

Cheers
Paul

Hey Paul,

I did take into account the created_on date field in the INSERT inside submit_edit()
though that still doesn't update the date field (and I'm obviously changing it).

Regards,
Liran.

Lirit,

couple lines above INSERT there is condition:

if($data['note']!=$text) {

and it's not passed if note isn't changed - INSERT is not called....

Paul

Ahh yeah, I didn't notice that I wasn't really changing the text (as I thought I did)
but only the date.

Thanks.

Write a Reply...