Browse Source

[UPDATE] use generic multiselect field for attached_to

experimental
Georgi Hristov 3 years ago
parent
commit
4803678c7f
5 changed files with 108 additions and 95 deletions
  1. +53
    -60
      modules/Utils/Attachment/AttachmentCommon_0.php
  2. +7
    -17
      modules/Utils/Attachment/AttachmentInstall.php
  3. +8
    -17
      modules/Utils/Attachment/Attachment_0.php
  4. +39
    -0
      modules/Utils/Attachment/patches/20171025_use_generic_attached_to_field.php
  5. +1
    -1
      modules/Utils/Attachment/theme/View_entry.tpl

+ 53
- 60
modules/Utils/Attachment/AttachmentCommon_0.php View File

@@ -39,9 +39,9 @@ class Utils_AttachmentCommon extends ModuleCommon {

public static function get_where($group,$group_starts_with=true) {
if($group_starts_with)
return DB::GetCol('SELECT attachment FROM utils_attachment_local WHERE local '.DB::like().' \''.DB::addq($group).'%\'');
return DB::GetCol('SELECT id FROM utils_attachment_data_1 WHERE f_attached_to '.DB::like().' \'\_\_'.DB::addq($group).'%\'');
else
return DB::GetCol('SELECT attachment FROM utils_attachment_local WHERE local='.DB::qstr($group));
return DB::GetCol('SELECT id FROM utils_attachment_data_1 WHERE f_attached_to='.DB::qstr('__' . $group. '__'));
}
/**
* Example usage:
@@ -52,12 +52,11 @@ class Utils_AttachmentCommon extends ModuleCommon {
if(isset($selective) && !empty($selective))
$ids = array_intersect($ids,$selective);
foreach($ids as $id) {
$note = Utils_RecordBrowserCommon::get_record('utils_attachment', $id);
$note = self::get_note($id);
DB::StartTrans();
foreach($note['files'] as $fsid) {
Utils_FileStorageCommon::delete($fsid);
}
DB::Execute('DELETE FROM utils_attachment_local WHERE attachment=%d',array($id));
DB::CompleteTrans();
Utils_RecordBrowserCommon::delete_record('utils_attachment',$id,true);
}
@@ -67,7 +66,7 @@ class Utils_AttachmentCommon extends ModuleCommon {
$ids = self::get_where($group,$group_starts_with);
if(!$ids) return;
foreach($ids as $id) {
$note = Utils_RecordBrowserCommon::get_record('utils_attachment', $id);
$note = self::get_note($id);
foreach($note['files'] as $fsid) {
try {
@@ -91,7 +90,7 @@ class Utils_AttachmentCommon extends ModuleCommon {
$details
);
if ($rid && $ret) {
$r = Utils_RecordBrowserCommon::get_record('utils_attachment', $rid);
$r = self::get_note($rid);
$of = Utils_RecordBrowserCommon::get_val('utils_attachment', 'attached_to', $r);
$ret['title'] .= " [ $of ]";
}
@@ -114,7 +113,7 @@ class Utils_AttachmentCommon extends ModuleCommon {
public static function add_file($note, $user, $oryg, $file) {
if($oryg===null) $oryg='';
$note = is_numeric($note)? Utils_RecordBrowserCommon::get_record('utils_attachment', $note): $note;
$note = is_numeric($note)? self::get_note($note): $note;
if ($note['crypted']) {
if (isset($_SESSION['client']['cp'.$note['id']]))
file_put_contents($file,self::encrypt(file_get_contents($file),$_SESSION['client']['cp'.$note['id']]));
@@ -146,7 +145,7 @@ class Utils_AttachmentCommon extends ModuleCommon {
if(!$ids) return array();
$files = array();
foreach($ids as $id) {
$note = Utils_RecordBrowserCommon::get_record('utils_attachment', $id);
$note = self::get_note($id);
foreach($note['files'] as $fsid) {
$meta = Utils_FileStorageCommon::meta($fsid);
$files[] = array_merge($meta, $note, array(
@@ -165,8 +164,12 @@ class Utils_AttachmentCommon extends ModuleCommon {
return $files;
}

public static function move_notes($to_group, $from_group) {
DB::Execute('UPDATE utils_attachment_local SET local=%s WHERE local=%s', array($to_group, $from_group));
public static function move_notes($to_group, $from_group, $notes = []) {
$notes = $notes?: self::get_where($from_group, false);
foreach ($notes as $note) {
if (!self::detach_note($note, $from_group)) continue;
self::attach_note($note, $to_group);
}
}

public static function copy_notes($from_group, $to_group) {
@@ -176,8 +179,26 @@ class Utils_AttachmentCommon extends ModuleCommon {
$mapping[$n['note_id']] = @self::add($to_group,$n['permission'],Acl::get_user(),$n['text'],$n['original'],$n['file']);
}
return $mapping;
}
public static function detach_note($note, $group) {
$note = is_numeric($note)? self::get_note($note): $note;
if (($key = array_search($group, $note['attached_to'])) === false) return false;
unset($note['attached_to'][$key]);
DB::Execute('UPDATE utils_attachment_data_1 SET f_attached_to=%s WHERE id=%d',array(Utils_RecordBrowserCommon::encode_multi($note['attached_to']), $note['id']));
self::new_watchdog_event($group, '-', $note['id']);
return true;
}
public static function attach_note($note, $group) {
$note = is_numeric($note)? self::get_note($note): $note;
if (array_search($group, $note['attached_to']) !== false) return false;
$note['attached_to'][] = $group;
DB::Execute('UPDATE utils_attachment_data_1 SET f_attached_to=%s WHERE id=%d',array(Utils_RecordBrowserCommon::encode_multi($note['attached_to']), $note['id']));
self::new_watchdog_event($group, '+', $note['id']);
return true;
}
public static function is_image($note) {
if (!is_string($note)) $note = $note['original'];
return preg_match('/\.(jpg|jpeg|gif|png|bmp)$/i',$note);
@@ -284,27 +305,6 @@ class Utils_AttachmentCommon extends ModuleCommon {
return implode('<br>', $labels) . ($inline_nodes? '<hr>': '') . implode('<hr>', $inline_nodes);
}

public static function display_attached_to($row, $nolink = false, $a=null,$view=false) {
$locals = DB::GetCol('SELECT local FROM utils_attachment_local WHERE attachment=%d',array($row['id']));
$ret = array();
foreach ($locals as $local) {
$param = explode('/', $local);
if (count($param) == 2 && preg_match('/^[1-9][0-9]*$/', $param[1])) {
if(!Utils_RecordBrowserCommon::check_table_name($param[0], false, false)) {
DB::Execute('DELETE FROM utils_attachment_local WHERE local=%s',array($local));
continue;
}
$label = Utils_RecordBrowserCommon::create_default_linked_label($param[0],$param[1],true);
$link =
Utils_RecordBrowserCommon::record_link_open_tag($param[0], $param[1], $nolink, 'view', array('switch_to_addon' => __('Notes')))
. $label . Utils_RecordBrowserCommon::record_link_close_tag();
$link = Utils_RecordBrowserCommon::create_default_record_tooltip_ajax($link, $param[0], $param[1]);
$ret[] = $link;
}
}
return implode(', ',$ret);
}
public static function description_callback($row,$nolink=false) {
if($row['title']) $ret = $row['title'];
elseif($row['crypted']) $ret = $row['id'].' ('.__('encrypted note').')';
@@ -493,8 +493,6 @@ class Utils_AttachmentCommon extends ModuleCommon {
$values['clone_id']=$values['id'];
break;
case 'added':
if(isset($values['local']))
DB::Execute('INSERT INTO utils_attachment_local(attachment,local,func,args) VALUES(%d,%s,%s,%s)',array($values['id'],$values['local'],$values['func'],$values['args']));
$new_values = $values;
break;
case 'edit_changes':
@@ -531,14 +529,9 @@ class Utils_AttachmentCommon extends ModuleCommon {
Epesi::alert(__('Cannot delete encrypted note'));
return false;
}
$count_locals = DB::GetOne('SELECT count(DISTINCT local) FROM utils_attachment_local WHERE attachment=%d',array($values['id']));
if($count_locals>1) {
$is_local = false;
if(isset($_SESSION['client']['utils_attachment_group']))
$is_local = DB::GetOne('SELECT 1 FROM utils_attachment_local WHERE attachment=%d AND local=%s',array($values['id'],$_SESSION['client']['utils_attachment_group']));
if($is_local) {
DB::Execute('DELETE FROM utils_attachment_local WHERE attachment=%d AND local=%s',array($values['id'],$_SESSION['client']['utils_attachment_group']));
self::new_watchdog_event($_SESSION['client']['utils_attachment_group'], '-', $values['id']);
if(count($values['attached_to'])>1) {
if(isset($_SESSION['client']['utils_attachment_group'])) {
self::detach_note($values, $_SESSION['client']['utils_attachment_group']);
} else
Epesi::alert(__('This note is attached to multiple records - please go to record and delete note there.'));
location(array());
@@ -559,11 +552,7 @@ class Utils_AttachmentCommon extends ModuleCommon {
$note_id = $values['id'];

if($mode=='added' && isset($values['clone_id'])) { //on cloning
$locals = DB::Execute('SELECT local,func,args FROM utils_attachment_local WHERE attachment=%d',array($values['clone_id']));
while($local = $locals->FetchRow())
DB::Execute('INSERT INTO utils_attachment_local(attachment,local,func,args) VALUES(%d,%s,%s,%s)',array($note_id,$local['local'],$local['func'],$local['args']));

$clone = Utils_RecordBrowserCommon::get_record('utils_attachment', $values['clone_id']);
$clone = self::get_note($values['clone_id']);
foreach($clone['files'] as $fsid) {
$meta = Utils_FileStorageCommon::meta($fsid);
$content = Utils_FileStorageCommon::read_content($fsid);
@@ -577,14 +566,11 @@ class Utils_AttachmentCommon extends ModuleCommon {
}
}

$locals = DB::GetCol('SELECT local FROM utils_attachment_local WHERE attachment=%d',array($note_id));
foreach ($locals as $local) {
$param = explode('/', $local);
if (count($param) == 2 && preg_match('/^[1-9][0-9]*$/', $param[1])) {
$subscribers = Utils_WatchdogCommon::get_subscribers($param[0], $param[1]);
foreach ($subscribers as $user_id) {
Utils_WatchdogCommon::user_subscribe($user_id, 'utils_attachment', $note_id);
}
foreach ($values['attached_to'] as $token) {
$token = Utils_RecordBrowserCommon::decode_record_token($token);
$subscribers = Utils_WatchdogCommon::get_subscribers($token['tab'], $token['id']);
foreach ($subscribers as $user_id) {
Utils_WatchdogCommon::user_subscribe($user_id, 'utils_attachment', $note_id);
}
}

@@ -602,19 +588,26 @@ class Utils_AttachmentCommon extends ModuleCommon {
}
public static function get_access($id) {
$locals = DB::GetCol('SELECT local FROM utils_attachment_local WHERE attachment=%d',array($id));
$note = self::get_note($id);
$ret = false;
foreach($locals as $local) {
list($recordset,$key) = explode('/',$local,2);
if(!Utils_RecordBrowserCommon::check_table_name($recordset, false, false)
|| !is_numeric($key)
|| Utils_RecordBrowserCommon::get_access($recordset,'view',$key)) {
foreach($note['attached_to'] as $token) {
$token = Utils_RecordBrowserCommon::decode_record_token($token);
if(!Utils_RecordBrowserCommon::check_table_name($token['tab'], false, false)
|| Utils_RecordBrowserCommon::get_access($token['tab'],'view',$token['id'])) {
$ret = true;
break;
}
}
return $ret;
}
public static function get_note($id) {
static $cache;
if (!isset($cache[$id])) {
$cache[$id] = Utils_RecordBrowserCommon::get_record('utils_attachment', $id);
}
return $cache[$id];
}

/**
* Create new watchdog event for record if $group denotes record.


+ 7
- 17
modules/Utils/Attachment/AttachmentInstall.php View File

@@ -59,10 +59,13 @@ class Utils_AttachmentInstall extends ModuleInstall {
'type' => 'checkbox',
'extra' => false,
'QFfield_callback'=>array('Utils_AttachmentCommon','QFfield_crypted')),
array('name' => _M('Attached to'),
'type' => 'calculated',
'extra' => false,
'display_callback'=>array('Utils_AttachmentCommon','display_attached_to')),
array('name' => _M('Attached to'),
'type' => 'multiselect',
'param' => '__RECORDSETS__::;',
'required' => false,
'extra' => false,
'visible'=>false,
),
);
Utils_RecordBrowserCommon::install_new_recordset('utils_attachment',$fields);
Utils_RecordBrowserCommon::add_access('utils_attachment', 'view', 'ACCESS:employee', array('(!permission'=>2, '|:Created_by'=>'USER_ID'));
@@ -79,18 +82,6 @@ class Utils_AttachmentInstall extends ModuleInstall {
Utils_RecordBrowserCommon::set_jump_to_id('utils_attachment', false);
Utils_RecordBrowserCommon::set_search('utils_attachment',1,0);

$ret &= DB::CreateTable('utils_attachment_local','
local C(255) NOTNULL,
attachment I4 NOTNULL,
func C(255),
args C(255)',
array('constraints'=>', FOREIGN KEY (attachment) REFERENCES utils_attachment_data_1(ID)'));
if(!$ret){
print('Unable to create table utils_attachment_local.<br>');
return false;
}
DB::CreateIndex('utils_attachment_local__idx', 'utils_attachment_local', 'local');

$this->create_data_dir();
file_put_contents($this->get_data_dir().'.htaccess','deny from all');
Base_ThemeCommon::install_default_theme($this->get_type());
@@ -103,7 +94,6 @@ class Utils_AttachmentInstall extends ModuleInstall {
Base_AclCommon::delete_permission('Attachments - view full download history');
$ret = true;

$ret &= DB::DropTable('utils_attachment_local');
Utils_RecordBrowserCommon::uninstall_recordset('utils_attachment');
Base_ThemeCommon::uninstall_default_theme($this->get_type());
return $ret;


+ 8
- 17
modules/Utils/Attachment/Attachment_0.php View File

@@ -68,13 +68,10 @@ class Utils_Attachment extends Module {
'func' => serialize($this->func),
'args' => serialize($this->args));
$rb_cols = array();
$single_group = (is_string($this->group) || count($this->group) == 1);
if ($this->force_multiple) {
$single_group = false;
}
$single_group = (is_string($this->group) || count($this->group) == 1) && !$this->force_multiple;
if ($single_group) {
$group = is_string($this->group) ? $this->group : reset($this->group);
$defaults['local'] = $group;
$defaults['attached_to'] = array($group);
} else {
// force attached to display
$rb_cols['attached_to'] = true;
@@ -94,7 +91,6 @@ class Utils_Attachment extends Module {
$this->rb->disable_actions(array('delete'));
$this->display_module($this->rb, array(array(':Created_by'=>$uid), $rb_cols, array('sticky'=>'DESC', 'edited_on'=>'DESC')), 'show_data');
} else {
$crits = array();
if(!is_array($this->group)) $this->group = array($this->group);

if(isset($_SESSION['attachment_copy']) && count($this->group)==1 && $_SESSION['attachment_copy']['group']!=$this->group) {
@@ -102,12 +98,11 @@ class Utils_Attachment extends Module {
Utils_TooltipCommon::open_tag_attrs($_SESSION['attachment_copy']['text']).' '.$this->create_callback_href(array($this,'paste'))
);
}
$crits = array(
'attached_to' => $this->group ?: 0
);

if($this->group) {
$g = array_map(array('DB','qstr'),$this->group);
$crits['id'] = DB::GetCol('SELECT attachment FROM utils_attachment_local WHERE local IN ('.implode(',',$g).')');
} else $crits['id'] = 0;
$this->display_module($this->rb, array($crits, $rb_cols, array('sticky'=>'DESC', 'edited_on'=>'DESC')), 'show_data');
$this->display_module($this->rb, array($crits, $rb_cols, array('sticky'=>'DESC', 'edited_on'=>'DESC')), 'show_data');
}
}

@@ -133,17 +128,13 @@ class Utils_Attachment extends Module {
public function paste() {
$group = reset($this->group);

if(DB::GetOne('SELECT 1 FROM utils_attachment_local WHERE attachment=%d AND local=%s',array($_SESSION['attachment_copy']['id'],$group))) return;
if (isset($_SESSION['attachment_cut']) && $_SESSION['attachment_cut']) {
$source_group = reset($_SESSION['attachment_copy']['group']);
DB::Execute('UPDATE utils_attachment_local SET local=%s,func=%s,args=%s WHERE attachment=%d AND local=%s', array($group, serialize($this->func), serialize($this->args), $_SESSION['attachment_copy']['id'], $source_group));
Utils_AttachmentCommon::new_watchdog_event($group, '+', $_SESSION['attachment_copy']['id']);
Utils_AttachmentCommon::new_watchdog_event($source_group, '-', $_SESSION['attachment_copy']['id']);
Utils_AttachmentCommon::move_notes($group, $source_group, [$_SESSION['attachment_copy']['id']]);
unset($_SESSION['attachment_cut']);
unset($_SESSION['attachment_copy']);
} else {
DB::Execute('INSERT INTO utils_attachment_local(local,attachment,func,args) VALUES(%s,%d,%s,%s)',array($group,$_SESSION['attachment_copy']['id'],serialize($this->func),serialize($this->args)));
Utils_AttachmentCommon::new_watchdog_event($group, '+', $_SESSION['attachment_copy']['id']);
Utils_AttachmentCommon::attach_note($_SESSION['attachment_copy']['id'], $group);
}
}



+ 39
- 0
modules/Utils/Attachment/patches/20171025_use_generic_attached_to_field.php View File

@@ -0,0 +1,39 @@
<?php

defined("_VALID_ACCESS") || die('Direct access forbidden');

Utils_RecordBrowserCommon::delete_record_field('utils_attachment', 'Attached to');

$field = array('name' => _M('Attached to'),
'type' => 'multiselect',
'position' => 'Crypted',
'param' => '__RECORDSETS__::;',
'required' => false,
'extra' => false,
'visible'=>false,
);

Utils_RecordBrowserCommon::new_record_field('utils_attachment', $field);

$cp = Patch::checkpoint('add_attached_to_values');
if (!$cp->is_done()) {
$last_id = $cp->get('last_id', 0);
$attachment_ids = DB::GetCol('SELECT id FROM utils_attachment_data_1 WHERE id > %d ORDER BY id ASC', [$last_id]);
foreach ($attachment_ids as $attachment_id) {
$attached_to = DB::GetCol('SELECT local FROM utils_attachment_local WHERE attachment=%d', [$attachment_id]);
if ($attached_to) {
$attached_to = Utils_RecordBrowserCommon::encode_multi($attached_to);
DB::Execute('UPDATE utils_attachment_data_1 SET f_attached_to=%s WHERE id=%d', [$attached_to, $attachment_id]);
}
$cp->set('last_id', $attachment_id);
}
$cp->done();
}

DB::DropTable('utils_filestorage_local');


+ 1
- 1
modules/Utils/Attachment/theme/View_entry.tpl View File

@@ -163,7 +163,7 @@
{assign var=y value=1}
{foreach key=k item=f from=$multiselects name=fields}
{if $y==1}
<td class="column" style="width: {$cols_percent}%;">
<td class="column" style="width: {$cols_percent}%;" colspan="2">
<table cellpadding="0" cellspacing="0" border="0" class="multiselects {if $action == 'view'}view{else}edit{/if}" style="border-top: none;">
{/if}
{$f.full_field}


Loading…
Cancel
Save