Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
AnnouncementForm.inc.php
1 <?php
2 
17 import('lib.pkp.classes.form.Form');
18 
19 class AnnouncementForm extends Form {
21  var $_readOnly;
22 
24  var $announcementId;
25 
27  var $_contextId;
28 
35  function AnnouncementForm($contextId, $announcementId = null, $readOnly = false) {
36 
37  $this->_readOnly = $readOnly;
38  $this->_contextId = $contextId;
39  $this->announcementId = $announcementId?(int)$announcementId:null;
40  parent::Form('manager/announcement/announcementForm.tpl');
41 
42  // Title is provided
43  $this->addCheck(new FormValidatorLocale($this, 'title', 'required', 'manager.announcements.form.titleRequired'));
44 
45  // Short description is provided
46  $this->addCheck(new FormValidatorLocale($this, 'descriptionShort', 'required', 'manager.announcements.form.descriptionShortRequired'));
47 
48  // Description is provided
49  $this->addCheck(new FormValidatorLocale($this, 'description', 'optional', 'manager.announcements.form.descriptionRequired'));
50 
51  // If provided, announcement type is valid
52  $this->addCheck(new FormValidatorCustom($this, 'typeId', 'optional', 'manager.announcements.form.typeIdValid', create_function('$typeId, $contextId', '$announcementTypeDao = DAORegistry::getDAO(\'AnnouncementTypeDAO\'); if((int)$typeId === 0) { return true; } else { return $announcementTypeDao->announcementTypeExistsByTypeId($typeId, Application::getContextAssocType(), $contextId);}'), array($contextId)));
53 
54  $this->addCheck(new FormValidatorPost($this));
55  }
56 
57 
58  //
59  // Getters and setters.
60  //
64  function isReadOnly() {
65  return $this->_readOnly;
66  }
67 
72  function getContextId() {
73  return $this->_contextId;
74  }
75 
76 
77  //
78  // Extended methods from Form.
79  //
84  function getLocaleFieldNames() {
85  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
86  return $announcementDao->getLocaleFieldNames();
87  }
88 
92  function fetch($request) {
93  $templateMgr = TemplateManager::getManager($request);
94  $templateMgr->assign('readOnly', $this->isReadOnly());
95  $templateMgr->assign('selectedTypeId', $this->getData('typeId'));
96 
97  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
98  $announcement = $announcementDao->getById($this->announcementId);
99  $templateMgr->assign('announcement', $announcement);
100 
101  $announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO');
102  $announcementTypeFactory = $announcementTypeDao->getByAssoc(Application::getContextAssocType(), $this->getContextId());
103 
104  $announcementTypeOptions = array();
105  if (!$announcementTypeFactory->wasEmpty()) {
106  $announcementTypeOptions = array(0 => __('common.none'));
107  }
108  while ($announcementType = $announcementTypeFactory->next()) {
109  $announcementTypeOptions[$announcementType->getId()] = $announcementType->getLocalizedTypeName();
110  }
111  $templateMgr->assign('announcementTypes', $announcementTypeOptions);
112 
113  return parent::fetch($request, 'controllers/grid/announcements/form/announcementForm.tpl');
114  }
115 
119  function initData() {
120  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
121  $announcement = $announcementDao->getById($this->announcementId);
122 
123  if ($announcement) {
124  $this->_data = array(
125  'typeId' => $announcement->getTypeId(),
126  'assocType' => $announcement->getAssocType(),
127  'assocId' => $announcement->getAssocId(),
128  'title' => $announcement->getTitle(null), // Localized
129  'descriptionShort' => $announcement->getDescriptionShort(null), // Localized
130  'description' => $announcement->getDescription(null), // Localized
131  'dateExpire' => $announcement->getDateExpire()
132  );
133  } else {
134  $this->announcementId = null;
135  }
136  }
137 
141  function readInputData() {
142  $this->readUserVars(array('typeId', 'title', 'descriptionShort', 'description', 'dateExpireYear', 'dateExpireMonth', 'dateExpireDay', 'dateExpire'));
143  }
144 
149  function execute($request) {
150  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
151 
152  $announcement = $announcementDao->getById($this->announcementId);
153  if (!$announcement) {
154  $announcement = $announcementDao->newDataObject();
155  }
156 
157  $announcement->setAssocType(Application::getContextAssocType());
158  $announcement->setAssocId($this->getContextId());
159 
160  $announcement->setTitle($this->getData('title'), null); // Localized
161  $announcement->setDescriptionShort($this->getData('descriptionShort'), null); // Localized
162  $announcement->setDescription($this->getData('description'), null); // Localized
163 
164  if ($this->getData('typeId')) {
165  $announcement->setTypeId($this->getData('typeId'));
166  } else {
167  $announcement->setTypeId(null);
168  }
169 
170  // Give the parent class a chance to set the dateExpire.
171  $dateExpireSetted = $this->setDateExpire($announcement);
172  if (!$dateExpireSetted) {
173  if ($this->getData('dateExpireYear') != null) {
174  $announcement->setDateExpire($this->getData('dateExpire'));
175  } else {
176  $announcement->setDateExpire(null);
177  }
178  }
179 
180  // Update or insert announcement
181  if ($announcement->getId()) {
182  $announcementDao->updateObject($announcement);
183  } else {
184  $announcement->setDatetimePosted(Core::getCurrentDate());
185  $announcementDao->insertObject($announcement);
186  }
187 
188  $contextId = $this->getContextId();
189 
190  // Send a notification to associated users
191  import('classes.notification.NotificationManager');
192  $notificationManager = new NotificationManager();
193  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
194  $notificationUsers = array();
195  $allUsers = $userGroupDao->getUsersByContextId($contextId);
196  while ($user = $allUsers->next()) {
197  $notificationUsers[] = array('id' => $user->getId());
198  }
199  if (!$this->announcementId) { // Only for new announcements
200  foreach ($notificationUsers as $userRole) {
201  $notificationManager->createNotification(
202  $request, $userRole['id'], NOTIFICATION_TYPE_NEW_ANNOUNCEMENT,
203  $contextId, ASSOC_TYPE_ANNOUNCEMENT, $announcement->getId()
204  );
205  }
206  $notificationManager->sendToMailingList($request,
207  $notificationManager->createNotification(
208  $request, UNSUBSCRIBED_USER_NOTIFICATION, NOTIFICATION_TYPE_NEW_ANNOUNCEMENT,
209  $contextId, ASSOC_TYPE_ANNOUNCEMENT, $announcement->getId()
210  )
211  );
212  }
213  return $announcement->getId();
214  }
215 
216 
217  //
218  // Protected methods.
219  //
224  function setDateExpire($announcement) {
225  $dateExpire = $this->getData('dateExpire');
226  if ($dateExpire) {
227  $announcement->setDateExpire(DAO::formatDateToDB($dateExpire, null, false));
228  } else {
229  // No date passed but null is acceptable for
230  // announcements.
231  $announcement->setDateExpire(null);
232  }
233  return true;
234  }
235 }
236 
237 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:397
formatDateToDB($date, $defaultNumWeeks=null, $acceptPastDate=true)
Definition: DAO.inc.php:661
Form validation check with a custom user function performing the validation check.
AnnouncementForm($contextId, $announcementId=null, $readOnly=false)
Form validation check to make sure the form is POSTed.
readUserVars($vars)
Definition: Form.inc.php:365
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:48
getData($key)
Definition: Form.inc.php:211
setDateExpire($announcement)
Class to represent a form validation check for localized fields.
static getContextAssocType()
Form for managers to create/edit announcements.
static getCurrentDate($ts=null)
Definition: Core.inc.php:95