Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MaarchCourrier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Harbor Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maarch
MaarchCourrier
Commits
139734a2
Verified
Commit
139734a2
authored
7 years ago
by
Damien
Browse files
Options
Downloads
Patches
Plain Diff
[REFACTORING] Add preparedClauseController
parent
70bdec95
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
composer.json
+1
-0
1 addition, 0 deletions
composer.json
src/core/controllers/PreparedClauseController.php
+210
-0
210 additions, 0 deletions
src/core/controllers/PreparedClauseController.php
with
211 additions
and
0 deletions
composer.json
+
1
−
0
View file @
139734a2
{
"autoload"
:
{
"psr-4"
:
{
"SrcCore\\"
:
"src/core/"
,
"Basket\\"
:
"src/app/basket/"
,
"Contact\\"
:
"src/app/contact/"
,
"History\\"
:
"src/app/history/"
,
...
...
This diff is collapsed.
Click to expand it.
src/core/controllers/PreparedClauseController.php
0 → 100644
+
210
−
0
View file @
139734a2
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Prepared Clause Controller
* @author dev@maarch.org
*/
namespace
SrcCore\controllers
;
use
Core\Models\UserModel
;
use
Core\Models\ValidatorModel
;
use
Entities\Models\EntityModel
;
class
PreparedClauseController
{
public
static
function
getPreparedClause
(
array
$aArgs
)
{
ValidatorModel
::
notEmpty
(
$aArgs
,
[
'clause'
,
'userId'
]);
ValidatorModel
::
stringType
(
$aArgs
,
[
'clause'
,
'userId'
]);
$clause
=
$aArgs
[
'clause'
];
if
(
preg_match
(
'/@user/'
,
$clause
))
{
$clause
=
str_replace
(
'@user'
,
"'
{
$aArgs
[
'userId'
]
}
'"
,
$clause
);
}
if
(
preg_match
(
'/@email/'
,
$clause
))
{
$user
=
UserModel
::
getByUserId
([
'userId'
=>
$aArgs
[
'userId'
],
'select'
=>
[
'mail'
]]);
$clause
=
str_replace
(
'@email'
,
"'
{
$user
[
'mail'
]
}
'"
,
$clause
);
}
if
(
preg_match
(
'/@my_entities/'
,
$clause
))
{
$entities
=
EntityModel
::
getByUserId
([
'userId'
=>
$aArgs
[
'userId'
],
'select'
=>
[
'entity_id'
]]);
$myEntitiesClause
=
''
;
foreach
(
$entities
as
$key
=>
$entity
)
{
if
(
$key
>
0
)
{
$myEntitiesClause
.
=
", "
;
}
$myEntitiesClause
.
=
"'
{
$entity
[
'entity_id'
]
}
'"
;
}
if
(
empty
(
$myEntitiesClause
))
{
$myEntitiesClause
=
"''"
;
}
$clause
=
str_replace
(
'@my_entities'
,
$myEntitiesClause
,
$clause
);
}
if
(
preg_match
(
'/@my_primary_entity/'
,
$clause
))
{
$entity
=
UserModel
::
getPrimaryEntityByUserId
([
'userId'
=>
$aArgs
[
'userId'
]]);
if
(
empty
(
$entity
))
{
$primaryEntity
=
"''"
;
}
else
{
$primaryEntity
=
"'"
.
$entity
[
'entity_id'
]
.
"'"
;
}
$clause
=
str_replace
(
'@my_primary_entity'
,
$primaryEntity
,
$clause
);
}
if
(
preg_match
(
'/@all_entities/'
,
$clause
))
{
$allEntities
=
EntityModel
::
get
([
'select'
=>
[
'entity_id'
],
'where'
=>
[
'enabled = ?'
],
'data'
=>
[
'Y'
]]);
$allEntitiesClause
=
''
;
foreach
(
$allEntities
as
$key
=>
$allEntity
)
{
if
(
$key
>
0
)
{
$allEntitiesClause
.
=
", "
;
}
$allEntitiesClause
.
=
"'
{
$allEntity
[
'entity_id'
]
}
'"
;
}
if
(
empty
(
$allEntitiesClause
))
{
$allEntitiesClause
=
"''"
;
}
$clause
=
str_replace
(
"@all_entities"
,
$allEntitiesClause
,
$clause
);
}
$total
=
preg_match_all
(
"|@subentities\[('[^\]]*')\]|"
,
$clause
,
$subEntities
,
PREG_PATTERN_ORDER
);
if
(
$total
>
0
)
{
for
(
$i
=
0
;
$i
<
$total
;
$i
++
)
{
$aEntities
=
[];
$tmpSubEntities
=
str_replace
(
"'"
,
''
,
$subEntities
[
1
][
$i
]);
if
(
preg_match
(
'/,/'
,
$tmpSubEntities
))
{
$aEntities
=
preg_split
(
'/,/'
,
$tmpSubEntities
);
}
else
{
$aEntities
[]
=
$tmpSubEntities
;
}
$allSubEntities
=
[];
foreach
(
$aEntities
as
$entity
)
{
$subEntitiesForEntity
=
EntityModel
::
getEntityChildren
([
'entityId'
=>
trim
(
$entity
)]);
unset
(
$subEntitiesForEntity
[
0
]);
$allSubEntities
=
array_merge
(
$allSubEntities
,
$subEntitiesForEntity
);
}
$allSubEntitiesClause
=
''
;
foreach
(
$allSubEntities
as
$key
=>
$allSubEntity
)
{
if
(
$key
>
0
)
{
$allSubEntitiesClause
.
=
", "
;
}
$allSubEntitiesClause
.
=
"'
{
$allSubEntity
}
'"
;
}
if
(
empty
(
$allSubEntitiesClause
))
{
$allSubEntitiesClause
=
"''"
;
}
$clause
=
preg_replace
(
"|@subentities\['[^\]]*'\]|"
,
$allSubEntitiesClause
,
$clause
,
1
);
}
}
$total
=
preg_match_all
(
"|@immediate_children\[('[^\]]*')\]|"
,
$clause
,
$immediateChildrens
,
PREG_PATTERN_ORDER
);
if
(
$total
>
0
)
{
for
(
$i
=
0
;
$i
<
$total
;
$i
++
)
{
$aEntities
=
[];
$tmpImmediateChildrens
=
str_replace
(
"'"
,
''
,
$immediateChildrens
[
1
][
$i
]);
if
(
preg_match
(
'/,/'
,
$tmpImmediateChildrens
))
{
$aEntities
=
preg_split
(
'/,/'
,
$tmpImmediateChildrens
);
}
else
{
$aEntities
[]
=
$tmpImmediateChildrens
;
}
$allImmediateChildrens
=
[];
foreach
(
$aEntities
as
$entity
)
{
$immediateChildrensForEntity
=
EntityModel
::
get
([
'select'
=>
[
'entity_id'
],
'where'
=>
[
'parent_entity_id = ?'
],
'data'
=>
[
trim
(
$entity
)]]);
foreach
(
$immediateChildrensForEntity
as
$value
)
{
$allImmediateChildrens
[]
=
$value
[
'entity_id'
];
}
}
$allImmediateChildrensClause
=
''
;
foreach
(
$allImmediateChildrens
as
$key
=>
$allImmediateChild
)
{
if
(
$key
>
0
)
{
$allImmediateChildrensClause
.
=
", "
;
}
$allImmediateChildrensClause
.
=
"'
{
$allImmediateChild
}
'"
;
}
if
(
empty
(
$allImmediateChildrensClause
))
{
$allImmediateChildrensClause
=
"''"
;
}
$clause
=
preg_replace
(
"|@immediate_children\['[^\]]*'\]|"
,
$allImmediateChildrensClause
,
$clause
,
1
);
}
}
$total
=
preg_match_all
(
"|@parent_entity\[('[^\]]*')\]|"
,
$clause
,
$parentEntity
,
PREG_PATTERN_ORDER
);
if
(
$total
>
0
)
{
for
(
$i
=
0
;
$i
<
$total
;
$i
++
)
{
$tmpParentEntity
=
trim
(
str_replace
(
"'"
,
''
,
$parentEntity
[
1
][
$i
]));
$entity
=
EntityModel
::
getById
([
'entityId'
=>
$tmpParentEntity
,
'select'
=>
[
'entity_id'
,
'parent_entity_id'
]]);
if
(
empty
(
$entity
[
'parent_entity_id'
]))
{
$parentEntityClause
=
"''"
;
}
else
{
$parentEntityClause
=
"'
{
$entity
[
'parent_entity_id'
]
}
'"
;
}
$clause
=
preg_replace
(
"|@parent_entity\['[^\]]*'\]|"
,
$parentEntityClause
,
$clause
,
1
);
}
}
$total
=
preg_match_all
(
"|@sisters_entities\[('[^\]]*')\]|"
,
$clause
,
$sistersEntities
,
PREG_PATTERN_ORDER
);
if
(
$total
>
0
)
{
for
(
$i
=
0
;
$i
<
$total
;
$i
++
)
{
$tmpSisterEntity
=
trim
(
str_replace
(
"'"
,
''
,
$sistersEntities
[
1
][
$i
]));
$sisterEntity
=
EntityModel
::
getById
([
'entityId'
=>
$tmpSisterEntity
,
'select'
=>
[
'parent_entity_id'
]]);
$allSisterEntities
=
EntityModel
::
get
([
'select'
=>
[
'entity_id'
],
'where'
=>
[
'parent_entity_id = ?'
],
'data'
=>
[
$sisterEntity
[
'parent_entity_id'
]]]);
$allSisterEntitiesClause
=
''
;
foreach
(
$allSisterEntities
as
$key
=>
$allSisterEntity
)
{
if
(
$key
>
0
)
{
$allSisterEntitiesClause
.
=
", "
;
}
$allSisterEntitiesClause
.
=
"'
{
$allSisterEntity
[
'entity_id'
]
}
'"
;
}
if
(
empty
(
$allSisterEntitiesClause
))
{
$allSisterEntitiesClause
=
"''"
;
}
$clause
=
preg_replace
(
"|@sisters_entities\['[^\]]*'\]|"
,
$allSisterEntitiesClause
,
$clause
,
1
);
}
}
$total
=
preg_match_all
(
"|@entity_type\[('[^\]]*')\]|"
,
$clause
,
$entityType
,
PREG_PATTERN_ORDER
);
if
(
$total
>
0
)
{
for
(
$i
=
0
;
$i
<
$total
;
$i
++
)
{
$tmpEntityType
=
trim
(
str_replace
(
"'"
,
''
,
$entityType
[
1
][
$i
]));
$allEntitiesType
=
EntityModel
::
get
([
'select'
=>
[
'entity_id'
],
'where'
=>
[
'entity_type = ?'
],
'data'
=>
[
$tmpEntityType
]]);
$allEntitiesTypeClause
=
''
;
foreach
(
$allEntitiesType
as
$key
=>
$allEntityType
)
{
if
(
$key
>
0
)
{
$allEntitiesTypeClause
.
=
", "
;
}
$allEntitiesTypeClause
.
=
"'
{
$allEntityType
[
'entity_id'
]
}
'"
;
}
if
(
empty
(
$allEntitiesTypeClause
))
{
$allEntitiesTypeClause
=
"''"
;
}
$clause
=
preg_replace
(
"|@entity_type\['[^\]]*'\]|"
,
$allEntitiesTypeClause
,
$clause
,
1
);
}
}
return
$clause
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment