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 = isset($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  if (isset($this->announcementId)) {
121  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
122  $announcement = $announcementDao->getById($this->announcementId);
123 
124  if ($announcement != null) {
125  $this->_data = array(
126  'typeId' => $announcement->getTypeId(),
127  'assocType' => $announcement->getAssocType(),
128  'assocId' => $announcement->getAssocId(),
129  'title' => $announcement->getTitle(null), // Localized
130  'descriptionShort' => $announcement->getDescriptionShort(null), // Localized
131  'description' => $announcement->getDescription(null), // Localized
132  'dateExpire' => $announcement->getDateExpire()
133  );
134  } else {
135  $this->announcementId = null;
136  }
137  }
138  }
139 
143  function readInputData() {
144  $this->readUserVars(array('typeId', 'title', 'descriptionShort', 'description', 'dateExpireYear', 'dateExpireMonth', 'dateExpireDay', 'dateExpire'));
145  }
146 
151  function execute($request) {
152  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
153 
154  if (isset($this->announcementId)) {
155  $announcement = $announcementDao->getById($this->announcementId);
156  }
157 
158  if (!isset($announcement)) {
159  $announcement = $announcementDao->newDataObject();
160  }
161 
162  $announcement->setAssocType(Application::getContextAssocType());
163  $announcement->setAssocId($this->getContextId());
164 
165  $announcement->setTitle($this->getData('title'), null); // Localized
166  $announcement->setDescriptionShort($this->getData('descriptionShort'), null); // Localized
167  $announcement->setDescription($this->getData('description'), null); // Localized
168 
169  if ($this->getData('typeId') != null) {
170  $announcement->setTypeId($this->getData('typeId'));
171  } else {
172  $announcement->setTypeId(null);
173  }
174 
175  // Give the parent class a chance to set the dateExpire.
176  $dateExpireSetted = $this->setDateExpire($announcement);
177  if (!$dateExpireSetted) {
178  if ($this->getData('dateExpireYear') != null) {
179  $announcement->setDateExpire($this->getData('dateExpire'));
180  } else {
181  $announcement->setDateExpire(null);
182  }
183  }
184 
185  // Update or insert announcement
186  if ($announcement->getId() != null) {
187  $announcementDao->updateObject($announcement);
188  } else {
189  $announcement->setDatetimePosted(Core::getCurrentDate());
190  $announcementDao->insertObject($announcement);
191  }
192 
193  $contextId = $this->getContextId();
194 
195  // Send a notification to associated users
196  import('classes.notification.NotificationManager');
197  $notificationManager = new NotificationManager();
198  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
199  $notificationUsers = array();
200  $allUsers = $userGroupDao->getUsersByContextId($contextId);
201  while ($user = $allUsers->next()) {
202  $notificationUsers[] = array('id' => $user->getId());
203  }
204  foreach ($notificationUsers as $userRole) {
205  $notificationManager->createNotification(
206  $request, $userRole['id'], NOTIFICATION_TYPE_NEW_ANNOUNCEMENT,
207  $contextId, ASSOC_TYPE_ANNOUNCEMENT, $announcement->getId()
208  );
209  }
210  $notificationManager->sendToMailingList($request,
211  $notificationManager->createNotification(
212  $request, UNSUBSCRIBED_USER_NOTIFICATION, NOTIFICATION_TYPE_NEW_ANNOUNCEMENT,
213  $contextId, ASSOC_TYPE_ANNOUNCEMENT, $announcement->getId()
214  )
215  );
216  return $announcement->getId();
217  }
218 
219 
220  //
221  // Protected methods.
222  //
227  function setDateExpire($announcement) {
228  $dateExpire = $this->getData('dateExpire');
229  if ($dateExpire) {
230  $announcement->setDateExpire(DAO::formatDateToDB($dateExpire, null, false));
231  } else {
232  // No date passed but null is acceptable for
233  // announcements.
234  $announcement->setDateExpire(null);
235  }
236  return true;
237  }
238 }
239 
240 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:397
formatDateToDB($date, $defaultNumWeeks=null, $acceptPastDate=true)
Definition: DAO.inc.php:635
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