Procházet zdrojové kódy

added reminders form in Notify; added validate in V

Piotr Labudda před 10 roky
rodič
revize
ef75124db7
2 změnil soubory, kde provedl 189 přidání a 5 odebrání
  1. 157 5
      SE/se-lib/Route/Notify.php
  2. 32 0
      SE/se-lib/V.php

+ 157 - 5
SE/se-lib/Route/Notify.php

@@ -22,13 +22,165 @@ class Route_Notify extends RouteBase {
 
 	public function defaultAction() {
 		SE_Layout::gora();
+		try {
+			$this->formUserTableReminder();
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
+	public function rmAction() {
+		SE_Layout::gora();
+		try {
+			$this->rmUserTableReminder($_GET);
+		} catch (Exception $e) {
+			SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+		}
+		SE_Layout::dol();
+	}
+
+	public function rmUserTableReminder($args) {
+		throw new Exception("TODO: rm args:" . json_encode($args));
+	}
+
+	public function formUserTableReminder() {
+		$usrLogin = User::getLogin();
+		echo '<div class="container">';
+		$subTask = V::get('_subTask', '', $_POST);
+		if ('_add_table_reminder' == $subTask) {
+			try {
+				$this->addUserTableReminder($usrLogin, $_POST);
+				SE_Layout::alert('info', "Dodano przypomnienie");
+			} catch (Exception $e) {
+				SE_Layout::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+			}
+		}
+		$userReminders = $this->getUserReminders($usrLogin);
 		?>
-<div class="container">
-	<h1>Nofitication system</h1>
-	...
-</div>
+	<h1>Powiadomienia dla <code><?php echo $usrLogin; ?></code></h1>
+	<?php if (empty($userReminders)) : ?>
+		<?php SE_Layout::alert('warning', "Brak zdefiniowanych przypomnień"); ?>
+	<?php else : ?>
+		<table class="table table-hovered">
+			<thead>
+				<tr>
+					<th>rodzaj</th>
+					<th>jak często</th>
+					<th>utworzony</th>
+					<th>ostatnio uruchomiony</th>
+					<th></th>
+				</tr>
+			</thead>
+			<tbody>
+				<?php foreach ($userReminders as $reminder) : ?>
+					<tr>
+						<td><?php echo $this->printValue('what', $reminder['what']); ?></td>
+						<td><?php echo $this->printValue('when', $reminder['when']); ?></td>
+						<td><?php echo $reminder['_created']; ?></td>
+						<td><?php echo ($reminder['last_exec_time'])? $reminder['last_exec_time'] : '<i>brak danych</i>'; ?></td>
+						<td><a href="index.php?_route=Notify&_task=rm&who=<?php echo $usrLogin; ?>&what=<?php echo $reminder['what']; ?>&when=<?php echo $reminder['when']; ?>"
+									 onclick="return confirm('Czy usunąć przypomnienie?')"
+									 ><i class="glyphicon glyphicon-remove" style="color:red;opacity:0.4;"></i></a></td>
+					</tr>
+				<?php endforeach; ?>
+			</tbody>
+		</table>
+	<?php endif; ?>
+
+	<hr>
+	<h4>Dodaj powiadomienia dla wybranych tabel:</h4>
+	<form method="post" class="form-inline">
+		<input type="hidden" name="_subTask" value="_add_table_reminder">
+		<label>rodzaj:</label>
+		<select type="select" name="what" class="form-control">
+			<option value="">[ Wybierz ]</option>
+			<option value="l_app_projekty">projekty</option>
+			<option value="l_app_koresp">korespondencja</option>
+			<option value="l_app_zadania">zadania</option>
+			<option value="l_app_procesy">procesy</option>
+			<option value="l_app_zasoby">zasoby</option>
+			<option value="l_app_all_przypomnij">*wszystkie</option>
+		</select>
+		<label>jak często:</label>
+		<select type="select" name="when" class="form-control">
+			<option value="">[ Wybierz ]</option>
+			<option value="once_a_day">Raz dziennie</option>
+		</select>
+		<input type="submit" value="Dodaj" class="btn btn-primary">
+	</form>
 		<?php
-		SE_Layout::dol();
+		echo '</div>';// .container
+	}
+
+	public function printValue($fldName, $argValue) {
+		if ('what' == $fldName || 'when' == $fldName) {
+			$valueLabels = $this->getFieldValueLabels($fldName);
+			return V::get($argValue, $argValue, $valueLabels);
+		}
+		return $argValue;
+	}
+
+	public function getFieldValueLabels($fldName) {
+		$valueLabels = array();
+		if ('what' == $fldName) {
+			$valueLabels['l_app_projekty'] = "projekty";
+			$valueLabels['l_app_koresp'] = "korespondencja";
+			$valueLabels['l_app_zadania'] = "zadania";
+			$valueLabels['l_app_procesy'] = "procesy";
+			$valueLabels['l_app_zasoby'] = "zasoby";
+			$valueLabels['l_app_all_przypomnij'] = "*wszystkie";
+		} else if ('when' == $fldName) {
+			$valueLabels['once_a_day'] = "Raz dziennie";
+		}
+		return $valueLabels;
+	}
+
+	public function addUserTableReminder($usrLogin, $args) {
+		$what_values = array();
+		$what_values[] = 'l_app_projekty';
+		$what_values[] = 'l_app_koresp';
+		$what_values[] = 'l_app_zadania';
+		$what_values[] = 'l_app_procesy';
+		$what_values[] = 'l_app_zasoby';
+		$what_values[] = 'l_app_all_przypomnij';
+		$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$what_values));
+		$when_values = array();
+		$when_values[] = 'once_a_day';
+		$when = V::validate('when', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
+		$pdo = DB::getPDO();
+		$sth = $pdo->prepare("
+			insert into `CRM_NOTIFY` (
+				`who`,
+				`what`,
+				`when`,
+				`_created`
+			) values (
+				:who,
+				:what,
+				:when,
+				NOW()
+			)
+		");
+		$bindValues = array();
+		$bindValues['who'] = array($usrLogin, PDO::PARAM_STR);
+		$bindValues['what'] = array($what, PDO::PARAM_STR);
+		$bindValues['when'] = array($when, PDO::PARAM_STR);
+		$pdo->bindValues($sth, $bindValues);
+		//DBG::_(true, true, "sql", $pdo->getRawSql($sth), __CLASS__, __FUNCTION__, __LINE__);
+		$sth->execute();
+	}
+
+	public function getUserReminders($usrLogin) {
+		$pdo = DB::getPDO();
+		$sth = $pdo->prepare("
+			select n.*
+			from `CRM_NOTIFY` n
+			where n.`who` = '{$usrLogin}'
+		");
+		$sth->execute();
+		$reminders = $sth->fetchAll();
+		return $reminders;
 	}
 
 	public function reinstallAction() {

+ 32 - 0
SE/se-lib/V.php

@@ -312,6 +312,38 @@ class V {
 		return false;
 	}
 
+	public static function validate($argName, $args, $params) {
+		//$what = V::validate('what', $args, array('type'=>'word', 'not_empty'=>true, 'max_length'=>'255', 'values'=>$when_values));
+		$argValue = V::get($argName, null, $args);
+		$fldLabel = V::get('fld_label', $argName, $params);
+		if (array_key_exists('not_empty', $params) && true == $params['not_empty']) {
+			if (!array_key_exists($argName, $args) || empty($args[$argName])) throw new Exception("Field {$fldLabel} not set.");
+		}
+		$params['fld_label'] = $fldLabel;
+		return V::validateValue($argValue, $params);
+	}
+
+	public static function validateValue($value, $params) {
+		$fldLabel = V::get('fld_label', '', $params);
+		$maxLength = V::get('max_length', 0, $params);
+		if ($maxLength > 0) {
+			if (strlen($value) > $maxLength) throw new Exception("Field {$fldLabel} cannot be longer then {$maxLength}.");
+		}
+		$allowedValues = V::get('values', null, $params);
+		if (is_array($allowedValues) && !empty($allowedValues)) {
+			if (!in_array($value, $allowedValues)) throw new Exception("Field value is not allowed ({$fldLabel})");
+		}
+		$type = V::get('type', null, $params);
+		if ($type != null) {
+			if ('word' == $type) {
+				if (!is_scalar($value) || !preg_match('/^[a-zA-Z_-]*$/', $value)) throw new Exception("required type '{$type}' ({$fldLabel})");
+			} else {
+				throw new Exception("Unimplemented type to validate: '{$type}'");
+			}
+		}
+		return $value;
+	}
+
 	public function exec($cmd, &$out, &$ret) {
 		$out = null;
 		$ret = null;