22
33namespace Jaysc \QuoteBy \XF \Entity ;
44
5- use XF \Mvc \Entity \Entity ;
65use XF \Mvc \Entity \Structure ;
76
87class 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}
0 commit comments