Skip to content

Commit 195eff3

Browse files
committed
fixes and updates
1 parent ea34b2a commit 195eff3

File tree

7 files changed

+95
-87
lines changed

7 files changed

+95
-87
lines changed

Job/QuoteByBuild.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ class QuoteByBuild extends AbstractJob
1414

1515
public function run($maxRunTime)
1616
{
17-
if($this->data['start'] == 0) {
18-
$this->app->db()->emptyTable('xf_jaysc_quote_by');
19-
}
2017

2118
$startTime = microtime(true);
2219

@@ -35,6 +32,26 @@ public function run($maxRunTime)
3532
), $this->data['start']);
3633
if (!$postIds)
3734
{
35+
$db->query(
36+
"
37+
DELETE FROM xf_jaysc_quote_by
38+
WHERE (
39+
SELECT xf_post.post_id
40+
FROM xf_post
41+
WHERE xf_post.post_id = xf_jaysc_quote_by.post_id
42+
) IS NULL
43+
",)->execute();
44+
45+
$db->query(
46+
"
47+
DELETE FROM xf_jaysc_quote_by
48+
WHERE (
49+
SELECT xf_post.post_id
50+
FROM xf_post
51+
WHERE xf_post.post_id = xf_jaysc_quote_by.parent_post_id
52+
) IS NULL
53+
",)->execute();
54+
3855
return $this->complete();
3956
}
4057

@@ -46,7 +63,7 @@ public function run($maxRunTime)
4663

4764
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $quoteByRepo */
4865
$quoteByRepo = $this->app->repository('Jaysc\QuoteBy:QuoteByPost');
49-
$quoteByFinder = $quoteByRepo->findExistingQuoteByPostToId($this->data['batch']);
66+
$quoteByFinder = $quoteByRepo->findExistingQuoteByPostToId($postIds);
5067

5168
$quoteByPosts = $quoteByFinder->fetch();
5269

@@ -56,6 +73,7 @@ public function run($maxRunTime)
5673
$this->data['start'] = $post->post_id;
5774

5875
$quoteByRepo->updateQuoteByPost($post, $quoteByPosts);
76+
$quoteByRepo->deleteUnreferencedQuoteByPosts($post, $quoteByPosts);
5977

6078
$done++;
6179

Repository/QuoteByPost.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class QuoteByPost extends Repository
1111
/**
1212
* @return Finder
1313
*/
14-
public function findExistingQuoteByPostToId(int $postId)
14+
public function findExistingQuoteByPostToId($postIds)
1515
{
1616
$finder = $this->finder('Jaysc\QuoteBy:Post');
1717
$finder
1818
->setDefaultOrder('post_id', 'DESC')
19-
->where('post_id', '<=', $postId);
19+
->where('post_id', '=', $postIds);
2020

2121
return $finder;
2222
}
@@ -52,10 +52,11 @@ public function findQuoteByPostInThread(int $threadId, $posts)
5252
return $finder;
5353
}
5454

55-
public function updateQuoteByPost(Post $post, $quoteByPosts = null)
55+
/** @param \Jaysc\QuoteBy\xf\Entity\Post $post */
56+
public function updateQuoteByPost(Post $post, $quoteByPosts = [])
5657
{
57-
preg_match_all('/\[QUOTE="(?P<username>.+), post: (?P<post>.+), member: (?P<member>.+)"\]/', $post->message, $matches);
58-
58+
$matches = $post->getMatches();
59+
5960
$numOfQuotes = count($matches[0]);
6061

6162
$posted = [];
@@ -75,16 +76,18 @@ public function updateQuoteByPost(Post $post, $quoteByPosts = null)
7576

7677
$key = [$parentPostId, $post->post_id];
7778

78-
if ($quoteByPosts) {
79-
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $quoteByPost */
79+
if (count($quoteByPosts) > 0) {
80+
/** @var \Jaysc\QuoteBy\Entity\Post $quoteByPost */
8081
foreach ($quoteByPosts as $quoteByPost) {
8182
if ($quoteByPost->parent_post_id == $parentPostId) {
8283
$exists = true;
84+
break;
8385
}
8486
}
8587
}
8688

8789
if ($exists || in_array($key, $posted)) {
90+
array_push($posted, $key);
8891
continue;
8992
}
9093

@@ -98,4 +101,33 @@ public function updateQuoteByPost(Post $post, $quoteByPosts = null)
98101
array_push($posted, $key);
99102
}
100103
}
104+
105+
/** @param \Jaysc\QuoteBy\xf\Entity\Post $post */
106+
public function deleteUnreferencedQuoteByPosts(Post $post, $quoteByPosts = []) {
107+
$matches = $post->getMatches();
108+
109+
/** @var \Jaysc\QuoteBy\Entity\Post $quoteByPost */
110+
foreach ($quoteByPosts as $quoteByPost) {
111+
if ($quoteByPost->post_id != $post->post_id) {
112+
continue;
113+
}
114+
115+
if (!in_array($quoteByPost->parent_post_id, $matches['post'])) {
116+
$quoteByPost->delete();
117+
}
118+
}
119+
}
120+
121+
public function deleteQuoteByPostByPostId(Post $post) {
122+
$finder = $this->finder('Jaysc\QuoteBy:Post');
123+
$quoteByPosts = $finder
124+
->setDefaultOrder('post_id', 'DESC')
125+
->with('Thread', true)
126+
->with('Post', true)
127+
->where('post_id', $post->post_id)->fetch();
128+
129+
foreach ($quoteByPosts as $quoteByPost) {
130+
$quoteByPost->delete();
131+
}
132+
}
101133
}

