Spaces:
Sleeping
Sleeping
File size: 17,093 Bytes
07c3cdd | 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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | <?php
/**
* Class InputDocumentDrive
*/
/**
* Class InputDocumentDrive
*/
class AppDocumentDrive
{
/**
* @var PmDrive $drive
*/
private $drive;
/**
* @var Application $app
*/
private $app;
/**
* @var Users $user
*/
private $user;
private $statusDrive;
private $usersEmail = '';
/**
* InputDocumentDrive constructor.
*/
public function __construct()
{
$this->drive = new PmDrive();
$status = $this->drive->getServiceDriveStatus();
$status = !empty($status) ? ($status == 1 ? true : false): false;
$this->usersEmail = '';
$this->setStatusDrive($status);
}
/**
* @return boolean
*/
public function getStatusDrive()
{
return $this->statusDrive;
}
/**
* @param boolen $statusDrive
*/
public function setStatusDrive($statusDrive)
{
$this->statusDrive = $statusDrive;
}
public function loadUser ($usrUid)
{
$this->user = new Users();
$this->user->load($usrUid);
$this->drive->setDriveUser($this->user->getUsrEmail());
}
public function loadApplication ($appUid)
{
$this->app = new Application();
$sw = $this->app->exists($appUid);
if ($sw === true) {
$this->app->Load($appUid);
}
}
public function existAppFolderDrive ()
{
try {
if ($this->app->getAppDriveFolderUid() == null) {
$process = new Process();
$process->setProUid($this->app->getProUid());
//Set name folder, for cron process.
$result = $this->drive->createFolder(
'Case #' . $this->app->getAppNumber(),
$this->drive->getFolderIdPMDrive($this->user->getUsrUid())
);
$this->app->setAppDriveFolderUid($result->id);
$this->app->update($this->app->toArray(BasePeer::TYPE_FIELDNAME));
}
} catch (Exception $e) {
error_log('Error create folder Drive: ' . $e->getMessage());
}
}
public function permission ($appUid, $folderUid, $fileIdDrive)
{
$criteria = new Criteria('workflow');
$criteria->addSelectColumn(ApplicationPeer::PRO_UID);
$criteria->addSelectColumn(TaskUserPeer::TAS_UID);
$criteria->addSelectColumn(TaskUserPeer::USR_UID);
$criteria->addSelectColumn(TaskUserPeer::TU_RELATION);
$criteria->add(ApplicationPeer::APP_UID, $appUid);
$criteria->addJoin(ApplicationPeer::PRO_UID, TaskPeer::PRO_UID, Criteria::LEFT_JOIN);
$criteria->addJoin(TaskPeer::TAS_UID, TaskUserPeer::TAS_UID, Criteria::LEFT_JOIN);
$rs = ApplicationPeer::doSelectRS($criteria);
$rs->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$userPermission = array();
$user = new Users();
while ($rs->next()) {
$row = $rs->getRow();
if ($row['TU_RELATION'] == 1) {
//users
$dataUser = $user->load($row['USR_UID']);
if (array_search($dataUser['USR_EMAIL'], $userPermission) === false) {
$objectPermissions = $this->getAllObjects($row['PRO_UID'], $appUid, $row['TAS_UID'],
$row['USR_UID']);
$userPermission[] = $dataUser['USR_EMAIL'];
}
} else {
//Groups
$criteria = new Criteria('workflow');
$criteria->addSelectColumn(UsersPeer::USR_EMAIL);
$criteria->addSelectColumn(UsersPeer::USR_UID);
$criteria->add(GroupUserPeer::GRP_UID, $row['USR_UID']);
$criteria->addJoin(GroupUserPeer::USR_UID, UsersPeer::USR_UID, Criteria::LEFT_JOIN);
$rsGroup = AppDelegationPeer::doSelectRS($criteria);
$rsGroup->setFetchmode(ResultSet::FETCHMODE_ASSOC);
while ($rsGroup->next()) {
$aRow = $rsGroup->getRow();
if (array_search($aRow['USR_EMAIL'], $userPermission) === false) {
$objectPermissions = $this->getAllObjects($row['PRO_UID'], $appUid,
$row['TAS_UID'], $aRow['USR_UID']);
$userPermission[] = $aRow['USR_EMAIL'];
}
}
}
}
$userPermission = array_unique($userPermission);
foreach ($userPermission as $key => $val) {
$this->drive->setPermission($folderUid, $val, 'user', 'writer');
$this->drive->setPermission($fileIdDrive, $val);
}
}
public function addUserEmail ($email)
{
if (empty($email)) {
return;
}
if ($this->usersEmail == '') {
$this->usersEmail = $email;
} else {
$emails = explode('|', $this->usersEmail);
if (array_search($email, $emails) === false) {
$this->usersEmail .= '|' . $email;
}
}
}
/**
* Get email of task users to app_uid
* @param $appUid id application
*
* @throws \Exception
*/
public function getEmailUsersTask($appUid)
{
try {
$criteria = new Criteria('workflow');
$criteria->add(AppDelegationPeer::APP_UID, $appUid);
$criteria->add(AppDelegationPeer::DEL_THREAD_STATUS, 'OPEN');
$rsAppDelegation = AppDelegationPeer::doSelectRS($criteria);
$rsAppDelegation->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$group = new Groups();
$user = new Users();
$data = [];
while ($rsAppDelegation->next()) {
$row = $rsAppDelegation->getRow();
if (!empty($row['USR_UID'])) {
if ($user->userExists($row['USR_UID'])) {
$data = [];
$data[] = $user->load($row['USR_UID']);
} else {
$data = $group->getUsersOfGroup($row['USR_UID']);
}
foreach ($data as $dataUser) {
$this->addUserEmail($dataUser["USR_EMAIL"]);
}
}
}
} catch (Exception $exception) {
error_log('Error: ' . $exception);
}
}
/**
* @param array $appDocument
* @param string $typeDocument type document INPUT, OUTPUT_DOC, OUTPUT_PDF, ATTACHED
* @param string $mime MIME type of the file to insert.
* @param string $src location of the file to insert.
* @param string $name Title of the file to insert, including the extension.
* return string uid
*/
public function upload ($appDocument, $typeDocument, $mime, $src, $name)
{
try
{
$idFileDrive = null;
$this->existAppFolderDrive();
$appDoc = new AppDocument();
$result = $this->drive->uploadFile(
$mime,
$src,
$name,
$this->app->getAppDriveFolderUid()
);
if ($result->id !== null) {
$idFileDrive = $result->id;
$appDoc->setDriveDownload($typeDocument, $result->id);
$appDoc->update($appDocument);
}
return $idFileDrive;
} catch (Exception $e) {
error_log('Error upload file drive: ' . $e->getMessage());
}
}
/**
* Download file drive
* @param $uidFileDrive
*/
public function download ($uidFileDrive)
{
try
{
$result = $this->drive->downloadFile($uidFileDrive);
} catch (Exception $e) {
error_log('Error Download file drive: ' . $e->getMessage());
}
return $result;
}
/**
* @param array $data
* @param string $typeDoc value INPUT, OUTPUT_DOC, OUTPUT_PDF, ATTACHED
*
* @return string url drive
*/
public function changeUrlDrive ($data, $typeDoc)
{
try
{
$urlDrive = $data['APP_DOC_DRIVE_DOWNLOAD'];
if ($this->getStatusDrive()) {
$driveDownload = @unserialize($data['APP_DOC_DRIVE_DOWNLOAD']);
$urlDrive = $driveDownload !== false
&& is_array($driveDownload)
&& array_key_exists($typeDoc, $driveDownload) ?
$driveDownload[$typeDoc] : $urlDrive;
}
} catch (Exception $e) {
error_log('Error change url drive: ' . $e->getMessage());
}
return $urlDrive;
}
/**
* Synchronize documents drive
*
* @param boolean $log enable print cron
*/
public function synchronizeDrive ($log)
{
if (!$this->statusDrive) {
error_log("It has not enabled Feature Gmail");
return;
}
$criteria = new Criteria('workflow');
$criteria->addSelectColumn(AppDocumentPeer::APP_DOC_UID);
$criteria->addSelectColumn(AppDocumentPeer::DOC_VERSION);
$criteria->add(
$criteria->getNewCriterion(AppDocumentPeer::SYNC_WITH_DRIVE, 'UNSYNCHRONIZED', Criteria::EQUAL)->
addOr($criteria->getNewCriterion(AppDocumentPeer::SYNC_PERMISSIONS, null, Criteria::NOT_EQUAL))
);
$criteria->add(AppDocumentPeer::APP_DOC_STATUS, 'ACTIVE');
$criteria->addAscendingOrderByColumn('APP_DOC_CREATE_DATE');
$criteria->addAscendingOrderByColumn('APP_UID');
$rs = AppDocumentPeer::doSelectRS($criteria);
$rs->setFetchMode(ResultSet::FETCHMODE_ASSOC);
while ($rs->next()) {
$row = $rs->getRow();
$appDoc = new AppDocument();
$fields = $appDoc->load($row['APP_DOC_UID'], $row['DOC_VERSION']);
$appDocUid = $appDoc->getAppDocUid();
$docVersion = $appDoc->getDocVersion();
$filename = pathinfo($appDoc->getAppDocFilename());
$name = !empty($filename['basename']) ? $filename['basename'] : '';
$ext = !empty($filename['extension']) ? $filename['extension'] : '';
$appUid = G::getPathFromUID($appDoc->getAppUid());
$file = G::getPathFromFileUID($appDoc->getAppUid(), $appDocUid);
$sw_file_exists_doc = false;
$sw_file_exists_pdf = false;
$sw_file_exists = false;
$realPath = '';
if ($appDoc->getAppDocType() === 'OUTPUT') {
$realPathDoc = PATH_DOCUMENT . $appUid . '/outdocs/' . $appDocUid . '_' . $docVersion . '.' . 'doc';
$realPathDoc1 = PATH_DOCUMENT . $appUid . '/outdocs/' . $name . '_' . $docVersion . '.' . 'doc';
$realPathDoc2 = PATH_DOCUMENT . $appUid . '/outdocs/' . $name . '.' . 'doc';
if (file_exists($realPathDoc)) {
$sw_file_exists = true;
$sw_file_exists_doc = true;
} elseif (file_exists($realPathDoc1)) {
$sw_file_exists = true;
$sw_file_exists_doc = true;
$realPathDoc = $realPathDoc1;
} elseif (file_exists($realPathDoc2)) {
$sw_file_exists = true;
$sw_file_exists_doc = true;
$realPathDoc = $realPathDoc2;
}
$realPathPdf = PATH_DOCUMENT . $appUid . '/outdocs/' . $appDocUid . '_' . $docVersion . '.' . 'pdf';
$realPathPdf1 = PATH_DOCUMENT . $appUid . '/outdocs/' . $name . '_' . $docVersion . '.' . 'pdf';
$realPathPdf2 = PATH_DOCUMENT . $appUid . '/outdocs/' . $name . '.' . 'pdf';
if (file_exists($realPathPdf)) {
$sw_file_exists = true;
$sw_file_exists_pdf = true;
} elseif (file_exists($realPathPdf1)) {
$sw_file_exists = true;
$sw_file_exists_pdf = true;
$realPathPdf = $realPathPdf1;
} elseif (file_exists($realPathPdf2)) {
$sw_file_exists = true;
$sw_file_exists_pdf = true;
$realPathPdf = $realPathPdf2;
}
} else {
$realPath = PATH_DOCUMENT . $appUid . '/' . $file[0] . $file[1] . '_' . $docVersion . '.' . $ext;
$realPath1 = PATH_DOCUMENT . $appUid . '/' . $file[0] . $file[1] . '.' . $ext;
if (file_exists($realPath)) {
$sw_file_exists = true;
} elseif (file_exists($realPath1)) {
$sw_file_exists = true;
$realPath = $realPath1;
}
}
if ($sw_file_exists) {
$this->loadApplication($appDoc->getAppUid());
$this->loadUser($fields['USR_UID']);
$emails = $appDoc->getSyncPermissions();
$emails = !empty($emails) ? explode('|', $emails) : array();
$result = null;
foreach ($emails as $index => $email) {
if (!empty($email)) {
if ($index == 0 && $fields['SYNC_WITH_DRIVE'] == 'UNSYNCHRONIZED') {
if ($log) {
eprintln('upload file:' . $name, 'green');
}
$this->drive->setDriveUser($email);
if ($appDoc->getAppDocType() == 'OUTPUT') {
if ($sw_file_exists_doc) {
$nameDoc = !empty($name)? $name : array_pop(explode('/', $realPathDoc));
$result = $this->upload($fields, 'OUTPUT_DOC', 'application/msword', $realPathDoc,
$nameDoc);
}
if ($sw_file_exists_pdf) {
$namePdf = !empty($name)? $name : array_pop(explode('/', $realPathPdf));
$info = finfo_open(FILEINFO_MIME_TYPE);
$result = $this->upload($fields, 'OUTPUT_PDF', finfo_file($info, $realPathPdf),
$realPathPdf, $namePdf);
}
} else {
$info = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($info, $realPath);
$type = $appDoc->getAppDocType();
$result = $this->upload($fields, $type, $mime, $realPath, $name);
}
} else {
$result = $this->drive->setDriveUser($this->user->getUsrEmail());
}
if ($log) {
eprintln('Set Permission:' . $email, 'green');
}
$result = $this->drive->setPermission($this->app->getAppDriveFolderUid(), $email, 'user', 'writer');
$fields['SYNC_PERMISSIONS'] = null;
}
}
if ($result != null) {
$fields['SYNC_WITH_DRIVE'] = 'SYNCHRONIZED';
$fields['SYNC_PERMISSIONS'] = null;
}
} else {
$fields['SYNC_WITH_DRIVE'] = 'NO_EXIST_FILE_PM';
if ($log) {
eprintln('File no exists:' . $name, 'red');
}
}
$appDoc->update($fields);
}
}
/**
* Add users to documents drive to give permissions
*
* @param $appUid Id application
* @param $emails array with emails which can be null
*
* @throws \Exception
*/
public function addUsersDocumentDrive ($appUid, $emails=null)
{
if (is_array($emails)) {
foreach ($emails as $index => $email) {
$this->addUserEmail($email);
}
} else {
$this->getEmailUsersTask($appUid);
}
$criteria = new Criteria( 'workflow' );
$criteria->add( AppDocumentPeer::APP_UID, $appUid );
$criteria->addAscendingOrderByColumn( 'DOC_VERSION' );
$rs = AppDocumentPeer::doSelectRS( $criteria );
$rs->setFetchmode( ResultSet::FETCHMODE_ASSOC );
$appDoc = new AppDocument();
while ($rs->next()) {
$row = $rs->getRow();
if (empty($row['SYNC_PERMISSIONS'])) {
$row['SYNC_PERMISSIONS'] = $this->usersEmail;
} else {
$emails = explode('|', $row['SYNC_PERMISSIONS']);
foreach ($emails as $email) {
$this->addUserEmail($email);
}
}
$appDoc->update($row);
}
}
} |