PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ow_plugins/skeleton/classes/notification_example_form.php

https://bitbucket.org/Noelfhim/no_ftp
PHP | 153 lines | 101 code | 35 blank | 17 comment | 6 complexity | e9c319d6a3d6a83df966dc47500c1211 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. class SKELETON_CLASS_NotificationExampleForm extends Form
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct('NotificationExampleForm');
  7. $language = OW::getLanguage();
  8. //Simple text field
  9. $content = new TextField("content");
  10. $content->setLabel($language->text("skeleton", "notification_content"));
  11. $content->setHasInvitation(true);
  12. $content->setInvitation($language->text("skeleton", "notification_content_invitation"));
  13. $content->setRequired();
  14. $this->addElement($content);
  15. //File upload field
  16. $image = new ImageField("image");
  17. $image->setLabel($language->text("skeleton", "notification_attach_image"));
  18. $image->addValidator(new ImageValidator());
  19. $this->addElement($image);
  20. //Send button
  21. $send = new Submit("send");
  22. $send->setValue($language->text("skeleton", "send"));
  23. //$this->setAjax();
  24. $this->setEnctype(Form::ENCTYPE_MULTYPART_FORMDATA);
  25. $this->addElement($send);
  26. }
  27. public function process()
  28. {
  29. $data = $this->getValues();
  30. $userId = OW::getUser()->getId();
  31. $avatars = BOL_AvatarService::getInstance()->getAvatarsUrlList(array($userId));
  32. $userUrls = BOL_UserService::getInstance()->getUserUrlsForList(array($userId));
  33. $notificationParams = array(
  34. 'pluginKey' => 'skeleton',
  35. 'action' => 'example',
  36. 'entityType' => 'skeleton-example',
  37. 'entityId' => OW::getUser()->getId(),
  38. 'userId' => OW::getUser()->getId(),
  39. 'time' => time()
  40. );
  41. /**
  42. * avatar - is an image of the user or entity that initiated notification
  43. * url - is an URL to the entity that initiated notification
  44. * string - language key with variables or text of the notification
  45. */
  46. $notificationData = array(
  47. 'string' => array(
  48. 'key' => 'skeleton+notify_example',
  49. 'vars' => array(
  50. 'content' => $data['content']
  51. )
  52. ),
  53. 'avatar' => $avatars[$userId],
  54. 'url' => $userUrls[$userId]
  55. );
  56. if (isset($_FILES['image']['error']) && $_FILES['image']['error'] == UPLOAD_ERR_OK)
  57. {
  58. //get configured file storage (Cloud files or file system drive, depends on settings in config file)
  59. $storage = OW::getStorage();
  60. $imagesDir = OW::getPluginManager()->getPlugin('skeleton')->getUserFilesDir();
  61. $imagePath = $imagesDir . $_FILES['image']['name'];
  62. if ( $storage->fileExists($imagePath) )
  63. {
  64. $storage->removeFile($imagePath);
  65. }
  66. $pluginfilesDir = Ow::getPluginManager()->getPlugin('skeleton')->getPluginFilesDir();
  67. $tmpImgPath = $pluginfilesDir . 'notification_' .uniqid() . '.jpg';
  68. $image = new UTIL_Image($_FILES['image']['tmp_name']);
  69. $image->resizeImage(200, null)->saveImage($tmpImgPath);
  70. unlink($_FILES['image']['tmp_name']);
  71. //Copy file into storage folder
  72. $storage->copyFile($tmpImgPath, $imagePath);
  73. unlink($tmpImgPath);
  74. $imagesUrl = OW::getPluginManager()->getPlugin('skeleton')->getUserFilesUrl();
  75. $notificationData['contentImage'] = $imagesUrl . $_FILES['image']['name'];
  76. }
  77. //Adding notification on action
  78. $event = new OW_Event('notifications.add', $notificationParams, $notificationData);
  79. OW::getEventManager()->trigger($event);
  80. }
  81. }
  82. class ImageField extends FileField
  83. {
  84. public function getValue()
  85. {
  86. return empty($_FILES[$this->getName()]['tmp_name']) ? null : $_FILES[$this->getName()];
  87. }
  88. }
  89. class ImageValidator extends OW_Validator
  90. {
  91. public function __construct()
  92. {
  93. }
  94. /**
  95. * @see OW_Validator::isValid()
  96. *
  97. * @param mixed $value
  98. */
  99. public function isValid( $value )
  100. {
  101. if ( empty($value) )
  102. {
  103. return true;
  104. }
  105. $realName = $value['name'];
  106. $tmpName = $value['tmp_name'];
  107. switch ( false )
  108. {
  109. case is_uploaded_file($tmpName):
  110. $this->setErrorMessage(OW::getLanguage()->text('base', 'upload_file_fail'));
  111. return false;
  112. case UTIL_File::validateImage($realName):
  113. $this->setErrorMessage(OW::getLanguage()->text('skeleton', 'errors_image_invalid'));
  114. return false;
  115. }
  116. return true;
  117. }
  118. }