XF/Entity/Post.php

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22

33
namespace Jaysc\QuoteBy\XF\Entity;
44

5-
use XF\Mvc\Entity\Entity;
65
use XF\Mvc\Entity\Structure;
76

87
class Post extends XFCP_Post
98
{
10-
public function quoteByCount()
9+
public function quoteByPosts()
1110
{
12-
$count = 0;
1311

12+
$result = [];
1413
if ($this && $this->QuoteBy) {
1514
foreach($this->QuoteBy as $quoteByPost) {
1615
if ($quoteByPost->Post->message_state == 'visible') {
17-
$count++;
16+
array_push($result, $quoteByPost);
1817
}
1918
}
2019
}
2120

22-
return $count;
21+
return $result;
2322
}
2423

2524
public static function getStructure(Structure $structure)
2625
{
2726
$structure = parent::getStructure($structure);
28-
$structure->getters['quote_by_count'] = ['getter' => 'quoteByCount', 'cache' => false];
27+
$structure->getters['quote_by_posts'] = ['getter' => 'quoteByPosts', 'cache' => false];
2928

3029
$structure->relations['QuoteBy'] = [
3130
'entity' => 'Jaysc\QuoteBy:Post',
@@ -38,95 +37,54 @@ public static function getStructure(Structure $structure)
3837
return $structure;
3938
}
4039

40+
public function getMatches()
41+
{
42+
preg_match_all('/\[QUOTE="(?P<username>[^\s\\\]+), post: (?P<post>\d+), member: (?P<member>\d+)"\]/', $this->message, $matches);
43+
44+
return $matches;
45+
}
46+
4147
protected function _postSave()
4248
{
4349
parent::_postSave();
4450

45-
preg_match_all('/\[QUOTE="(?P<username>[^\s\\\]+), post: (?P<post>\d+), member: (?P<member>\d+)"\]/', $this->message, $matches);
51+
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $repo */
52+
$repo = $this->repository('Jaysc\QuoteBy:QuoteByPost');
53+
54+
$matches = $this->getMatches();
4655

4756
if ($this->isInsert()) {
48-
$this->saveQuoteByPost($matches);
57+
$repo->updateQuoteByPost($this);
4958
} elseif ($this->isUpdate()) {
5059
$quoteByPosts = $this->getQuoteByPosts();
5160

52-
$this->deleteQuoteByPosts($matches, $quoteByPosts);
53-
54-
$this->saveQuoteByPost($matches, $quoteByPosts);
55-
} elseif ($this->isDeleted()) {
56-
$quoteByPosts = $this->getQuoteByPosts();
61+
$repo->deleteUnreferencedQuoteByPosts($this, $quoteByPosts);
5762

58-
$this->deleteQuoteByPosts($matches, $quoteByPosts);
63+
$repo->updateQuoteByPost($this, $quoteByPosts);
5964
}
6065

6166
$matches;
6267
}
6368

64-
protected function getQuoteByPosts()
69+
protected function _postDelete()
6570
{
71+
parent::_postDelete();
72+
6673
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $repo */
6774
$repo = $this->repository('Jaysc\QuoteBy:QuoteByPost');
6875

69-
$finder = $repo->findQuoteByPost($this->post_id);
70-
71-
$quoteByPosts = $finder->fetch();
72-
73-
return $quoteByPosts;
74-
}
75-
76-
protected function deleteQuoteByPosts($matches, $quoteByPosts)
77-
{
78-
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $quoteByPost */
79-
foreach ($quoteByPosts as $quoteByPost) {
80-
if (!in_array($quoteByPost->parent_post_id, $matches['post'])) {
81-
$dbQuoteByPost = $this->em()->find('Jaysc\QuoteBy:Post', [$quoteByPost->parent_post_id, $quoteByPost->post_id]);
82-
83-
$dbQuoteByPost->delete();
84-
}
85-
}
76+
$repo->deleteQuoteByPostByPostId($this);
8677
}
8778

88-
protected function saveQuoteByPost($matches, $quoteByPosts = null)
79+
protected function getQuoteByPosts()
8980
{
90-
$numOfQuotes = count($matches[0]);
91-
92-
$posted = [];
93-
94-
for ($x = 0; $x < $numOfQuotes; $x++) {
95-
$exists = false;
96-
//$username = $matches['username'][$x];
97-
$parentPostId = $matches['post'][$x];
98-
//$member = $matches['member'][$x];
99-
100-
$parentPost = $this->em()
101-
->find('XF:Post', $parentPostId);
102-
103-
if (!$parentPost || $parentPost->thread_id != $this->thread_id){
104-
continue;
105-
}
106-
107-
$key = [$parentPostId, $this->post_id];
108-
109-
if ($quoteByPosts) {
110-
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $quoteByPost */
111-
foreach ($quoteByPosts as $quoteByPost) {
112-
if ($quoteByPost->parent_post_id == $parentPostId) {
113-
$exists = true;
114-
}
115-
}
116-
}
117-
118-
if ($exists || in_array($key, $posted)) {
119-
continue;
120-
}
81+
/** @var \Jaysc\QuoteBy\Repository\QuoteByPost $repo */
82+
$repo = $this->repository('Jaysc\QuoteBy:QuoteByPost');
12183

122-
$quoteByPost = $this->em()->create('Jaysc\QuoteBy:Post');
123-
$quoteByPost->parent_post_id = $parentPostId;
124-
$quoteByPost->post_id = $this->post_id;
125-
$quoteByPost->thread_id = $this->thread_id;
84+
$finder = $repo->findQuoteByPost($this->post_id);
12685

127-
$quoteByPost->save();
86+
$quoteByPosts = $finder->fetch();
12887

129-
array_push($posted, $key);
130-
}
88+
return $quoteByPosts;
13189
}
13290
}

_data/templates.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
margin-top: 20px;
55
padding: 6px 10px;
66
color: @xf-paletteAccent3;
7-
background: @xf-paletteAccent1;
7+
background: xf-diminish(@xf-contentAltBg, 2%);
88
border-width: 1px;
99
border-style: solid;
1010
border-top-color: #dfdfdf;

_output/template_modifications/_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"hash": "fab1136ec7c9e83bfa1c79a114197584"
44
},
55
"public/jaysc_post_macros_quoted_by.json": {
6-
"hash": "ce6e1e5a8c2c71027e4ba3cd5a4497a2"
6+
"hash": "21e2ff61acb26bc67bf90308004316bc"
77
}
88
}

