Repository navigation
Expand file tree
/
Copy pathEvernoteModel.php
More file actions
239 lines (210 loc) · 5.42 KB
/
Copy pathEvernoteModel.php
File metadata and controls
239 lines (210 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
defined('BASE_PATH') or define("BASE_PATH", dirname(__FILE__));
require (BASE_PATH.'/vendor/autoload.php');
require (BASE_PATH.'/Tool.php');
require (BASE_PATH.'/Session_Cli.php');
class EvernoteModel
{
protected $token;
protected $tag;
protected $notebook;
protected $note_prefix;
protected $sandbox = false;
protected $china = false;
protected $instance = null;
public function __construct($config)
{
$this->token = $config['token'];
$this->tag = $config['tag'];
$this->notebook = $config['notebook'];
$this->note_prefix = $config['note_prefix'];
$this->instance = Session_Cli::getInstance();
}
/**
* get base client instance
* @author bignerd
* @since 2017-01-09T11:43:36+0800
*/
protected function getClient()
{
$client = new \Evernote\Client($this->token, $this->sandbox, null, null, $this->china);
return $client;
}
/**
* 添加一条笔记记录
* @author bignerd
* @since 2017-01-09T15:46:53+0800
*/
public function add($content='')
{
$title = $this->noteTitle();
$guid = $this->noteExist();
$content = Tool::handleSaveContent($content);
if($guid){
return $this->updateNote($guid, $content);
}else{
return $this->createNote($title, $content, $this->tag);
}
}
/**
* 笔记的默认标题
* @author bignerd
* @since 2017-01-09T15:28:17+0800
*/
public function noteTitle()
{
date_default_timezone_set('PRC');
return $this->note_prefix.' '.date('Y/m').' 第'.Tool::weekIndexOfMonth().'周';
}
/**
* 判断本周的笔记是否已经创建
* @author bignerd
* @since 2017-01-09T15:35:27+0800
*/
public function noteExist()
{
$guid = '';
$title = $this->noteTitle();
if($guid = $this->getSession()){
return $guid;
}else if($guid = $this->searchNote($title)){
$this->setSession($guid);
return $guid;
}else{
return false;
}
}
/**
* 搜索笔记
* @author bignerd
* @since 2017-01-09T11:51:49+0800
* @param string $title 笔记标题
* @param string $notebookName 笔记本名称
*/
public function searchNote($title = '', $notebookName = 'mind')
{
$client = $this->getClient();
/**
* The search string
*/
$search = new \Evernote\Model\Search($title);
/**
* The notebook to search in
*/
$notebook = new \Evernote\Model\Notebook();
$notebook->guid = $this->getNotebookGuid($notebookName);
/**
* The scope of the search
*/
$scope = \Evernote\Client::SEARCH_SCOPE_PERSONAL;
/**
* The order of the sort
*/
$order = \Evernote\Client::SORT_ORDER_REVERSE | \Evernote\Client::SORT_ORDER_RECENTLY_CREATED;
/**
* The number of results
*/
$maxResult = 1;
$results = $client->findNotesWithSearch($search, $notebook, $scope, $order, $maxResult);
$noteGuid = '';
foreach ($results as $result) {
$noteGuid = $result->guid;
$noteType = $result->type;
$noteTitle = $result->title;
$noteCreated = $result->created;
$noteUpdated = $result->updated;
}
return $noteGuid;
}
/**
* 获取当前笔记中的笔记内容
* @author bignerd
* @since 2017-01-09T14:39:21+0800
* @param [type] $guid
*/
public function getNoteData($guid)
{
$client = $this->getClient();
$scope = \Evernote\Client::PERSONAL_SCOPE;
$note = $client->getNote($guid, $scope);
$noteContext = (string)($note->content->toEnml());
$oldContext = Tool::handleEvernoteContent($noteContext);
$noteData = [
'title' => $note->getTitle(),
'plainText' => $oldContext,
'noteObj' => $note,
];
return $noteData;
}
/**
* 创建笔记
* @author bignerd
* @since 2017-01-09T13:30:56+0800
* @param string $title
* @param string $content
* @param array $tag
*/
public function createNote($title='', $content='', array $tag)
{
$client = $this->getClient();
$note = new \Evernote\Model\Note();
$note->title = $title;
$note->content = new \Evernote\Model\PlainTextNoteContent($content);
$note->tagNames = $tag;
$notebook = new \Evernote\Model\Notebook();
$notebook->guid = $this->getNotebookGuid($this->notebook);
$saved = $client->uploadNote($note, $notebook);
if($guid = $saved->getGuid()){
$this->setSession($guid);
echo '创建成功.';
}else{
echo '创建失败.';
}
}
/**
* 更新笔记内容
* @author bignerd
* @since 2017-01-09T15:07:27+0800
* @param [type] $guid
* @param string $newContent
*/
public function updateNote($guid, $newContent='')
{
$client = $this->getClient();
$existNoteData = $this->getNoteData($guid);
$newContent = $existNoteData['plainText']."<br /><br />".$newContent;
$note = new \Evernote\Model\Note();
$note->title = $existNoteData['title'];
$note->tagNames = $this->tag;
$note->content = new \Evernote\Model\PlainTextNoteContent($newContent);
$updated = $client->replaceNote($existNoteData['noteObj'], $note);
echo ($updated)? '更新成功.' : '更新失败.';
}
public function getNotebookGuid($notebookName)
{
$client = $this->getClient();
$notebooks = $client->listNotebooks();
$guid = '';
foreach ($notebooks as $notebook) {
if($notebook->name == $notebookName){
$guid = $notebook->guid;
break;
}
}
return $guid;
}
public function getSession()
{
$title = $this->noteTitle();
$sessionKey = 'guid_'.$title;
return $this->instance->get($sessionKey);
}
public function setSession($guid)
{
$title = $this->noteTitle();
$sessionKey = 'guid_'.$title;
$this->instance->set($sessionKey, $guid);
return true;
}
}
?>