1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radchasay\Comment\HTMLForm; |
4
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
6
|
|
|
use \Anax\DI\DIInterface; |
7
|
|
|
use \Radchasay\Comment\Post; |
8
|
|
|
use \Radchasay\User\User; |
9
|
|
|
use \Radchasay\Comment\PostCategory; |
10
|
|
|
use \Radchasay\Comment\Post2Cat; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Example of FormModel implementation. |
14
|
|
|
*/ |
15
|
|
|
class CreatePostForm extends FormModel |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Constructor injects with DI container. |
19
|
|
|
* |
20
|
|
|
* @param Anax\DI\DIInterface $di a service container |
21
|
|
|
*/ |
22
|
2 |
View Code Duplication |
public function __construct(DIInterface $di) |
|
|
|
|
23
|
|
|
{ |
24
|
2 |
|
parent::__construct($di); |
25
|
|
|
|
26
|
2 |
|
$this->form->create( |
27
|
|
|
[ |
28
|
2 |
|
"id" => __CLASS__, |
29
|
2 |
|
"legend" => "Create Post", |
30
|
|
|
"class" => "createNewPostForm" |
31
|
2 |
|
], |
32
|
|
|
[ |
33
|
|
|
"title" => [ |
34
|
2 |
|
"type" => "text", |
35
|
2 |
|
], |
36
|
|
|
|
37
|
|
|
"text" => [ |
38
|
2 |
|
"type" => "textarea", |
39
|
2 |
|
], |
40
|
|
|
|
41
|
|
|
"tags" => [ |
42
|
2 |
|
"type" => "text", |
43
|
2 |
|
], |
44
|
|
|
|
45
|
|
|
"submit" => [ |
46
|
2 |
|
"type" => "submit", |
47
|
2 |
|
"value" => "Create Post", |
48
|
2 |
|
"callback" => [$this, "callbackSubmit"], |
49
|
2 |
|
], |
50
|
|
|
] |
51
|
2 |
|
); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Callback for submit-button which should return true if it could |
57
|
|
|
* carry out its work and false if something failed. |
58
|
|
|
* |
59
|
|
|
* @return boolean true if okey, false if something went wrong. |
60
|
|
|
*/ |
61
|
|
|
public function callbackSubmit() |
62
|
|
|
{ |
63
|
|
|
// Get values from the submitted form |
64
|
|
|
// |
65
|
|
|
$post = new Post(); |
66
|
|
|
$post->setDB($this->di->get("db")); |
67
|
|
|
$title = htmlentities($this->form->value("title")); |
68
|
|
|
$data = $this->form->value("text"); |
69
|
|
|
$text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]); |
70
|
|
|
|
71
|
|
|
var_dump(strlen($title)); |
|
|
|
|
72
|
|
|
if (strlen($title) > 50 ) { |
73
|
|
|
//$this->form->remeberValues(); |
|
|
|
|
74
|
|
|
$this->form->addOutput("Title is too long. Please fix!"); |
75
|
|
|
return false; |
76
|
|
|
} else { |
77
|
|
|
$post->posttitle = $title; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$post->posttext = $text; |
81
|
|
|
$post->postname = htmlentities($this->di->get("session")->get("email")); |
82
|
|
|
|
83
|
|
|
$tags = htmlspecialchars($this->form->value("tags")); |
84
|
|
|
|
85
|
|
|
$user = new User(); |
86
|
|
|
$user->setDb($this->di->get("db")); |
87
|
|
|
$user->getInformation($post->postname); |
88
|
|
|
$user->points += 2; |
|
|
|
|
89
|
|
|
|
90
|
|
|
$user->save(); |
91
|
|
|
$post->save(); |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->createCategory($tags, $post->id); |
95
|
|
|
|
96
|
|
|
$url = $this->di->get("url")->create("comment/viewAllPosts"); |
97
|
|
|
$this->di->get("response")->redirect($url); |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
public function createCategory($tags, $postId) |
103
|
|
|
{ |
104
|
|
|
$postcat = new PostCategory(); |
105
|
|
|
$postcat->setDb($this->di->get("db")); |
106
|
|
|
$allCats = $postcat->findall(); |
107
|
|
|
|
108
|
|
|
$existingCats = []; |
109
|
|
|
|
110
|
|
|
foreach ($allCats as $ac) { |
111
|
|
|
array_push($existingCats, $ac->category); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$tags = array_map('trim', explode(',', $tags)); |
115
|
|
|
|
116
|
|
|
$tags = array_map("strtoupper", $tags); |
117
|
|
|
|
118
|
|
|
$uniqueTags = []; |
119
|
|
|
|
120
|
|
|
foreach ($tags as $t) { |
121
|
|
|
if (!in_array($t, $existingCats)) { |
122
|
|
|
if (!in_array($t, $uniqueTags)) { |
123
|
|
|
array_push($uniqueTags, $t); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
foreach ($uniqueTags as $ut) { |
129
|
|
|
$pc = new PostCategory(); |
130
|
|
|
$pc->setDb($this->di->get("db")); |
131
|
|
|
$pc->category = $ut; |
132
|
|
|
|
133
|
|
|
$pc->save(); |
134
|
|
|
} |
135
|
|
|
// |
136
|
|
|
foreach ($tags as $t) { |
137
|
|
|
$pc = new PostCategory(); |
138
|
|
|
$pc->setDb($this->di->get("db")); |
139
|
|
|
|
140
|
|
|
$id = $pc->getId($t); |
141
|
|
|
|
142
|
|
|
$p2c = new Post2Cat(); |
143
|
|
|
$p2c->setDb($this->di->get("db")); |
144
|
|
|
$p2c->catid = $id; |
145
|
|
|
$p2c->postid = $postId; |
146
|
|
|
|
147
|
|
|
$p2c->save(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.