Skip to content

Commit 828b269

Browse files
committed
Basically everything
1 parent ef77653 commit 828b269

18 files changed

+573
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 4
6+
charset = utf-8
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.{yml, json}]
12+
indent_style = space
13+
indent_size = 2
14+
15+
[*.md]
16+
trim_trailing_whitespace = false

.gitattributes

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
README.md export-ignore
2+
CHANGELOG.md export-ignore
3+
.gitignore export-ignore
4+
.gitattributes export-ignore
5+
/.idea export-ignore
6+
.editorconfig export-ignore
7+
/docs export-ignore

.gitignore

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### OSX
2+
.DS_Store
3+
.AppleDouble
4+
.LSOverride
5+
6+
# Icon must end with two \r
7+
Icon
8+
9+
# Thumbnails
10+
._*
11+
12+
# Files that might appear in the root of a volume
13+
.DocumentRevisions-V100
14+
.fseventsd
15+
.Spotlight-V100
16+
.TemporaryItems
17+
.Trashes
18+
.VolumeIcon.icns
19+
20+
# Directories potentially created on remote AFP share
21+
.AppleDB
22+
.AppleDesktop
23+
Network Trash Folder
24+
Temporary Items
25+
.apdisk
26+
27+
vendor
28+
node_modules
29+
hot
30+
mix-manifest.json
31+
/.idea

.idea/Notes for Craft CMS.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## 1.0.0
2+
- Initial Release

composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "ether/notes",
3+
"description": "A note taking field type for Craft CMS 3",
4+
"version": "1.0.0",
5+
"type": "craft-plugin",
6+
"license": "MIT",
7+
"minimum-stability": "dev",
8+
"require": {
9+
"craftcms/cms": "^3.5"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"ether\\notes\\": "src/"
14+
}
15+
},
16+
"support": {
17+
"email": "[email protected]",
18+
"docs": "https://docs.ethercreative.co.uk/notes",
19+
"source": "https://github.com/ethercreative/notes",
20+
"issues": "https://github.com/ethercreative/notes/issues"
21+
},
22+
"extra": {
23+
"handle": "notes",
24+
"name": "Notes",
25+
"developer": "Ether Creative",
26+
"developerUrl": "https://ethercreative.co.uk",
27+
28+
"class": "ether\\notes\\Notes",
29+
"schemaVersion": "1.0.0"
30+
}
31+
}

icon.svg

+12
Loading

src/Field.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Notes for Craft CMS
4+
*
5+
* @link https://ethercreative.co.uk
6+
* @copyright Copyright (c) 2020 Ether Creative
7+
*/
8+
9+
namespace ether\notes;
10+
11+
use Craft;
12+
use craft\base\ElementInterface;
13+
use craft\db\Query;
14+
use craft\elements\User;
15+
use craft\helpers\ArrayHelper;
16+
use craft\helpers\DateTimeHelper;
17+
use ether\notes\web\NotesAsset;
18+
use yii\db\Expression;
19+
20+
/**
21+
* Class Field
22+
*
23+
* @author Ether Creative
24+
* @package ether\notes
25+
*/
26+
class Field extends \craft\base\Field
27+
{
28+
29+
public static $table = '{{%notes}}';
30+
public static $dateFormat = 'M j, Y g:ia';
31+
32+
/** @var bool Will allow the deleting of notes if true */
33+
public $allowDeleting = false;
34+
35+
public static function displayName (): string
36+
{
37+
return Craft::t('notes', 'Notes');
38+
}
39+
40+
public static function hasContentColumn (): bool
41+
{
42+
return false;
43+
}
44+
45+
public static function supportedTranslationMethods (): array
46+
{
47+
return [
48+
self::TRANSLATION_METHOD_NONE,
49+
self::TRANSLATION_METHOD_SITE,
50+
];
51+
}
52+
53+
public function normalizeValue ($value, ElementInterface $element = null)
54+
{
55+
if (!$element)
56+
return [];
57+
58+
$where = [
59+
'elementId' => $element->id,
60+
];
61+
62+
if ($this->getIsTranslatable($element))
63+
$where['siteId'] = $element->siteId;
64+
65+
$rawNotes = (new Query())
66+
->select('id, note, userId, dateCreated')
67+
->from(self::$table)
68+
->where($where)
69+
->orderBy('dateCreated desc')
70+
->all();
71+
72+
$select = <<<SQL
73+
[[elements]].[[id]],
74+
CASE WHEN NULLIF([[users.firstName]], '') is null THEN [[users.username]] ELSE CONCAT([[users.firstName]], " ", [[users.lastName]]) END
75+
SQL;
76+
77+
$users = User::find()
78+
->select(new Expression($select))
79+
->id(ArrayHelper::getColumn($rawNotes, 'userId'))
80+
->pairs();
81+
82+
$notes = [];
83+
84+
foreach ($rawNotes as $note)
85+
{
86+
$notes[] = new Note([
87+
'id' => $note['id'],
88+
'note' => $note['note'],
89+
'author' => $users[$note['userId']],
90+
'userId' => $note['userId'],
91+
'date' => DateTimeHelper::toDateTime($note['dateCreated'])->format(self::$dateFormat),
92+
]);
93+
}
94+
95+
return $notes;
96+
}
97+
98+
public function getSettingsHtml ()
99+
{
100+
return Craft::$app->getView()->renderTemplate('notes/settings', [
101+
'allowDeleting' => $this->allowDeleting,
102+
]);
103+
}
104+
105+
protected function inputHtml ($value, ElementInterface $element = null): string
106+
{
107+
if (!$element || !$element->id)
108+
return Craft::t('notes', 'You must save the element before you can add notes!');
109+
110+
$view = Craft::$app->getView();
111+
$view->registerAssetBundle(NotesAsset::class);
112+
113+
return $view->renderTemplate('notes/field', [
114+
'ns' => $this->handle,
115+
'element' => $element,
116+
'notes' => $value,
117+
'allowDeleting' => $this->allowDeleting,
118+
]);
119+
}
120+
121+
}

