PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/plugins/inc/magmi_plugin.php

https://bitbucket.org/jit_bec/shopifine
PHP | 341 lines | 293 code | 48 blank | 0 comment | 14 complexity | e17dbcfc97200790125d22c677a9a8a5 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. require_once("magmi_config.php");
  3. require_once("magmi_mixin.php");
  4. class Magmi_PluginConfig extends ProfileBasedConfig
  5. {
  6. protected $_prefix;
  7. protected $_conffile;
  8. public function __construct($pname,$profile=null)
  9. {
  10. $this->_prefix=$pname;
  11. parent::__construct("$this->_prefix.conf",$profile);
  12. }
  13. public function getConfDir()
  14. {
  15. return dirname($this->_confname);
  16. }
  17. public function load($name=null)
  18. {
  19. $cname=($name==null?$this->_confname:$name);
  20. if(file_exists($cname))
  21. {
  22. parent::load($cname);
  23. }
  24. }
  25. public function getIniStruct($arr)
  26. {
  27. $conf=array();
  28. foreach($arr as $k=>$v)
  29. {
  30. $k=$this->_prefix.":".$k;
  31. list($section,$value)=explode(":",$k,2);
  32. if(!isset($conf[$section]))
  33. {
  34. $conf[$section]=array();
  35. }
  36. $conf[$section][$value]=$v;
  37. }
  38. return $conf;
  39. }
  40. public function getConfig()
  41. {
  42. return parent::getsection($this->_prefix);
  43. }
  44. }
  45. class Magmi_PluginOptionsPanel
  46. {
  47. private $_plugin;
  48. private $_defaulthtml="";
  49. private $_file=null;
  50. public function __construct($pinst,$file=null)
  51. {
  52. $this->_plugin=$pinst;
  53. $this->_file=($file==null?"options_panel.php":$file);
  54. $this->initDefaultHtml();
  55. }
  56. public function getFile()
  57. {
  58. return $this->_file;
  59. }
  60. public final function initDefaultHtml()
  61. {
  62. $panelfile=dirname(__FILE__)."/magmi_default_options_panel.php";
  63. ob_start();
  64. require($panelfile);
  65. $this->_defaulthtml = ob_get_contents();
  66. ob_end_clean();
  67. }
  68. public function getHtml()
  69. {
  70. $plugin=$this->_plugin;
  71. $pdir=Magmi_PluginHelper::getInstance()->getPluginDir($this->_plugin);
  72. $panelfile="$pdir/".$this->getFile();
  73. $content="";
  74. if(!file_exists($panelfile))
  75. {
  76. $content=$this->_defaulthtml;
  77. }
  78. else
  79. {
  80. ob_start();
  81. require($panelfile);
  82. $content = ob_get_contents();
  83. ob_end_clean();
  84. }
  85. return $content;
  86. }
  87. public function __call($data,$arg)
  88. {
  89. return call_user_func_array(array($this->_plugin,$data), $arg);
  90. }
  91. }
  92. abstract class Magmi_Plugin extends Magmi_Mixin
  93. {
  94. protected $_class;
  95. protected $_plugintype;
  96. protected $_plugindir;
  97. protected $_config;
  98. protected $_magmiconfig;
  99. protected $_pluginmeta;
  100. public function __construct()
  101. {
  102. }
  103. public function getParam($pname,$default=null)
  104. {
  105. return (isset($this->_params[$pname]) && $this->_params[$pname]!="")?$this->_params[$pname]:$default;
  106. }
  107. public function setParam($pname,$value)
  108. {
  109. $this->_params[$pname]=$value;
  110. }
  111. public function fixListParam($pvalue)
  112. {
  113. $iarr=explode(",",$pvalue);
  114. $oarr=array();
  115. foreach($iarr as $v)
  116. {
  117. if($v!="")
  118. {
  119. $oarr[]=$v;
  120. }
  121. }
  122. $val=implode(",",$oarr);
  123. unset($iarr);
  124. unset($oarr);
  125. return $val;
  126. }
  127. public function getPluginParamNames()
  128. {
  129. return array();
  130. }
  131. public function getPluginInfo()
  132. {
  133. return array("name"=>$this->getPluginName(),
  134. "version"=>$this->getPluginVersion(),
  135. "author"=>$this->getPluginAuthor(),
  136. "url"=>$this->getPluginUrl());
  137. }
  138. public function getPluginUrl()
  139. {
  140. return null;
  141. }
  142. public function getPluginVersion()
  143. {
  144. return null;
  145. }
  146. public function getPluginName()
  147. {
  148. return null;
  149. }
  150. public function getPluginAuthor()
  151. {
  152. return null;
  153. }
  154. public function log($data,$type='std',$useprefix=true)
  155. {
  156. $pinf=$this->getPluginInfo();
  157. if($useprefix)
  158. {
  159. $data="{$pinf["name"]} v{$pinf["version"]} - ".$data;
  160. }
  161. $this->_caller_log($data,"plugin;$this->_class;$type");
  162. }
  163. public function pluginHello()
  164. {
  165. $info=$this->getPluginInfo();
  166. $hello=array(!isset($info["name"])?"":$info["name"]);
  167. $hello[]=!isset($info["version"])?"":$info["version"];
  168. $hello[]=!isset($info["author"])?"":$info["author"];
  169. $hello[]=!isset($info["url"])?"":$info["url"];
  170. $hellostr=implode("-",$hello);
  171. $base=get_parent_class($this);
  172. $this->log("$hellostr ","pluginhello",false);
  173. }
  174. public function initialize($params)
  175. {
  176. }
  177. public function getConfig()
  178. {
  179. return $this->_config;
  180. }
  181. public function getMagmiConfig()
  182. {
  183. return $this->_magmiconfig;
  184. }
  185. public final function pluginInit($mmi,$meta,$params=null,$doinit=true,$profile=null)
  186. {
  187. $this->bind($mmi);
  188. $this->_pluginmeta=$meta;
  189. $this->_class=get_class($this);
  190. $this->_config=new Magmi_PluginConfig(get_class($this),$profile);
  191. $this->_config->load();
  192. $this->_magmiconfig=Magmi_Config::getInstance();
  193. $this->_params=($params!=null?array_merge($this->_config->getConfig(),$params):$this->_config->getConfig());
  194. if(isset($mmi))
  195. {
  196. $this->pluginHello();
  197. }
  198. if($doinit)
  199. {
  200. $this->initialize($this->_params);
  201. }
  202. }
  203. public function getPluginParamsNoCurrent($params)
  204. {
  205. $arr=array();
  206. $paramkeys=$this->getPluginParamNames();
  207. foreach($paramkeys as $pk)
  208. {
  209. if(isset($params[$pk]))
  210. {
  211. $arr[$pk]=$params[$pk];
  212. }
  213. else
  214. {
  215. $arr[$pk]=0;
  216. }
  217. }
  218. return $arr;
  219. }
  220. public function getPluginParams($params)
  221. {
  222. $arr=array();
  223. $paramkeys=$this->getPluginParamNames();
  224. foreach($paramkeys as $pk)
  225. {
  226. if(isset($params[$pk]))
  227. {
  228. $arr[$pk]=$params[$pk];
  229. }
  230. else
  231. {
  232. if(isset($this->_params[$pk]))
  233. {
  234. $arr[$pk]=$this->_params[$pk];
  235. }
  236. }
  237. }
  238. return $arr;
  239. }
  240. public function persistParams($plist)
  241. {
  242. if(count($plist)>0)
  243. {
  244. $this->_config->setPropsFromFlatArray($plist);
  245. return $this->_config->save();
  246. }
  247. return true;
  248. }
  249. public function getOptionsPanel($file=null)
  250. {
  251. return new Magmi_PluginOptionsPanel($this,$file);
  252. }
  253. public function getShortDescription()
  254. {
  255. $panel=$this->getOptionsPanel()->getHtml();
  256. $info=null;
  257. if(preg_match('|<div class="plugin_description">(.*?)</div>|smi',$panel,$match))
  258. {
  259. $info=$match[1];
  260. $delims=array(".",":");
  261. foreach($delims as $delim)
  262. {
  263. $p=strpos($info,$delim);
  264. if($p!==false)
  265. {
  266. $info=substr($info,0,$p);
  267. break;
  268. }
  269. }
  270. }
  271. return $info;
  272. }
  273. static public function getCategory()
  274. {
  275. return "common";
  276. }
  277. public function getPluginDir()
  278. {
  279. return $this->_pluginmeta["dir"];
  280. }
  281. public function getPluginMeta()
  282. {
  283. return $this->_pluginmeta;
  284. }
  285. public function getPluginClass()
  286. {
  287. return $this->_class;
  288. }
  289. public function isRunnable()
  290. {
  291. return array(true,"");
  292. }
  293. }