Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
AnnouncementForm.inc.php
1 <?php
16 import('lib.pkp.classes.manager.form.PKPAnnouncementForm');
17 
19 
21  var $_readOnly;
22 
29  function AnnouncementForm($pressId, $announcementId = null, $readOnly = false) {
30  parent::PKPAnnouncementForm($pressId, $announcementId);
31 
32  $this->_readOnly = $readOnly;
33 
34  // Validate date expire.
35  $this->addCheck(new FormValidatorCustom($this, 'dateExpire', 'optional', 'manager.announcements.form.dateExpireValid', create_function('$dateExpire', '$today = getDate(); $todayTimestamp = mktime(0, 0, 0, $today[\'mon\'], $today[\'mday\'], $today[\'year\']); return (strtotime($dateExpire) > $todayTimestamp);')));
36 
37  // If provided, announcement type is valid
38  $this->addCheck(new FormValidatorCustom($this, 'typeId', 'optional', 'manager.announcements.form.typeIdValid', create_function('$typeId, $pressId', '$announcementTypeDao = DAORegistry::getDAO(\'AnnouncementTypeDAO\'); if((int)$typeId === 0) { return true; } else { return $announcementTypeDao->announcementTypeExistsByTypeId($typeId, ASSOC_TYPE_PRESS, $pressId);}'), array($pressId)));
39  }
40 
41 
42  //
43  // Getters and Setters
44  //
49  function isReadOnly() {
50  return $this->_readOnly;
51  }
52 
53 
54  //
55  // Extended methods from Form
56  //
61  function fetch($request) {
62  $templateMgr = TemplateManager::getManager($request);
63  $templateMgr->assign('readOnly', $this->isReadOnly());
64  $templateMgr->assign('selectedTypeId', $this->getData('typeId'));
65 
66  $announcementDao = DAORegistry::getDAO('AnnouncementDAO');
67  $announcement = $announcementDao->getById($this->announcementId);
68  $templateMgr->assign_by_ref('announcement', $announcement);
69 
70  $announcementTypeDao = DAORegistry::getDAO('AnnouncementTypeDAO');
71  list($assocType, $assocId) = $this->_getAnnouncementTypesAssocId();
72  $announcementTypeFactory = $announcementTypeDao->getByAssoc($assocType, $assocId);
73 
74  $announcementTypeOptions = array();
75  if (!$announcementTypeFactory->wasEmpty()) {
76  $announcementTypeOptions = array(0 => __('common.none'));
77  }
78  while ($announcementType = $announcementTypeFactory->next()) {
79  $announcementTypeOptions[$announcementType->getId()] = $announcementType->getLocalizedTypeName();
80  }
81  $templateMgr->assign('announcementTypes', $announcementTypeOptions);
82 
83  return parent::fetch($request, 'controllers/grid/announcements/form/announcementForm.tpl');
84  }
85 
86  //
87  // Extended methods from PKPAnnouncementForm
88  //
92  function readInputData() {
93  parent::readInputData();
94  $this->readUserVars(array('dateExpire'));
95  }
96 
101  function execute($request) {
102  $announcement = parent::execute();
103  $pressId = $this->getContextId();
105  // Send a notification to associated users
106  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
107  $notificationUsers = array();
108  $allUsers = $userGroupDao->getUsersByContextId($pressId);
109  while (!$allUsers->eof()) {
110  $user = $allUsers->next();
111  $notificationUsers[] = array('id' => $user->getId());
112  }
113  $notificationManager = new NotificationManager();
114  foreach ($notificationUsers as $userRole) {
115  $notificationManager->createNotification(
116  $request, $userRole['id'], NOTIFICATION_TYPE_NEW_ANNOUNCEMENT,
117  $pressId, ASSOC_TYPE_ANNOUNCEMENT, $announcement->getId()
118  );
119  }
120  $notificationManager->sendToMailingList($request,
121  $notificationManager->createNotification(
122  $request, UNSUBSCRIBED_USER_NOTIFICATION, NOTIFICATION_TYPE_NEW_ANNOUNCEMENT,
123  $pressId, ASSOC_TYPE_ANNOUNCEMENT, $announcement->getId()
124  )
125  );
126 
127  return $announcement->getId();
128  }
129 
130 
131  //
132  // Implement protected methods from PKPAnnouncementForm.
133  //
137  function setDateExpire($announcement) {
138  /* @var $announcement Announcement */
139  $dateExpire = $this->getData('dateExpire');
140  if ($dateExpire) {
141  $announcement->setDateExpire(DAO::formatDateToDB($dateExpire, null, false));
142  } else {
143  // No date passed but null is acceptable for
144  // announcements.
145  $announcement->setDateExpire(null);
146  }
147  return true;
148  }
149 
150 
151  //
152  // Private helper methdos.
153  //
157  function _getAnnouncementTypesAssocId() {
158  $pressId = $this->getContextId();
159  return array(ASSOC_TYPE_PRESS, $pressId);
160  }
161 
166  function _setAnnouncementAssocId($announcement) {
167  $pressId = $this->getContextId();
168  $announcement->setAssocType(ASSOC_TYPE_PRESS);
169  $announcement->setAssocId($pressId);
170  }
171 }
172 
173 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
formatDateToDB($date, $defaultNumWeeks=null, $acceptPastDate=true)
Definition: DAO.inc.php:635
Form validation check with a custom user function performing the validation check.
readUserVars($vars)
Definition: Form.inc.php:364
getData($key)
Definition: Form.inc.php:210
setDateExpire($announcement)
AnnouncementForm($pressId, $announcementId=null, $readOnly=false)
_setAnnouncementAssocId($announcement)
Form for to read/create/edit announcements.
Form for managers to create/edit announcements.