1<?php 2 3use go\core\model\Acl; 4use go\core\util\StringUtil; 5use go\modules\community\notes\model\Note; 6 7 8 9class goNote extends GoBaseBackendDiff { 10 11 public function DeleteMessage($folderid, $id, $contentparameters) { 12 ZLog::Write(LOGLEVEL_DEBUG, 'goNote->DeleteMessage('.$folderid.','.$id.')'); 13 $note = Note::findById($id); 14 15 if($note && $note->hasPermissionLevel(Acl::LEVEL_DELETE)) { 16 return $note->delete($note->primaryKeyValues()); 17 } else { 18 return true; 19 } 20 } 21 22 /** 23 * Get the item object that needs to be synced to the phone. 24 * This information will be send to the phone. 25 * 26 * Direction: SERVER -> PHONE 27 * 28 * @param int $folderid 29 * @param int $id 30 * @param array $contentparameters 31 * @return \SyncNote 32 */ 33 public function GetMessage($folderid, $id, $contentparameters) { 34 ZLog::Write(LOGLEVEL_DEBUG, 'goNote->GetMessage('.$folderid.','.$id.')'); 35 $note = Note::findById($id); 36 37 if(!$note) { 38 return false; 39 } 40 41 if(!$note->hasPermissionLevel(Acl::LEVEL_READ)) { 42 return false; 43 } 44 45 $message = new SyncNote(); 46 47 $bpReturnType = GoSyncUtils::getBodyPreferenceMatch($contentparameters->GetBodyPreference()); 48 49 if (Request::GetProtocolVersion() >= 12.0) { 50 $sbBody = new SyncBaseBody(); 51 52 $asBodyData = StringUtil::normalizeCrlf($note->content); 53 54 if ($bpReturnType == SYNC_BODYPREFERENCE_HTML) { 55 $sbBody->type = SYNC_BODYPREFERENCE_HTML; 56 $asBodyData = $note->content; 57 } else { 58 59 $sbBody->type = SYNC_BODYPREFERENCE_PLAIN; 60 $asBodyData = StringUtil::htmlToText($note->content); 61 } 62 ZLog::Write(LOGLEVEL_DEBUG, $asBodyData); 63 64 $sbBody->estimatedDataSize = strlen($asBodyData); 65 $sbBody->data = StringStreamWrapper::Open($asBodyData); 66 $sbBody->truncated = 0; 67 68 $message->asbody = $sbBody; 69 70 } else { 71 $message->body = StringUtil::normalizeCrlf($note->content); 72 $message->bodysize = strlen($message->body); 73 $message->bodytruncated = 0; 74 75 } 76 77 $message->lastmodified = $note->modifiedAt->format('U'); 78 $message->subject = $note->name; 79 80 81 return $message; 82 } 83 84 /** 85 * Save the information from the phone to Group-Office. 86 * 87 * Direction: PHONE -> SERVER 88 * 89 * @param int $folderid 90 * @param int $id 91 * @param \SyncNote $message 92 * @return array 93 */ 94 public function ChangeMessage($folderid, $id, $message, $contentParameters) { 95 ZLog::Write(LOGLEVEL_DEBUG, 'goNote->ChangeMessage('.$folderid.','.$id.')'); 96 97 ZLog::Write(LOGLEVEL_DEBUG, var_export($message, TRUE)); 98 99 $note = Note::findById($id); 100 101 if(!$note) { 102 $note = new Note (); 103 $note->noteBookId = (new \go\core\db\Query)->selectSingleValue('noteBookId')->from('sync_user_note_book')->where(['userId' => go()->getUserId()])->orderBy(['isDefault' => 'DESC'])->single(); 104 } 105 106 if(!$note->hasPermissionLevel(Acl::LEVEL_WRITE)) { 107 throw new StatusException(SYNC_ITEMOPERATIONSSTATUS_DL_ACCESSDENIED); 108 } 109 110 $note->content = GoSyncUtils::getBodyFromMessage($message); 111 112 if(isset($message->asbody) && isset($message->asbody->type)){ 113 switch($message->asbody->type){ 114 case SYNC_BODYPREFERENCE_PLAIN: 115 $note->content = StringUtil::textToHtml($note->content); 116 } 117 } 118 119 $note->name = !empty($message->subject) ? $message->subject : StringUtil::cutString(strip_tags($note->content), 20); 120 121 $note->cutPropertiesToColumnLength(); 122 123 if(!$note->save()){ 124 ZLog::Write(LOGLEVEL_WARN, 'ZPUSH2NOTE::Could not save ' . $note->id); 125 ZLog::Write(LOGLEVEL_WARN, var_export($note->getValidationErrors(), true)); 126 throw new StatusException(SYNC_STATUS_SERVERERROR); 127 } 128 129 130 131 return $this->StatMessage($folderid, $note->id); 132 } 133 134 /** 135 * Get the status of an item 136 * 137 * @param int $folderid 138 * @param int $id 139 * @return array 140 */ 141 public function StatMessage($folderid, $id) { 142 ZLog::Write(LOGLEVEL_DEBUG, 'goNote->StatMessage('.$folderid.','.$id.')'); 143 144 return Note::find() 145 ->select('id,unix_timestamp(modifiedAt) AS `mod`, "1" AS `flags`') 146 ->fetchMode(PDO::FETCH_ASSOC) 147 ->where(['id' => $id])->single(); 148 } 149 150 /** 151 * Get the list of the items that need to be synced 152 * 153 * @param int $folderid 154 * @param int $cutoffdate 155 * @return array 156 */ 157 public function GetMessageList($folderid, $cutoffdate) { 158 ZLog::Write(LOGLEVEL_DEBUG, 'goNote->GetMessageList('.$folderid.','.$cutoffdate.')'); 159 //if(!go()->getUser()->hasModule('notes')) { 160 //TODO refactor 161 if (!\GO::modules()->notes) { 162 return []; 163 } 164 $query = Note::find() 165 ->select('id,unix_timestamp(modifiedAt) AS `mod`, "1" AS `flags`') 166 ->fetchMode(PDO::FETCH_ASSOC) 167 ->join("sync_user_note_book", 's', 'n.noteBookId = s.noteBookId') 168 ->where(['s.userId' => go()->getUserId(), 'password' => ""]); 169// ZLog::Write(LOGLEVEL_DEBUG, $query->debugQueryString); 170 $notes = $query->all(); 171 172 173 174// ZLog::Write(LOGLEVEL_DEBUG, var_export($notes, true)); 175 176 return $notes; 177 } 178 179 /** 180 * Get the syncFolder that is attached to the given id 181 * 182 * @param int $id 183 * @return \SyncFolder 184 */ 185 public function GetFolder($id) { 186 187 if ($id != BackendGoConfig::NOTESBACKENDFOLDER) { 188 ZLog::Write(LOGLEVEL_WARN, "Note folder '$id' not found"); 189 return false; 190 } 191 192 $folder = new SyncFolder(); 193 $folder->serverid = $id; 194 $folder->parentid = "0"; 195 $folder->displayname = 'Notes'; 196 $folder->type = SYNC_FOLDER_TYPE_NOTE; 197 198 return $folder; 199 } 200 201 /** 202 * Get a list of folders that are located in the current folder 203 * 204 * @return array 205 */ 206 public function GetFolderList() { 207 $folders = array(); 208 $folder = $this->StatFolder(BackendGoConfig::NOTESBACKENDFOLDER); 209 $folders[] = $folder; 210 211 return $folders; 212 } 213 214 215 public function getNotification($folder=null) { 216 217 $record = Note::find() 218 ->fetchMode(PDO::FETCH_ASSOC) 219 ->select('COALESCE(count(*), 0) AS count, COALESCE(max(modifiedAt), 0) AS modifiedAt') 220 ->join("sync_user_note_book", 's', 'n.noteBookId = s.noteBookId') 221 ->where(['s.userId' => go()->getUserId()]) 222 ->single(); 223 224 $newstate = 'M'.$record['modifiedAt'].':C'.$record['count']; 225 ZLog::Write(LOGLEVEL_DEBUG,'goNote->getNotification() State: '.$newstate); 226 227 return $newstate; 228 } 229 230} 231