_output/template_modifications/public/jaysc_post_macros_quoted_by.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"enabled": true,
66
"action": "str_replace",
77
"find": "<footer class=\"message-footer\">",
8-
"replace": "$0\n<xf:css src=\"jaysc_quote_by.less\" />\n<xf:if is=\"$post.QuoteBy is not empty and $post.quote_by_count > 0\">\n\t<div class=\"quoteByBlock\">\n\t\t{{ phrase('jaysc_quoteBy_quotedBy') }}\n\t\t<xf:foreach loop=\"$post.QuoteBy\" value=\"$quoteByPost\" i=\"$i\">\n\t\t\t<a href=\"{{ link('goto/' . 'post', null, {'id': $quoteByPost.Post.post_id}) }}\"\n\t\t\t rel=\"nofollow\"\n\t\t\t data-xf-click=\"attribution\"\n\t\t\t data-content-selector=\"#post-{$quoteByPost.Post.post_id}\">{{$quoteByPost.Post.username}}</a>\n\t\t\t<xf:if is=\"{$i} < count($post.QuoteBy)\">, </xf:if>\n\t\t</xf:foreach>\n\t</div>\n</xf:if>\n"
8+
"replace": "$0\n<xf:css src=\"jaysc_quote_by.less\" />\n<xf:if is=\"$post.quote_by_posts is not empty\">\n\t<div class=\"quoteByBlock\">\n\t\t{{ phrase('jaysc_quoteBy_quotedBy') }}\n\t\t<xf:foreach loop=\"$post.quote_by_posts\" value=\"$quoteByPost\" i=\"$i\">\n\t\t\t<a href=\"{{ link('goto/' . 'post', null, {'id': $quoteByPost.Post.post_id}) }}\"\n\t\t\t rel=\"nofollow\"\n\t\t\t data-xf-click=\"attribution\"\n\t\t\t data-content-selector=\"#post-{$quoteByPost.Post.post_id}\">{{$quoteByPost.Post.username}}</a><xf:if is=\"{$i} < count($post.quote_by_posts)\">, </xf:if>\n\t\t</xf:foreach>\n\t</div>\n</xf:if>\n"
99
}

addon.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"legacy_addon_id": "",
33
"title": "Jaysc - Quote By",
44
"description": "",
5-
"version_id": 101,
6-
"version_string": "1.0.1 Alpha",
5+
"version_id": 110,
6+
"version_string": "1.1.0 Alpha",
77
"dev": "",
88
"dev_url": "",
99
"faq_url": "",

0 commit comments

Comments
 (0)