Skip to content
Permalink
Browse files

added insert position option for cache pic upload; updates #987

  • Loading branch information...
following5 committed Aug 26, 2017
1 parent 7887929 commit 0a85567d860f099cca76140313edba5825bb1775
@@ -16,6 +16,8 @@ class picture
public $sFileExtension = '';
public $bFilenamesSet = false;
private $originalPosition;
public static function pictureIdFromUUID($uuid)
{
return sql_value("SELECT `id` FROM `pictures` WHERE `uuid`='&1'", 0, $uuid);
@@ -63,13 +65,14 @@ public function __construct($nNewPictureId = ID_NEW)
$sUUID = mb_strtoupper(sql_value("SELECT UUID()", ''));
$this->rePicture->setValue('uuid', $sUUID);
$this->rePicture->setValue('node', $opt['logic']['node']['id']);
$this->originalPosition = false;
} else {
$this->rePicture->load($this->nPictureId);
$this->originalPosition = $this->getPosition();
$sFilename = $this->getFilename();
$fna = mb_split('\\.', $sFilename);
$this->sFileExtension = mb_strtolower($fna[count($fna) - 1]);
$this->bFilenamesSet = true;
}
}
@@ -524,6 +527,19 @@ public function getPosition()
return $this->rePicture->getValue('seq');
}
/**
* @param int $position
*/
public function setPosition($position)
{
if ($this->originalPosition === false) {
// position numbers are always >= 1
$this->rePicture->setValue('seq', max(1, $position));
} else {
// repositioning existing pictures is not implemented yet
}
}
/**
* @return bool|null
*/
@@ -587,6 +603,20 @@ public function save($restore = false, $original_id = 0, $original_url = "")
return false;
}
// produce a position number gap for inserting
sql(
"UPDATE `pictures`
SET `seq`=`seq`+1
WHERE `object_type`='&1' AND `object_id`='&2' AND `seq` >= '&3'
ORDER BY `seq` DESC",
$this->getObjectType(),
$this->getObjectId(),
$this->getPosition()
);
// The seq numbers need not to be consecutive, so it dosen't matter
// if something fails here and leaves the gap open.
$this->setArchiveFlag($restore, $original_id);
$bRetVal = $this->rePicture->save();
$this->resetArchiveFlag();
@@ -855,6 +885,8 @@ public function imagick_rotate(&$image)
*/
public function up()
{
// TODO: protect the following operations by a transaction
$prevPos = sql_value(
"
SELECT MAX(`seq`)
@@ -886,6 +918,10 @@ public function up()
$this->getPictureId(),
$maxPos + 1
);
// If something goes wrong from here (i.e. DB server failure) and
// we are moving anything else but the last picture, it will
// produce a wrong picture order. No big deal, the user can easily
// correct that, but still a transaction would be nice.
sql(
"
UPDATE `pictures` SET `seq`='&4'
@@ -71,13 +71,31 @@
$tpl->error(ERROR_INVALID_OPERATION);
}
$rsPics = sql(
"SELECT `id`, `title`, `seq`, `seq`+1 AS `nextseq`
FROM `pictures`
WHERE `object_type`='&1' AND `object_id`='&2'
ORDER BY `seq`",
$picture->getObjectType(),
$picture->getObjectId()
);
$allpics = sql_fetch_assoc_table($rsPics);
if (isset($_REQUEST['position'])) {
// picture position numbers always are >= 1
$position = max(1, $_REQUEST['position'] + 0);
} else {
$position = PHP_INT_MAX;
}
$position = min($position, $allpics ? $allpics[count($allpics)-1]['seq'] + 1 : 1);
// uploaded file ok?
if (isset($_REQUEST['ok'])) { // Ocprop
$bError = false;
$picture->setSpoiler(isset($_REQUEST['spoiler']) && $_REQUEST['spoiler'] == '1'); // Ocprop
$picture->setDisplay((isset($_REQUEST['notdisplay']) && $_REQUEST['notdisplay'] == '1') == false); // Ocprop
$picture->setMapPreview(isset($_REQUEST['mappreview']) && $_REQUEST['mappreview'] == '1');
$picture->setPosition($position);
$title = isset($_REQUEST['title']) ? $_REQUEST['title'] : ''; // Ocprop
if ($title == '' || ($picture->getObjectType() == OBJECT_CACHELOG && trim($title) == '')) {
@@ -213,6 +231,9 @@
} elseif ($picture->getObjectType() == OBJECT_CACHELOG) {
$tpl->assign('loguuid', $_REQUEST['loguuid']);
}
$tpl->assign('allpics', $allpics);
$tpl->assign('position', $position);
$tpl->assign('appendseq', $allpics ? $allpics[count($allpics)-1]['seq']+1 : 1);
}
$rsCache = sql("SELECT `wp_oc`, `name` FROM `caches` WHERE `cache_id`='&1'", $picture->getCacheId());
@@ -76,10 +76,10 @@
<td valign="top">{t}File:{/t}</td>
<td colspan="2">
<input type="hidden" name="MAX_FILE_SIZE" value="{$siteSettings.logic.pictures.maxsize}" />
<input class="input300" name="file" type="file" />
<input class="input400" name="file" type="file" />
</td>
</tr>
{if $errorfile==ERROR_UPLOAD_ERR_NO_FILE}
{if $errorfile==ERROR_UPLOAD_ERR_NO_FILE}
<tr><td>&nbsp;</td><td colspan="2"><span class="errormsg">{t}No picture file given.{/t}</span></td></tr>
{elseif $errorfile==ERROR_UPLOAD_ERR_SIZE}
<tr><td>&nbsp;</td><td colspan="2"><span class="errormsg">{t 1=$siteSettings.logic.pictures.maxsize/1024|string_format:"%d"}The file was too big. The maximum file size is %1 KB.{/t}</span></td></tr>
@@ -88,6 +88,30 @@
{elseif $errorfile==ERROR_UPLOAD_ERR_TYPE}
<tr><td>&nbsp;</td><td colspan="2"><span class="errormsg">{t 1=$siteSettings.logic.pictures.extensions|upper|replace:";":", "}Only the following picture formats are allowed: %1.{/t}</span></td></tr>
{/if}
{* The code would work for both cache and log pics, but probably noone will need
this for logs. (Logs are rarely edited later, usually have few pics and allow
easier reordering.) *}
{if $allpics|@count > 0 && $objecttype==OBJECT_CACHE}
<tr><td class="spacer" colspan="2"></td></tr>
<tr>
<td>{t}Insert:{/t}</td>
<td colspan="3">
<select name="position">
<option value="{$allpics.0.seq}" {if $position <= $allpics.0.seq}selected{/if}>{t}as first picture{/t}</option>
{assign var=picpos value=1}
{foreach from=$allpics item=pic}
{if $picpos < $allpics|@count}
<option value="{$pic.nextseq}" {if $position == $pic.nextseq}selected{/if}>{t 1=$pic.title|escape}after "%1"{/t}</option>
{/if}
{assign var=picpos value=$picpos+1}
{/foreach}
<option value="{$appendseq}" {if $position >= $appendseq}selected{/if}>{t}as last picture{/t}</option>
</select>
</td>
</tr>
{/if}
{/if}
<tr><td class="spacer" colspan="2"></td></tr>
@@ -1948,3 +1948,7 @@ INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2649', 'There a
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2650', 'This geocache will be published today at <b>%1</b>.', '2017-08-20 19:30:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2651', 'This geocache will be published <b>tomorrow</b> at <b>%1</b>.', '2017-08-20 19:30:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2652', 'This geocache will be published <b>in %1 days</b> (%2) at <b>%3</b>.', '2017-08-20 19:30:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2655', 'Insert:', '2017-08-20 19:30:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2656', 'as first picture', '2017-08-20 19:30:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2657', 'after "%1"', '2017-08-20 19:30:00');
INSERT INTO `sys_trans` (`id`, `text`, `last_modified`) VALUES ('2658', 'as last picture', '2017-08-20 19:30:00');
@@ -1946,3 +1946,7 @@ INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUE
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2650', 'DE', 'Dieser Cache wird heute um <b>%1</b> Uhr veröffentlicht.', '2017-08-20 19:30:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2651', 'DE', 'Dieser Cache wird <b>morgen</b> um <b>%1</b> Uhr veröffentlicht.', '2017-08-20 19:30:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2652', 'DE', 'Dieser Cache wird <b>in %1 Tagen</b> (%2) um <b>%3</b> Uhr veröffentlicht.', '2017-08-20 19:30:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2655', 'DE', 'Einfügen:', '2017-08-20 19:30:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2656', 'DE', 'als erstes Bild', '2017-08-20 19:30:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2657', 'DE', 'nach &bdquo;%1&ldquo;', '2017-08-20 19:30:00');
INSERT INTO `sys_trans_text` (`trans_id`, `lang`, `text`, `last_modified`) VALUES ('2658', 'DE', 'als letztes Bild', '2017-08-20 19:30:00');
@@ -2013,7 +2013,11 @@
IF ISNULL(@XMLSYNC) OR @XMLSYNC!=1 THEN
SET NEW.`date_created`=NOW();
SET NEW.`last_modified`=NOW();
IF NEW.`seq` = 0 THEN
IF NEW.`seq` <= 0 THEN
/*
* position numbers always start at 1;
* by default we append the new pic at the end
*/
SET NEW.`seq` =
IFNULL(
(SELECT MAX(`seq`)+1

0 comments on commit 0a85567

Please sign in to comment.
You can’t perform that action at this time.