src/Note.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Notes for Craft CMS
4+
*
5+
* @link https://ethercreative.co.uk
6+
* @copyright Copyright (c) 2020 Ether Creative
7+
*/
8+
9+
namespace ether\notes;
10+
11+
use craft\base\Model;
12+
use craft\elements\User;
13+
14+
/**
15+
* Class Note
16+
*
17+
* @author Ether Creative
18+
* @package ether\notes
19+
*/
20+
class Note extends Model
21+
{
22+
23+
public $id;
24+
public $note;
25+
public $author;
26+
public $userId;
27+
public $date;
28+
29+
public function getUser ()
30+
{
31+
return User::findOne(['id' => $this->userId]);
32+
}
33+
34+
}

src/Notes.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Notes for Craft CMS
4+
*
5+
* @link https://ethercreative.co.uk
6+
* @copyright Copyright (c) 2020 Ether Creative
7+
*/
8+
9+
namespace ether\notes;
10+
11+
use craft\base\Plugin;
12+
use craft\events\RegisterComponentTypesEvent;
13+
use craft\services\Fields;
14+
use yii\base\Event;
15+
16+
/**
17+
* Class Notes
18+
*
19+
* @author Ether Creative
20+
* @package ether\notes
21+
*/
22+
class Notes extends Plugin
23+
{
24+
25+
public function init ()
26+
{
27+
parent::init();
28+
29+
Event::on(
30+
Fields::class,
31+
Fields::EVENT_REGISTER_FIELD_TYPES,
32+
[$this, 'onRegisterFieldTypes']
33+
);
34+
}
35+
36+
public function onRegisterFieldTypes (RegisterComponentTypesEvent $event)
37+
{
38+
$event->types[] = Field::class;
39+
}
40+
41+
}

src/controllers/FieldController.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Notes for Craft CMS
4+
*
5+
* @link https://ethercreative.co.uk
6+
* @copyright Copyright (c) 2020 Ether Creative
7+
*/
8+
9+
namespace ether\notes\controllers;
10+
11+
use Craft;
12+
use craft\elements\User;
13+
use craft\web\Controller;
14+
use DateTime;
15+
use ether\notes\Field;
16+
17+
/**
18+
* Class FieldController
19+
*
20+
* @author Ether Creative
21+
* @package ether\notes\controllers
22+
*/
23+
class FieldController extends Controller
24+
{
25+
26+
public function actionAdd ()
27+
{
28+
$request = Craft::$app->getRequest();
29+
30+
$siteId = $request->getRequiredBodyParam('siteId');
31+
$elementId = $request->getRequiredBodyParam('elementId');
32+
$userId = $request->getRequiredBodyParam('userId');
33+
$note = $request->getRequiredBodyParam('note');
34+
35+
Craft::$app->getDb()->createCommand()
36+
->insert(
37+
Field::$table,
38+
compact('siteId', 'elementId', 'userId', 'note')
39+
)
40+
->execute();
41+
42+
$user = User::findOne(['id' => $userId]);
43+
$meta = $user->fullName ?: $user->username . ' &bull; ' . (new DateTime())->format(Field::$dateFormat);
44+
45+
return $this->asJson(compact('meta'));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)