Skip to content

Commit

Permalink
add links and steps to zip import. fix #1645
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Nov 28, 2019
1 parent af3da54 commit 9932668
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/models/Steps.php
Expand Up @@ -59,6 +59,32 @@ public function create(string $body): void
}
}

/**
* Import a step from a complete step array
* Used when importing from zip archive (json)
*
* @param array $step
* @return void
*/
public function import(array $step): void
{
$this->Entity->canOrExplode('write');

$body = str_replace('|', ' ', $step['body']);
$sql = 'INSERT INTO ' . $this->Entity->type . '_steps (item_id, body, ordering, finished, finished_time)
VALUES(:item_id, :body, :ordering, :finished, :finished_time)';
$req = $this->Db->prepare($sql);
$req->bindParam(':item_id', $this->Entity->id, PDO::PARAM_INT);
$req->bindParam(':body', $body);
$req->bindParam(':ordering', $step['ordering']);
$req->bindParam(':finished', $step['finished']);
$req->bindParam(':finished_time', $step['finished_time']);

if ($req->execute() !== true) {
throw new DatabaseErrorException('Error while executing SQL query.');
}
}

/**
* Toggle the finished column of a step
*
Expand Down
19 changes: 19 additions & 0 deletions src/services/ImportZip.php
Expand Up @@ -181,9 +181,28 @@ private function dbInsert($item): void
$this->Entity->setId($newItemId);
}

// add tags
if (\mb_strlen($item['tags'] ?? '') > 1) {
$this->tagsDbInsert($item['tags']);
}
// add links
if (!empty($item['links'])) {
// don't import the links as as because the id might be different from the one we had before
// so add the link in the body
$header = '<h3>Linked items:</h3><ul>';
$end = '</ul>';
$linkText = '';
foreach($item['links'] as $link) {
$linkText .= sprintf('<li>[%s] %s</li>', $link['name'], $link['title']);
}
$this->Entity->update($item['title'], $item['date'], $item['body'] . $header . $linkText . $end);
}
// add steps
if (!empty($item['steps'])) {
foreach($item['steps'] as $step) {
$this->Entity->Steps->import($step);
}
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/services/MakeStreamZip.php
Expand Up @@ -210,6 +210,10 @@ private function addToZip(int $id): void
$entityArr = $this->Entity->entityData;
// save the uploads in entityArr for the json file
$entityArr['uploads'] = $uploadedFilesArr;
// add links
$entityArr['links'] = $this->Entity->Links->readAll();
// add steps
$entityArr['steps'] = $this->Entity->Steps->readAll();

$this->nameFolder();
$this->addTimestampFiles($id);
Expand Down

0 comments on commit 9932668

Please sign in to comment.