Open Monograph Press  3.3.0
SpotlightForm.inc.php
1 <?php
16 import('lib.pkp.classes.form.Form');
17 
18 class SpotlightForm extends Form {
22  var $_spotlightId;
23 
27  var $_pressId;
28 
34  public function __construct($pressId, $spotlightId = null) {
35  parent::__construct('controllers/grid/content/spotlights/form/spotlightForm.tpl');
36 
37  $this->_spotlightId = $spotlightId;
38  $this->_pressId = $pressId;
39 
40  $form = $this;
41  $this->addCheck(new FormValidatorCustom($this, 'assocId', 'required', 'grid.content.spotlights.itemRequired', function($assocId) use ($form) {
42  list($id, $type) = preg_split('/:/', $assocId);
43  return is_numeric($id) && $id > 0 && $form->_isValidSpotlightType($type);
44  }));
45  $this->addCheck(new FormValidator($this, 'title', 'required', 'grid.content.spotlights.titleRequired'));
46  $this->addCheck(new FormValidatorPost($this));
47  $this->addCheck(new FormValidatorCSRF($this));
48  }
49 
50 
51  //
52  // Extended methods from Form
53  //
57  public function fetch($request, $template = null, $display = false) {
58  $templateMgr = TemplateManager::getManager($request);
59 
60  $spotlightDao = DAORegistry::getDAO('SpotlightDAO'); /* @var $spotlightDao SpotlightDAO */
61  $spotlight = $spotlightDao->getById($this->getSpotlightId());
62  $templateMgr->assign(array(
63  'spotlight' => $spotlight,
64  'pressId' => $this->getPressId()
65  ));
66 
67  if (isset($spotlight)) $templateMgr->assign(array(
68  'title' => $spotlight->getTitle(null),
69  'description' => $spotlight->getDescription(null),
70  'assocTitle' => $this->getAssocTitle($spotlight->getAssocId(), $spotlight->getAssocType()),
71  'assocId' => $spotlight->getAssocId() . ':' . $spotlight->getAssocType(),
72  ));
73 
74  return parent::fetch($request, $template, $display);
75  }
76 
77  //
78  // Extended methods from Form
79  //
83  public function readInputData() {
84  $this->readUserVars(array('title', 'description', 'assocId'));
85  }
86 
90  public function execute(...$functionArgs) {
91  $spotlightDao = DAORegistry::getDAO('SpotlightDAO'); /* @var $spotlightDao SpotlightDAO */
92 
93  $spotlight = $spotlightDao->getById($this->getSpotlightId(), $this->getPressId());
94 
95  if (!$spotlight) {
96  // this is a new spotlight
97  $spotlight = $spotlightDao->newDataObject();
98  $spotlight->setPressId($this->getPressId());
99  $existingSpotlight = false;
100  } else {
101  $existingSpotlight = true;
102  }
103 
104  list($assocId, $assocType) = preg_split('/:/', $this->getData('assocId'));
105  $spotlight->setAssocType($assocType);
106  $spotlight->setTitle($this->getData('title'), null); // localized
107  $spotlight->setDescription($this->getData('description'), null); // localized
108  $spotlight->setAssocId($assocId);
109 
110  if ($existingSpotlight) {
111  $spotlightDao->updateObject($spotlight);
112  $spotlightId = $spotlight->getId();
113  } else {
114  $spotlightId = $spotlightDao->insertObject($spotlight);
115  }
116 
117  parent::execute(...$functionArgs);
118  return $spotlightId;
119  }
120 
121 
122  //
123  // helper methdods.
124  //
125 
130  public function getSpotlightId() {
131  return $this->_spotlightId;
132  }
133 
138  public function getPressId() {
139  return $this->_pressId;
140  }
141 
147  public function getAssocTitle($assocId, $assocType) {
148 
149  $returner = null;
150  switch ($assocType) {
151  case SPOTLIGHT_TYPE_BOOK:
152  $submission = Services::get('submission')->get($assocId);
153  $returner = isset($submission) ? $submission->getLocalizedTitle() : '';
154  break;
155  case SPOTLIGHT_TYPE_SERIES:
156  $seriesDao = DAORegistry::getDAO('SeriesDAO'); /* @var $seriesDao SeriesDAO */
157  $series = $seriesDao->getById($assocId, $this->getPressId());
158  $returner = isset($series) ? $series->getLocalizedTitle() : '';
159  break;
160  default:
161  fatalError('invalid type specified');
162  }
163  return $returner;
164  }
165 
171  public function _isValidSpotlightType($type) {
172  $validTypes = array(SPOTLIGHT_TYPE_BOOK, SPOTLIGHT_TYPE_SERIES);
173  return in_array((int) $type, $validTypes);
174  }
175 }
176 
177 
SpotlightForm\getAssocTitle
getAssocTitle($assocId, $assocType)
Definition: SpotlightForm.inc.php:153
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
SpotlightForm\getSpotlightId
getSpotlightId()
Definition: SpotlightForm.inc.php:136
SpotlightForm\_isValidSpotlightType
_isValidSpotlightType($type)
Definition: SpotlightForm.inc.php:177
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
SpotlightForm\$_pressId
$_pressId
Definition: SpotlightForm.inc.php:33
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
SpotlightForm\$_spotlightId
$_spotlightId
Definition: SpotlightForm.inc.php:25
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
SpotlightForm\execute
execute(... $functionArgs)
Definition: SpotlightForm.inc.php:96
SpotlightForm
Form for reading/creating/editing spotlight items.
Definition: SpotlightForm.inc.php:18
FormValidatorCSRF
Form validation check to make sure the CSRF token is correct.
Definition: FormValidatorCSRF.inc.php:18
Form
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
SpotlightForm\__construct
__construct($pressId, $spotlightId=null)
Definition: SpotlightForm.inc.php:40
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
SpotlightForm\readInputData
readInputData()
Definition: SpotlightForm.inc.php:89
FormValidatorCustom
Form validation check with a custom user function performing the validation check.
Definition: FormValidatorCustom.inc.php:18
SpotlightForm\fetch
fetch($request, $template=null, $display=false)
Definition: SpotlightForm.inc.php:63
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49
SpotlightForm\getPressId
getPressId()
Definition: SpotlightForm.inc.php:144