forked from rspieker/konsolidate_breed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.class.php
138 lines (129 loc) · 4.13 KB
/
resource.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.net
* /___ ___/ \ /
* \ / /\ \ / \ Class: BreedResource
* \/___/ \___\/ \ Tier: Breed
* \ \ /\ \ /\ / Module: Resource
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*/
/**
* Resources
* @name BreedResource
* @type class
* @package Kontribute
* @author Rogier Spieker <rogier@konsolidate.net>
*/
class BreedResource extends Konsolidate
{
/**
* Retrieve the resource id for given path
* @name getIDByPath
* @type method
* @access public
* @param string path
* @param bool create (optional, false)
* @returns int id
* @syntax (int) Object->getIDByPath( string path [, bool create ] )
*/
public function getIDByPath( $sPath=null, $bAutoCreate=false )
{
if ( empty( $sPath ) )
$sPath = $this->path;
$sQuery = "SELECT rscid,
rscenabled
FROM resource
WHERE rscpath=" . $this->call( "/DB/quote", $sPath );
$oResult = $this->call( "/DB/query", $sQuery );
if ( is_object( $oResult ) && $oResult->errno <= 0 && $oResult->rows > 0 )
{
// there can be only one
$oRecord = $oResult->next();
$this->id = (int) $oRecord->rscid;
$this->path = $sPath;
$this->enabled = (bool) $oRecord->rscenabled;
return $this->id;
}
if ( $bAutoCreate )
return $this->create( $sPath, false );
return false;
}
/**
* Retrieve the path for given resource id
* @name getPathByID
* @type method
* @access public
* @param int resource id
* @returns string path
* @syntax (string) Object->getPathByID( int resourceid )
*/
public function getPathByID( $nID )
{
$sQuery = "SELECT rscpath,
rscenabled
FROM resource
WHERE rscid=" . $this->call( "/DB/quote", $nID );
$oResult = $this->call( "/DB/query", $sQuery );
if ( is_object( $oResult ) && $oResult->errno <= 0 && $oResult->rows > 0 )
{
// there can be only one
$oRecord = $oResult->next();
$this->id = (int) $nID;
$this->path = (string) $oRecord->rscpath;
$this->enabled = (bool) $oRecord->rscenabled;
return $oRecord->rscpath;
}
return false;
}
/**
* Create a resource
* @name create
* @type method
* @access public
* @param string path
* @param bool unique (optional, false)
* @returns int resource id (false on (bool) unique true and already existant
* @syntax (int) Object->create( string path [, (bool) unique ] )
*/
public function create( $sPath=null, $bUnique=false )
{
if ( empty( $sPath ) )
$sPath = $this->path;
$sQuery = "INSERT INTO resource
( rscpath, rsccreatedts )
VALUES ( " . $this->call( "/DB/quote", $sPath ) . ", NOW() )";
if ( !$bUnique )
$sQuery .= " ON DUPLICATE KEY UPDATE rscmodifiedts=NOW()";
$oResult = $this->call( "/DB/query", $sQuery );
if ( is_object( $oResult ) && $oResult->errno <= 0 )
return $oResult->lastInsertID();
return false;
}
/**
* Suggest a sanitized name for use in a path
* @name suggestSanitizedPath
* @type method
* @access public
* @param string input
* @param bool lowercase
* @returns string path element suggestion
* @syntax (string) Object->suggestSanitizedPath( string input [, bool lowercase ] )
*/
public function suggestSanitizedPath( $sInput, $bLowerCase=true )
{
$aSpecialSuffix = Array( "acute", "grave", "circ", "ring", "tilde", "uml", "lig", "cedil", "slash" );
for ( $i = 0; $i < count( $aSpecialSuffix ); ++$i )
$aSpecialSuffix[ $i ] = "/&([a-zA-Z]+){$aSpecialSuffix[ $i ]};/";
$sInput = htmlentities( utf8_decode( $sInput ) );
$sInput = preg_replace( $aSpecialSuffix, "\\1", $sInput );
$sInput = preg_replace( "/[^a-zA-Z0-9_-]+/", "_", html_entity_decode( $sInput ) );
return $bLowerCase ? strToLower( $sInput ) : $sInput;
}
}