Skip to content

Commit

Permalink
added authors
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Aug 16, 2016
1 parent f43f34d commit f4225b4
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,11 @@ private function persistDocument($data, $document, $locale)
]
);
$form->submit($data, false);

if (array_key_exists('authored', $data)) {
$document->setAuthored(new \DateTime($data['authored']));
}
$document->setAuthors($this->getAuthors(isset($data['authors']) ? $data['authors'] : []));

if (!$form->isValid()) {
throw new InvalidFormException($form);
Expand All @@ -286,6 +288,15 @@ private function persistDocument($data, $document, $locale)
);
}

private function getAuthors(array $authors)
{
if (0 === count($authors)) {
return [$this->getUser()->getContact()->getId()];
}

return $authors;
}

/**
* Returns document-manager.
*
Expand Down
33 changes: 32 additions & 1 deletion Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class ArticleDocument implements
*/
protected $authored;

/**
* Array of contact-ids which has authored this article.
*
* @var int[]
*/
protected $authors;

public function __construct()
{
$this->structure = new Structure();
Expand Down Expand Up @@ -414,7 +421,7 @@ public function getPublished()
*
* @return $this
*/
public function setAuthored($authored)
public function setAuthored(\DateTime $authored = null)
{
$this->authored = $authored;

Expand All @@ -430,4 +437,28 @@ public function getAuthored()
{
return $this->authored;
}

/**
* Returns id of authors.
*
* @return int[]
*/
public function getAuthors()
{
return $this->authors;
}

/**
* Set authors.
*
* @param int[] $authors
*
* @return $this
*/
public function setAuthors($authors)
{
$this->authors = $authors;

return $this;
}
}
31 changes: 31 additions & 0 deletions Document/ArticleOngrDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class ArticleOngrDocument
*/
protected $authored;

/**
* @var int[]
*
* @Property(type="integer")
*/
protected $authors;

/**
* @param string $uuid
*/
Expand Down Expand Up @@ -371,4 +378,28 @@ public function setAuthored(\DateTime $authored = null)

return $this;
}

/**
* Returns authors.
*
* @return int[]
*/
public function getAuthors()
{
return $this->authors;
}

/**
* Set authors.
*
* @param int[] $authors
*
* @return $this
*/
public function setAuthors($authors)
{
$this->authors = $authors;

return $this;
}
}
7 changes: 7 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,12 @@ public function handleMetadataLoad(MetadataLoadEvent $event)
'property' => 'authored',
]
);
$metadata->addFieldMapping(
'authors',
[
'encoding' => 'system_localized',
'property' => 'authors',
]
);
}
}
3 changes: 3 additions & 0 deletions Resources/config/serializer/Document.ArticleDocument.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

<property name="published" type="DateTime" groups="defaultPage,smallPage"/>
<property name="authored" type="DateTime" groups="defaultPage,smallPage"/>
<property name="authors" groups="defaultPage,smallPage">
<type><![CDATA[array<integer>]]></type>
</property>
<property name="extensions" type="Sulu\Component\Content\Document\Extension\ExtensionContainer" serialized-name="ext" groups="defaultPage,preview"/>
</class>
</serializer>
17 changes: 17 additions & 0 deletions Resources/public/dist/components/articles/edit/settings/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
class="trigger-save-button"/>
</div>
</div>
<div class="grid-row">
<div class="grid-col-12 form-group">
<label for="authors"><%= translations.authors %></label>

<div id="authors"
data-mapper-property="authors"
data-form="true"
data-type="contact-selection"
data-type-instance-name="authors"
data-aura-component="contact-selection@sulucontact"
data-aura-instance-name="authors"
data-aura-contact="true"
data-aura-account="false"
data-aura-url="/admin/api/customers"
data-aura-contact-url="/admin/api/contacts?flat=true&sortBy=lastName"/>
</div>
</div>

<div class="grid-row section m-top-40" id="changelog-container" style="display: none;">
<h2 class="divider m-bottom-20" title="Shadow Section">
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Resources/public/js/components/articles/edit/settings/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
class="trigger-save-button"/>
</div>
</div>
<div class="grid-row">
<div class="grid-col-12 form-group">
<label for="authors"><%= translations.authors %></label>

