PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/class.componentManipulation.php

https://github.com/victoralex/gameleon
PHP | 593 lines | 420 code | 104 blank | 69 comment | 64 complexity | 1bfb16b0aa3e377018a12c2e019c49f6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. class ComponentManipulation
  3. {
  4. const ERR_OK = 0;
  5. const ERR_INVALID_ARGS = 1;
  6. const ERR_NO_COMPONENT_NAME = 2;
  7. const ERR_NO_AREA_NAME = 3;
  8. const ERR_COMPONENT_EXISTS = 4;
  9. const ERR_COPY = 5;
  10. const ERR_NO_PAGE_NAME = 6;
  11. const ERR_UPDATE = 7;
  12. const ERR_COMPONENT_NOT_FOUND = 8;
  13. const ERR_PAGE_NOT_FOUND = 9;
  14. const ERR_NO_LAYOUT_AREA = 10;
  15. const ERR_NO_OVERWRITE_SPECIFIED = 11;
  16. public static $ERR_LAST_ERROR = self::ERR_OK;
  17. const AREA_PUBLIC = "public_web";
  18. const AREA_ADMIN = "public_admin";
  19. const COMPONENT_DEFAULT_NAME = "blankComponent";
  20. public function init()
  21. {
  22. }
  23. private function setError( $errorID )
  24. {
  25. self::$ERR_LAST_ERROR = $errorID;
  26. return $errorID;
  27. }
  28. public function addToPage( $args )
  29. {
  30. /*
  31. Params eval
  32. */
  33. if(!is_array($args))
  34. {
  35. return self::setError( self::ERR_INVALID_ARGS );
  36. }
  37. if(
  38. !isset($args["area"]) ||
  39. (
  40. $args["area"] != self::AREA_PUBLIC &&
  41. $args["area"] != self::AREA_ADMIN
  42. )
  43. )
  44. {
  45. return self::setError( self::ERR_NO_AREA_NAME );
  46. }
  47. if(
  48. !isset($args["component"]) ||
  49. empty($args["component"])
  50. )
  51. {
  52. return self::setError( self::ERR_NO_COMPONENT_NAME );
  53. }
  54. if(
  55. !isset($args["page"]) ||
  56. empty($args["page"])
  57. )
  58. {
  59. return self::setError( self::ERR_NO_PAGE_NAME );
  60. }
  61. if(
  62. !isset($args["overwriteTranslations"])
  63. )
  64. {
  65. return self::setError( self::ERR_NO_OVERWRITE_SPECIFIED );
  66. }
  67. /*
  68. Functional part
  69. */
  70. // Check if the component exists
  71. if(!file_exists( SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] ))
  72. {
  73. return self::setError( self::ERR_COMPONENT_NOT_FOUND );
  74. }
  75. $destPrefix = SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "pages" . DIRECTORY_SEPARATOR . $args["page"];
  76. // Check if the page exists
  77. if(!file_exists( $destPrefix ))
  78. {
  79. return self::setError( self::ERR_PAGE_NOT_FOUND );
  80. }
  81. // Include into XSL
  82. $doc = new DOMDocument();
  83. $doc->load( $destPrefix . DIRECTORY_SEPARATOR . $args["page"] . ".xsl" );
  84. $xpath = new DOMXPath($doc);
  85. $xpath->registerNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
  86. $xpath->registerNamespace("site", "http://site.emotionconcept.ro");
  87. // Include XSL into page
  88. $existanceCheck = $xpath->query("/xsl:stylesheet/xsl:include[@href='../../components/" . $args["component"] . "/" . $args["component"] . ".xsl']", $doc);
  89. if($existanceCheck->length == 0)
  90. {
  91. $xmlList = $xpath->query("/xsl:stylesheet/xsl:include", $doc);
  92. $xmlStylesheet = $xpath->query("/xsl:stylesheet", $doc);
  93. $newElement = $doc->createElement("xsl:include");
  94. $newElement->setAttribute("href", "../../components/" . $args["component"] . "/" . $args["component"] . ".xsl");
  95. $xmlStylesheet->item(0)->insertBefore( $newElement, $xmlList->item( $xmlList->length - 1 ) );
  96. $doc->save( $destPrefix . DIRECTORY_SEPARATOR . $args["page"] . ".xsl" );
  97. }
  98. // Translations
  99. $pointer = opendir( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "translations" );
  100. while(false !== ($file = readdir($pointer)))
  101. {
  102. if( is_dir( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $file ) )
  103. {
  104. continue;
  105. }
  106. $language = explode(".", $file);
  107. $componentTranslationFile = SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $language[1] . ".xml";
  108. if( !file_exists( $componentTranslationFile ))
  109. {
  110. continue;
  111. }
  112. // Page translation file
  113. $docPage = new DOMDocument();
  114. $docPage->load( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $file );
  115. // Component translation file
  116. $docComponent = new DOMDocument();
  117. $docComponent->load( $componentTranslationFile );
  118. $xpath = new DOMXPath($docComponent);
  119. $translationTree = $xpath->query("/translation/component[@name='" . $args["component"] . "']", $docComponent);
  120. // Check if the translation exists already
  121. $xpath = new DOMXPath($docPage);
  122. $componentOnPage = $xpath->query("/translation/component[@name='" . $args["component"] . "']", $docPage);
  123. if($componentOnPage->length > 0)
  124. {
  125. if( $args["overwriteTranslations"] == true )
  126. {
  127. // Overwrite all translations
  128. $docPage->getElementsByTagName("translation")->item(0)->replaceChild(
  129. $docPage->importNode($translationTree->item(0), true),
  130. $componentOnPage->item(0)
  131. );
  132. }
  133. else
  134. {
  135. // Just add new fields
  136. $fields = $translationTree->item(0)->getElementsByTagName("field");
  137. for($i=0;$i<$fields->length;$i++)
  138. {
  139. $xpath = new DOMXPath($docPage);
  140. $existanceCheck = $xpath->query("/translation/component[@name='" . $args["component"] . "']/field[@name='" . $fields->item($i)->getAttribute("name") . "']", $docPage);
  141. if($existanceCheck->length > 0 )
  142. {
  143. continue;
  144. }
  145. // Add the new translation as it is
  146. $componentOnPage->item(0)->appendChild(
  147. $docPage->importNode($fields->item($i), true)
  148. );
  149. }
  150. }
  151. }
  152. else
  153. {
  154. $docPage->getElementsByTagName("translation")->item(0)->appendChild(
  155. $docPage->importNode($translationTree->item(0), true)
  156. );
  157. }
  158. $docPage->save( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $file );
  159. }
  160. // Put values into the page
  161. $docPage = new DOMDocument();
  162. $docPage->load( $destPrefix . DIRECTORY_SEPARATOR . $args["page"] . ".xml" );
  163. $xpath = new DOMXPath($docPage);
  164. $existanceCheck = $xpath->query("//application/values/components/component[@name='" . $args["component"] . "']", $docPage);
  165. $docComponent = new DOMDocument();
  166. $docComponent->load(
  167. SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "values.xml"
  168. );
  169. $xp = new DOMXPath($docComponent);
  170. $componentRoot = $xp->query("//component", $docComponent);
  171. if($existanceCheck->length > 0)
  172. {
  173. $componentsRoot = $xpath->query("//application/values/components", $docPage);
  174. $componentsRoot->item(0)->replaceChild(
  175. $docPage->importNode($componentRoot->item(0), true),
  176. $existanceCheck->item(0)
  177. );
  178. }
  179. else
  180. {
  181. $componentsRoot = $xpath->query("//application/values/components", $docPage);
  182. $componentsRoot->item(0)->appendChild(
  183. $docPage->importNode($componentRoot->item(0), true)
  184. );
  185. }
  186. // Place the component in the designated placeholder
  187. if( isset($args["layoutArea"]) && !empty($args["layoutArea"]) )
  188. {
  189. $componentCheck = $xpath->query("//application/components/site:layout/" . $args["layoutArea"] . "/site:" . $args["component"], $docPage);
  190. if( $componentCheck->length == 0 )
  191. {
  192. $componentTag = $docPage->createElement( "component:" . $args["component"] );
  193. $configurationDoc = new DomDocument();
  194. $configurationDoc->load( SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "configuration.xml" );
  195. $configurationTag = $configurationDoc->getElementsByTagName("configuration")->item(0);
  196. // Append attributes
  197. // Append child nodes
  198. for($i=0;$i<$configurationTag->childNodes->length;$i++)
  199. {
  200. $componentTag->appendChild(
  201. $configurationTag->item($i)->cloneNode()
  202. );
  203. }
  204. // Append the new node
  205. $componentCheck->item(0)->appendChild( $componentTag );
  206. }
  207. }
  208. // Save the modifications
  209. $docPage->save( $destPrefix . DIRECTORY_SEPARATOR . $args["page"] . ".xml" );
  210. // Create configuration folder and files
  211. @ mkdir( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] );
  212. @ mkdir( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "client" );
  213. /*
  214. copy(
  215. SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "configuration.xml",
  216. $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "client" . DIRECTORY_SEPARATOR . "configuration.xml"
  217. );
  218. */
  219. @ mkdir( $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "server" );
  220. // Server security settings
  221. file_put_contents(
  222. $destPrefix . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["component"] . DIRECTORY_SEPARATOR . "server" . DIRECTORY_SEPARATOR . ".htaccess",
  223. "Order allow,deny\nDeny from all"
  224. );
  225. /*
  226. Finalize
  227. */
  228. return self::setError( self::ERR_OK );
  229. }
  230. public function updateAll( $args )
  231. {
  232. /*
  233. Params eval
  234. */
  235. if(!is_array($args))
  236. {
  237. return self::setError( self::ERR_INVALID_ARGS );
  238. }
  239. if(
  240. !isset($args["area"]) ||
  241. (
  242. $args["area"] != self::AREA_PUBLIC &&
  243. $args["area"] != self::AREA_ADMIN
  244. )
  245. )
  246. {
  247. return self::setError( self::ERR_NO_AREA_NAME );
  248. }
  249. if(
  250. !isset($args["component"]) ||
  251. empty($args["component"])
  252. )
  253. {
  254. return self::setError( self::ERR_NO_COMPONENT_NAME );
  255. }
  256. if(
  257. !isset($args["component"]) ||
  258. empty($args["component"])
  259. )
  260. {
  261. return self::setError( self::ERR_NO_COMPONENT_NAME );
  262. }
  263. if(
  264. !isset($args["overwriteTranslations"])
  265. )
  266. {
  267. return self::setError( self::ERR_NO_OVERWRITE_SPECIFIED );
  268. }
  269. /*
  270. Functional part
  271. */
  272. $pointer = opendir( SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "pages" );
  273. while(false !== ($file = readdir($pointer)))
  274. {
  275. // Exclude dot files
  276. if( $file == "." || $file == ".." || substr( $file, 0, 1 ) == "." )
  277. {
  278. continue;
  279. }
  280. $doc = new DOMDocument();
  281. $doc->load( SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "pages" . DIRECTORY_SEPARATOR . $file . DIRECTORY_SEPARATOR . $file . ".xml" );
  282. $xpath = new DOMXPath($doc);
  283. $xpath->registerNamespace("site", "http://site.emotionconcept.ro");
  284. $xpath->registerNamespace("component", "http://component.emotionconcept.ro");
  285. // Search for the component
  286. $components = $xpath->query("//application/components/site:meta/component:" . $args["component"], $doc);
  287. foreach($components as $component)
  288. {
  289. if(
  290. ComponentManipulation::ERR_OK !== ( $retVal = ComponentManipulation::addToPage( array(
  291. "area" => $args["area"],
  292. "page" => $file,
  293. "component" => $args["component"],
  294. "overwriteTranslations" => $args["overwriteTranslations"]
  295. ) ) )
  296. )
  297. {
  298. return self::setError( self::ERR_UPDATE );
  299. }
  300. }
  301. $components = $xpath->query("//application/components/site:layout/*/component:" . $args["component"], $doc);
  302. foreach($components as $component)
  303. {
  304. if(
  305. ComponentManipulation::ERR_OK !== ( $retVal = ComponentManipulation::addToPage( array(
  306. "area" => $args["area"],
  307. "page" => $file,
  308. "component" => $args["component"],
  309. "overwriteTranslations" => $args["overwriteTranslations"]
  310. ) ) )
  311. )
  312. {
  313. return self::setError( self::ERR_UPDATE );
  314. }
  315. }
  316. /*
  317. ComponentManipulation::addToPage( array(
  318. "area" => $args["area"],
  319. "page" => $file,
  320. "component" => $args["name"],
  321. ) );
  322. */
  323. }
  324. /*
  325. Finalize
  326. */
  327. return self::setError( self::ERR_OK );
  328. }
  329. public function create( $args )
  330. {
  331. /*
  332. Params eval
  333. */
  334. if(!is_array($args))
  335. {
  336. return self::setError( self::ERR_INVALID_ARGS );
  337. }
  338. $args["force"] = isset($args["force"]) ? $args["force"] : false;
  339. $args["configuration"] = isset($args["configuration"]) ? $args["configuration"] : false;
  340. if(
  341. !isset($args["area"]) ||
  342. (
  343. $args["area"] != self::AREA_PUBLIC &&
  344. $args["area"] != self::AREA_ADMIN
  345. )
  346. )
  347. {
  348. return self::setError( self::ERR_NO_AREA_NAME );
  349. }
  350. if(
  351. !isset($args["name"]) ||
  352. empty($args["name"])
  353. )
  354. {
  355. return self::setError( self::ERR_NO_COMPONENT_NAME );
  356. }
  357. /*
  358. Functional part
  359. */
  360. $destFolder = SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . $args["name"];
  361. if(
  362. file_exists( $destFolder ) &&
  363. $args["force"] == false
  364. )
  365. {
  366. return self::setError( self::ERR_COMPONENT_EXISTS );
  367. }
  368. if(
  369. FolderManipulation::ERR_OK !== ( $retVal = FolderManipulation::copy( array(
  370. "source" => SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . self::COMPONENT_DEFAULT_NAME,
  371. "destination" => $destFolder,
  372. "force" => $args["force"]
  373. ) ) )
  374. )
  375. {
  376. return self::setError( self::ERR_COPY );
  377. }
  378. // Rename component files
  379. rename(
  380. $destFolder . DIRECTORY_SEPARATOR . self::COMPONENT_DEFAULT_NAME . ".php",
  381. $destFolder . DIRECTORY_SEPARATOR . $args["name"] . ".php"
  382. );
  383. rename(
  384. $destFolder . DIRECTORY_SEPARATOR . self::COMPONENT_DEFAULT_NAME . ".xsl",
  385. $destFolder . DIRECTORY_SEPARATOR . $args["name"] . ".xsl"
  386. );
  387. rename(
  388. $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "component." . self::COMPONENT_DEFAULT_NAME . ".js",
  389. $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "component." . $args["name"] . ".js"
  390. );
  391. // Replace XSL component name
  392. file_put_contents(
  393. $destFolder . DIRECTORY_SEPARATOR . $args["name"] . ".xsl",
  394. str_replace(
  395. self::COMPONENT_DEFAULT_NAME,
  396. $args["name"],
  397. file_get_contents( $destFolder . DIRECTORY_SEPARATOR . $args["name"] . ".xsl" )
  398. )
  399. );
  400. // Replace PHP component name
  401. file_put_contents(
  402. $destFolder . DIRECTORY_SEPARATOR . $args["name"] . ".php",
  403. str_replace(
  404. self::COMPONENT_DEFAULT_NAME,
  405. $args["name"],
  406. file_get_contents( $destFolder . DIRECTORY_SEPARATOR . $args["name"] . ".php" )
  407. )
  408. );
  409. // Replace XML values component name
  410. file_put_contents(
  411. $destFolder . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "values.xml",
  412. str_replace(
  413. self::COMPONENT_DEFAULT_NAME,
  414. $args["name"],
  415. file_get_contents( $destFolder . DIRECTORY_SEPARATOR . "configuration" . DIRECTORY_SEPARATOR . "values.xml" )
  416. )
  417. );
  418. // Replace XML Translation
  419. $pointer = opendir( $destFolder . DIRECTORY_SEPARATOR . "translations" );
  420. while (false !== ($file = readdir($pointer)))
  421. {
  422. if ( is_dir( $destFolder . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $file ) )
  423. {
  424. continue;
  425. }
  426. file_put_contents(
  427. $destFolder . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $file,
  428. str_replace(
  429. self::COMPONENT_DEFAULT_NAME,
  430. $args["name"],
  431. file_get_contents( $destFolder . DIRECTORY_SEPARATOR . "translations" . DIRECTORY_SEPARATOR . $file )
  432. )
  433. );
  434. }
  435. // Replace JS
  436. file_put_contents(
  437. $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "component." . $args["name"] . ".js",
  438. str_replace(
  439. self::COMPONENT_DEFAULT_NAME,
  440. $args["name"],
  441. file_get_contents( $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . "component." . $args["name"] . ".js" )
  442. )
  443. );
  444. // Replace CSS Template name in all CSS files
  445. $pointer = opendir( $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" );
  446. while (false !== ($file = readdir($pointer)))
  447. {
  448. $info = pathinfo( $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . $file );
  449. if ($file == "." || $file == ".." || $info['extension'] != 'css')
  450. {
  451. continue;
  452. }
  453. file_put_contents(
  454. $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . $file,
  455. str_replace(
  456. self::COMPONENT_DEFAULT_NAME,
  457. $args["name"],
  458. file_get_contents( $destFolder . DIRECTORY_SEPARATOR . "resources" . DIRECTORY_SEPARATOR . "public" . DIRECTORY_SEPARATOR . $file )
  459. )
  460. );
  461. }
  462. closedir($pointer);
  463. // Create default configuration in all pages
  464. if($args["configuration"] == true)
  465. {
  466. $pointer = opendir( SystemConfig::$installDir . DIRECTORY_SEPARATOR . $args["area"] . DIRECTORY_SEPARATOR . "pages" );
  467. while(false !== ($file = readdir($pointer)))
  468. {
  469. if( $file == "." || $file == ".." || $file == "blankPage" )
  470. {
  471. continue;
  472. }
  473. ComponentManipulation::addToPage( array(
  474. "area" => $args["area"],
  475. "page" => $file,
  476. "component" => $args["name"],
  477. ) );
  478. }
  479. }
  480. /*
  481. Finalize
  482. */
  483. return self::setError( self::ERR_OK );
  484. }
  485. public function delete( $args )
  486. {
  487. }
  488. }
  489. ?>