[212] | 1 | <?php |
---|
| 2 | // Survey database interface class as intermediate for storing data from the site to the RDF database |
---|
| 3 | require_once 'rdfConstants.php'; |
---|
| 4 | // Include RAP Library to write RDF files |
---|
| 5 | include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); |
---|
| 6 | /** |
---|
| 7 | * Description of Application |
---|
| 8 | * |
---|
| 9 | * @author jkraaijeveld |
---|
| 10 | */ |
---|
| 11 | class Application extends ResearchToolObject{ |
---|
| 12 | |
---|
| 13 | public static $filename = 'data/applications/applications.rdf'; |
---|
| 14 | |
---|
| 15 | public $title; |
---|
| 16 | public $description; |
---|
| 17 | public $style; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * Constructor for an Application object |
---|
| 21 | * @param type $uid : The uid of the Application object. |
---|
| 22 | * @param type $title: The title of the Application. |
---|
| 23 | * @param type $description: The description of the Application. |
---|
| 24 | * @param type $style: The style of the Application. |
---|
| 25 | */ |
---|
| 26 | public function __construct($uid = null, $title = null, $description = null, $style = null) |
---|
| 27 | { |
---|
| 28 | if(!isset ($uid)) |
---|
| 29 | { |
---|
| 30 | $uid = md5(uniqid(rand(), true)); |
---|
| 31 | } |
---|
| 32 | $this->uid = $uid; |
---|
| 33 | $this->title = $title; |
---|
| 34 | $this->description = $description; |
---|
| 35 | $this->style = $style; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * function save() |
---|
| 40 | * Saves the current object into the database. |
---|
| 41 | */ |
---|
| 42 | public function save() |
---|
| 43 | { |
---|
| 44 | //Ensure the required folder exists. |
---|
| 45 | if(!is_dir('data/applications/')) |
---|
| 46 | mkdir('data/applications/'); |
---|
| 47 | |
---|
| 48 | $model = ResearchToolObject::load(Application::$filename); |
---|
| 49 | $resourceApplication = new Resource(APPLICATION.'/'.$this->uid); |
---|
| 50 | //Remove the old value stored with the given id |
---|
| 51 | $model->subtract($model->find($resourceApplication, null, null)); |
---|
| 52 | |
---|
| 53 | //Add the new statements to the model |
---|
| 54 | $resourceApplicationType = new Resource(APPLICATION); |
---|
| 55 | $predicateRType = new Resource(RTYPE); |
---|
| 56 | $model->add(new Statement($resourceApplication,$predicateRType,$resourceApplicationType)); |
---|
| 57 | |
---|
| 58 | $literalApplicationID = new Literal($this->uid); |
---|
| 59 | $predicateUniqueID = new Resource(UID); |
---|
| 60 | $model->add(new Statement($resourceApplication,$predicateUniqueID,$literalApplicationID)); |
---|
| 61 | |
---|
| 62 | $applicationTitle = new Literal($this->title); |
---|
| 63 | $predicateTitle = new Resource(TITLE); |
---|
| 64 | $model->add(new Statement($resourceApplication,$predicateTitle,$applicationTitle)); |
---|
| 65 | |
---|
| 66 | $applicationDescription = new Literal($this->description); |
---|
| 67 | $predicateDescription = new Resource(DESCRIPTION); |
---|
| 68 | $model->add(new Statement($resourceApplication,$predicateDescription,$applicationDescription)); |
---|
| 69 | |
---|
| 70 | $applicationStyle = new Literal($this->style); |
---|
| 71 | $predicateStyle = new Resource(STYLE); |
---|
| 72 | $model->add(new Statement($resourceApplication,$predicateStyle,$applicationStyle)); |
---|
| 73 | |
---|
| 74 | $model->saveAs(Application::$filename, 'rdf'); |
---|
| 75 | return true; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * static function get($arguments) |
---|
| 80 | * Gets the array of Application objects belonging to arguments supplied. |
---|
| 81 | * @param type $arguments : An array containing zero or more of the following keys: |
---|
| 82 | * 'uid', 'title', 'description', 'style' |
---|
| 83 | */ |
---|
| 84 | public static function get($arguments) |
---|
| 85 | { |
---|
| 86 | $model = ResearchToolObject::load(Application::$filename); |
---|
| 87 | //Create the querystring |
---|
| 88 | $querystring = ' |
---|
| 89 | PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> |
---|
| 90 | PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> |
---|
| 91 | SELECT ?uid, ?title, ?description, ?style |
---|
| 92 | WHERE |
---|
| 93 | { |
---|
| 94 | _application predicates:resource_type resources:application ; |
---|
| 95 | predicates:uid ?uid ; |
---|
| 96 | predicates:title ?title ; |
---|
| 97 | predicates:description ?description ; |
---|
| 98 | predicates:style ?style ; ' |
---|
| 99 | . ResearchToolObject::createArguments($arguments) . ' |
---|
| 100 | }'; |
---|
| 101 | |
---|
| 102 | //Query the model |
---|
| 103 | $results = $model->sparqlQuery($querystring); |
---|
| 104 | $applications = array(); |
---|
| 105 | if(!empty($results)) |
---|
| 106 | { |
---|
| 107 | //Run over all results and create appropriate Application objets |
---|
| 108 | foreach($results as $result) |
---|
| 109 | { |
---|
| 110 | $applications[] = new Application($result['?uid']->label, $result['?title']->label, $result['?description']->label, $result['?style']->label); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | return $applications; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | ?> |
---|