<div id="authors"
data-mapper-property="authors"
data-form="true"
data-type="contact-selection"
data-type-instance-name="authors"
data-aura-component="contact-selection@sulucontact"
data-aura-instance-name="authors"
data-aura-contact="true"
data-aura-account="false"
data-aura-url="/admin/api/customers"
data-aura-contact-url="/admin/api/contacts?flat=true&sortBy=lastName"/>
</div>
</div>

<div class="grid-row section m-top-40" id="changelog-container" style="display: none;">
<h2 class="divider m-bottom-20" title="Shadow Section">
Expand Down
18 changes: 16 additions & 2 deletions Resources/public/js/components/articles/edit/settings/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ define([
},
translations: {
authored: 'sulu_article.authored',
authors: 'sulu_article.authors',
changelog: 'sulu.content.form.settings.changelog',
changed: 'sulu.content.form.settings.changelog.changed',
created: 'sulu.content.form.settings.changelog.created',
Expand All @@ -41,7 +42,16 @@ define([
* @param {object} data
*/
parseData: function(data) {
return data;
return {
authored: data.authored,
authors: _.map(data.authors, function(item) {
return 'c' + item;
}),
creator: data.creator,
changer: data.changer,
created: data.created,
changed: data.changed
};
},

/**
Expand Down Expand Up @@ -80,6 +90,10 @@ define([
* @param {object} action
*/
save: function(data, action) {
data.authors = _.map(data.authors, function(item) {
return item.substr(1);
});

this.sandbox.emit('sulu.articles.save', data, action);
},

Expand All @@ -92,7 +106,7 @@ define([
* @param {object} data
*/
saved: function(data) {
this.data = data;
this.data = this.parseData(data);
},

/**
Expand Down
35 changes: 31 additions & 4 deletions Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,44 @@ public function setUp()
$this->initPhpcr();
}

public function testPostWithoutAuthors($title = 'Test-Article', $template = 'default')
{
$client = $this->createAuthenticatedClient();
$client->request(
'POST',
'/api/articles?locale=de',
['title' => $title, 'template' => $template, 'authored' => '2016-01-01']
);

$this->assertHttpStatusCode(200, $client->getResponse());

$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals($title, $response['title']);
$this->assertEquals(self::$typeMap[$template], $response['type']);
$this->assertEquals($template, $response['template']);
$this->assertEquals(new \DateTime('2016-01-01'), new \DateTime($response['authored']));
$this->assertEquals([$this->getTestUserId()], $response['authors']);

return $response;
}

public function testPost($title = 'Test-Article', $template = 'default')
{
$client = $this->createAuthenticatedClient();
$client->request('POST', '/api/articles?locale=de', ['title' => $title, 'template' => $template, 'authored' => '2016-01-01']);
$client->request(
'POST',
'/api/articles?locale=de',
['title' => $title, 'template' => $template, 'authored' => '2016-01-01', 'authors' => [1, 2, 3]]
);

$this->assertHttpStatusCode(200, $client->getResponse());

$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals($title, $response['title']);
$this->assertEquals(self::$typeMap[$template], $response['type']);
$this->assertEquals($template, $response['template']);
$this->assertEquals('2016-01-01T00:00:00+0100', $response['authored']);
$this->assertEquals(new \DateTime('2016-01-01'), new \DateTime($response['authored']));
$this->assertEquals([1, 2, 3], $response['authors']);

return $response;
}
Expand Down Expand Up @@ -68,15 +94,16 @@ public function testPut($title = 'Sulu is awesome')
$client->request(
'PUT',
'/api/articles/' . $article['id'] . '?locale=de',
['title' => $title, 'template' => 'default', 'authored' => '2016-01-01']
['title' => $title, 'template' => 'default', 'authored' => '2016-01-01', 'authors' => [1, 3]]
);

$this->assertHttpStatusCode(200, $client->getResponse());

$response = json_decode($client->getResponse()->getContent(), true);
$this->assertNotEquals($article['title'], $response['title']);
$this->assertEquals($title, $response['title']);
$this->assertEquals('2016-01-01T00:00:00+0100', $response['authored']);
$this->assertEquals(new \DateTime('2016-01-01'), new \DateTime($response['authored']));
$this->assertEquals([1, 3], $response['authors']);
}

public function testPutExtensions(
Expand Down

0 comments on commit f4225b4

Please sign in to comment.