2012-07-18 03:07:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Application_Model_Webstream{
|
|
|
|
|
2012-07-31 03:48:04 +02:00
|
|
|
private $id;
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function __construct($id=-1)
|
2012-07-31 03:48:04 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
if ($id == -1) {
|
|
|
|
$this->webstream = new CcWebstream();
|
|
|
|
|
|
|
|
//We're not saving this object in the database, so -1 ID is OK.
|
|
|
|
$this->webstream->setDbId(-1);
|
|
|
|
$this->webstream->setDbName("Untitled Webstream");
|
|
|
|
$this->webstream->setDbDescription("");
|
|
|
|
$this->webstream->setDbUrl("http://");
|
|
|
|
$this->webstream->setDbLength("00h 00m");
|
|
|
|
$this->webstream->setDbName("Untitled Webstream");
|
|
|
|
} else {
|
|
|
|
$this->id = $id;
|
|
|
|
$this->webstream = CcWebstreamQuery::create()->findPK($this->id);
|
|
|
|
}
|
2012-07-31 03:48:04 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getName()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbName();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getId()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbId();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getLastModified($p_type)
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-07-18 03:07:57 +02:00
|
|
|
return "modified";
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getDefaultLength()
|
|
|
|
{
|
|
|
|
return $this->webstream->getDbLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbDescription();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
public function getUrl()
|
2012-07-26 00:41:52 +02:00
|
|
|
{
|
2012-08-03 21:59:34 +02:00
|
|
|
return $this->webstream->getDbUrl();
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|
2012-07-19 00:27:39 +02:00
|
|
|
|
2012-07-31 03:48:04 +02:00
|
|
|
public function getMetadata()
|
|
|
|
{
|
|
|
|
$webstream = CcWebstreamQuery::create()->findPK($this->id);
|
|
|
|
$subjs = CcSubjsQuery::create()->findPK($webstream->getDbCreatorId());
|
|
|
|
|
|
|
|
$username = $subjs->getDbLogin();
|
|
|
|
return array(
|
2012-08-03 21:59:34 +02:00
|
|
|
"name" => $this->webstream->getDbName(),
|
|
|
|
"length" => $this->webstream->getDbLength(),
|
|
|
|
"description" => $this->webstream->getDbDescription(),
|
2012-07-31 03:48:04 +02:00
|
|
|
"login"=> $username,
|
2012-08-03 21:59:34 +02:00
|
|
|
"url" => $this->webstream->getDbUrl(),
|
2012-07-31 03:48:04 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function deleteStreams($p_ids, $p_userId)
|
|
|
|
{
|
|
|
|
$leftOver = self::streamsNotOwnedByUser($p_ids, $p_userId);
|
|
|
|
if (count($leftOver) == 0) {
|
|
|
|
CcWebstreamQuery::create()->findPKs($p_ids)->delete();
|
|
|
|
} else {
|
|
|
|
throw new Exception("Invalid user permissions");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function returns that are not owen by $p_user_id among $p_ids
|
|
|
|
private static function streamsNotOwnedByUser($p_ids, $p_userId)
|
|
|
|
{
|
|
|
|
$ownedByUser = CcWebstreamQuery::create()->filterByDbCreatorId($p_userId)->find()->getData();
|
|
|
|
$ownedStreams = array();
|
|
|
|
foreach ($ownedByUser as $pl) {
|
|
|
|
if (in_array($pl->getDbId(), $p_ids)) {
|
|
|
|
$ownedStreams[] = $pl->getDbId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$leftOvers = array_diff($p_ids, $ownedStreams);
|
|
|
|
return $leftOvers;
|
|
|
|
|
|
|
|
}
|
2012-08-02 21:45:15 +02:00
|
|
|
/*
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[controller] => Webstream
|
|
|
|
[action] => save
|
|
|
|
[module] => default
|
|
|
|
[format] => json
|
|
|
|
[description] => desc
|
|
|
|
[url] => http://
|
|
|
|
[length] => 00h 20m
|
|
|
|
[name] => Default
|
|
|
|
)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public static function analyzeFormData($request)
|
|
|
|
{
|
|
|
|
$valid = array("length" => array(true, ''),
|
2012-08-03 21:59:34 +02:00
|
|
|
"url" => array(true, ''),
|
|
|
|
"name" => array(true, ''));
|
2012-08-02 21:45:15 +02:00
|
|
|
|
|
|
|
$length = trim($request->getParam("length"));
|
|
|
|
$result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches);
|
|
|
|
if (!$result == 1 || !count($matches) == 3) {
|
|
|
|
$valid['length'][0] = false;
|
|
|
|
$valid['length'][1] = 'Length should be of form "00h 00m"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$url = trim($request->getParam("url"));
|
|
|
|
//simple validator that checks to make sure that the url starts with http(s),
|
|
|
|
//and that the domain is at least 1 letter long followed by a period.
|
|
|
|
$result = preg_match("/^(http|https):\/\/.+\./", $url, $matches);
|
|
|
|
|
|
|
|
if ($result == 0) {
|
|
|
|
$valid['url'][0] = false;
|
|
|
|
$valid['url'][1] = 'URL should be of form "http://www.domain.com/mount"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$name = trim($request->getParam("name"));
|
|
|
|
if (strlen($name) == 0) {
|
|
|
|
$valid['name'][0] = false;
|
|
|
|
$valid['name'][1] = 'Webstream name cannot be empty';
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:34 +02:00
|
|
|
$id = trim($request->getParam("id"));
|
|
|
|
|
|
|
|
if (!is_null($id)) {
|
|
|
|
// user has performed a create stream action instead of edit
|
|
|
|
// stream action. Check if user has the rights to edit this stream.
|
|
|
|
|
|
|
|
Logging::log("CREATE");
|
|
|
|
} else {
|
|
|
|
Logging::log("EDIT");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-02 21:45:15 +02:00
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function isValid($analysis)
|
|
|
|
{
|
|
|
|
foreach ($analysis as $k => $v) {
|
|
|
|
if ($v[0] == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-31 03:48:04 +02:00
|
|
|
|
2012-07-26 00:41:52 +02:00
|
|
|
public static function save($request)
|
|
|
|
{
|
|
|
|
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
|
|
|
|
|
|
|
|
$length = trim($request->getParam("length"));
|
2012-08-02 21:45:15 +02:00
|
|
|
$result = preg_match("/^([0-9]{1,2})h ([0-5][0-9])m$/", $length, $matches);
|
2012-08-03 21:59:34 +02:00
|
|
|
|
2012-08-02 21:45:15 +02:00
|
|
|
if ($result == 1 && count($matches) == 3) {
|
|
|
|
$hours = $matches[1];
|
|
|
|
$minutes = $matches[2];
|
|
|
|
$di = new DateInterval("PT{$hours}H{$minutes}M");
|
|
|
|
$dblength = $di->format("%H:%I");
|
|
|
|
} else {
|
|
|
|
//This should never happen because we should have already validated
|
|
|
|
//in the controller
|
|
|
|
throw new Exception("Invalid date format: $length");
|
|
|
|
}
|
2012-08-03 21:59:34 +02:00
|
|
|
|
|
|
|
$id = $request->getParam("id");
|
|
|
|
|
|
|
|
if (is_null($id)) {
|
|
|
|
$webstream = new CcWebstream();
|
|
|
|
} else {
|
|
|
|
$webstream = CcWebstreamQuery::create()->findPK($id);
|
|
|
|
}
|
2012-08-02 21:45:15 +02:00
|
|
|
|
2012-07-19 00:27:39 +02:00
|
|
|
$webstream->setDbName($request->getParam("name"));
|
|
|
|
$webstream->setDbDescription($request->getParam("description"));
|
|
|
|
$webstream->setDbUrl($request->getParam("url"));
|
2012-07-26 00:41:52 +02:00
|
|
|
|
|
|
|
$webstream->setDbLength($dblength);
|
2012-07-31 03:48:04 +02:00
|
|
|
$webstream->setDbCreatorId($userInfo->id);
|
2012-07-27 20:47:15 +02:00
|
|
|
$webstream->setDbUtime(new DateTime("now", new DateTimeZone('UTC')));
|
|
|
|
$webstream->setDbMtime(new DateTime("now", new DateTimeZone('UTC')));
|
2012-07-19 00:27:39 +02:00
|
|
|
$webstream->save();
|
|
|
|
}
|
2012-07-18 03:07:57 +02:00
|
|
|
}
|