kopia lustrzana https://github.com/newroco/mail2deck
refac: undo formatting
rodzic
0ac19c37f0
commit
7189ce34c5
|
@ -2,14 +2,12 @@
|
||||||
|
|
||||||
namespace Mail2Deck;
|
namespace Mail2Deck;
|
||||||
|
|
||||||
class DeckClass
|
class DeckClass {
|
||||||
{
|
|
||||||
private $responseCode;
|
private $responseCode;
|
||||||
|
|
||||||
private function apiCall($request, $endpoint, $data = null, $attachment = false)
|
private function apiCall($request, $endpoint, $data = null, $attachment = false){
|
||||||
{
|
|
||||||
$curl = curl_init();
|
$curl = curl_init();
|
||||||
if ($data && !$attachment) {
|
if($data && !$attachment) {
|
||||||
$endpoint .= '?' . http_build_query($data);
|
$endpoint .= '?' . http_build_query($data);
|
||||||
}
|
}
|
||||||
curl_setopt_array($curl, array(
|
curl_setopt_array($curl, array(
|
||||||
|
@ -27,44 +25,38 @@ class DeckClass
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
|
|
||||||
if ($request === 'POST') {
|
if($request === 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, (array) $data);
|
||||||
curl_setopt($curl, CURLOPT_POSTFIELDS, (array) $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = curl_exec($curl);
|
$response = curl_exec($curl);
|
||||||
$err = curl_error($curl);
|
$err = curl_error($curl);
|
||||||
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
if ($err) {
|
if($err) echo "cURL Error #:" . $err;
|
||||||
echo "cURL Error #:" . $err;
|
|
||||||
}
|
|
||||||
|
|
||||||
return json_decode($response);
|
return json_decode($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getParameters($params, $boardFromMail = null)
|
public function getParameters($params, $boardFromMail = null) {// get the board and the stack
|
||||||
{// get the board and the stack
|
if(!$boardFromMail) // if board is not set within the email address, look for board into email subject
|
||||||
if (!$boardFromMail) { // if board is not set within the email address, look for board into email subject
|
if(preg_match('/b-"([^"]+)"/', $params, $m) || preg_match("/b-'([^']+)'/", $params, $m)) {
|
||||||
if (preg_match('/b-"([^"]+)"/', $params, $m) || preg_match("/b-'([^']+)'/", $params, $m)) {
|
$boardFromMail = $m[1];
|
||||||
$boardFromMail = $m[1];
|
$params = str_replace($m[0], '', $params);
|
||||||
$params = str_replace($m[0], '', $params);
|
}
|
||||||
}
|
if(preg_match('/s-"([^"]+)"/', $params, $m) || preg_match("/s-'([^']+)'/", $params, $m)) {
|
||||||
}
|
|
||||||
if (preg_match('/s-"([^"]+)"/', $params, $m) || preg_match("/s-'([^']+)'/", $params, $m)) {
|
|
||||||
$stackFromMail = $m[1];
|
$stackFromMail = $m[1];
|
||||||
$params = str_replace($m[0], '', $params);
|
$params = str_replace($m[0], '', $params);
|
||||||
}
|
}
|
||||||
if (preg_match('/u-"([^"]+)"/', $params, $m) || preg_match("/u-'([^']+)'/", $params, $m)) {
|
if(preg_match('/u-"([^"]+)"/', $params, $m) || preg_match("/u-'([^']+)'/", $params, $m)) {
|
||||||
$userFromMail = $m[1];
|
$userFromMail = $m[1];
|
||||||
$params = str_replace($m[0], '', $params);
|
$params = str_replace($m[0], '', $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
$boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards");
|
$boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards");
|
||||||
$boardId = $boardName = null;
|
$boardId = $boardName = null;
|
||||||
foreach ($boards as $board) {
|
foreach($boards as $board) {
|
||||||
if (strtolower($board->title) == strtolower($boardFromMail)) {
|
if(strtolower($board->title) == strtolower($boardFromMail)) {
|
||||||
if (!$this->checkBotPermissions($board)) {
|
if(!$this->checkBotPermissions($board)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$boardId = $board->id;
|
$boardId = $board->id;
|
||||||
|
@ -73,17 +65,14 @@ class DeckClass
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($boardId) {
|
if($boardId) {
|
||||||
$stacks = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/$boardId/stacks");
|
$stacks = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/$boardId/stacks");
|
||||||
foreach ($stacks as $key => $stack) {
|
foreach($stacks as $key => $stack)
|
||||||
if (strtolower($stack->title) == strtolower($stackFromMail)) {
|
if(strtolower($stack->title) == strtolower($stackFromMail)) {
|
||||||
$stackId = $stack->id;
|
$stackId = $stack->id;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
if($key == array_key_last($stacks) && !isset($stackId)) $stackId = $stacks[0]->id;
|
||||||
if ($key == array_key_last($stacks) && !isset($stackId)) {
|
|
||||||
$stackId = $stacks[0]->id;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -98,27 +87,20 @@ class DeckClass
|
||||||
return $boardStack;
|
return $boardStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addCard($data, $user, $board = null)
|
public function addCard($data, $user, $board = null) {
|
||||||
{
|
|
||||||
$params = $this->getParameters($data->title, $board);
|
$params = $this->getParameters($data->title, $board);
|
||||||
|
|
||||||
if ($params) {
|
if($params) {
|
||||||
$data->title = $params->newTitle;
|
$data->title = $params->newTitle;
|
||||||
$card = $this->apiCall("POST", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$params->board}/stacks/{$params->stack}/cards", $data);
|
$card = $this->apiCall("POST", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$params->board}/stacks/{$params->stack}/cards", $data);
|
||||||
$card->board = $params->board;
|
$card->board = $params->board;
|
||||||
$card->stack = $params->stack;
|
$card->stack = $params->stack;
|
||||||
|
|
||||||
if ($params->userId) {
|
if ($params->userId) $user->userId = $params->userId;
|
||||||
$user->userId = $params->userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->responseCode == 200) {
|
if($this->responseCode == 200) {
|
||||||
if (ASSIGN_SENDER || $params->userId) {
|
if(ASSIGN_SENDER || $params->userId) $this->assignUser($card, $user);
|
||||||
$this->assignUser($card, $user);
|
if($data->attachments) $this->addAttachments($card, $data->attachments);
|
||||||
}
|
|
||||||
if ($data->attachments) {
|
|
||||||
$this->addAttachments($card, $data->attachments);
|
|
||||||
}
|
|
||||||
$card->boardTitle = $params->boardTitle;
|
$card->boardTitle = $params->boardTitle;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -128,8 +110,7 @@ class DeckClass
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addAttachments($card, $attachments)
|
private function addAttachments($card, $attachments) {
|
||||||
{
|
|
||||||
$fullPath = getcwd() . "/attachments/"; //get full path to attachments directory
|
$fullPath = getcwd() . "/attachments/"; //get full path to attachments directory
|
||||||
for ($i = 0; $i < count($attachments); $i++) {
|
for ($i = 0; $i < count($attachments); $i++) {
|
||||||
$file = $fullPath . $attachments[$i];
|
$file = $fullPath . $attachments[$i];
|
||||||
|
@ -144,25 +125,20 @@ class DeckClass
|
||||||
public function assignUser($card, $mailUser)
|
public function assignUser($card, $mailUser)
|
||||||
{
|
{
|
||||||
$board = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}");
|
$board = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}");
|
||||||
$boardUsers = array_map(function ($user) {
|
$boardUsers = array_map(function ($user) { return $user->uid; }, $board->users);
|
||||||
return $user->uid;
|
|
||||||
}, $board->users);
|
|
||||||
|
|
||||||
foreach ($boardUsers as $user) {
|
foreach($boardUsers as $user) {
|
||||||
if ($user === $mailUser->userId) {
|
if($user === $mailUser->userId) {
|
||||||
$this->apiCall("PUT", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}/stacks/{$card->stack}/cards/{$card->id}/assignUser", $mailUser);
|
$this->apiCall("PUT", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}/stacks/{$card->stack}/cards/{$card->id}/assignUser", $mailUser);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkBotPermissions($board)
|
private function checkBotPermissions($board) {
|
||||||
{
|
foreach($board->acl as $acl)
|
||||||
foreach ($board->acl as $acl) {
|
if($acl->participant->uid == NC_USER && $acl->permissionEdit)
|
||||||
if ($acl->participant->uid == NC_USER && $acl->permissionEdit) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue